Xingchen LIU
Xingchen LIU

Reputation: 91

linear regression residuals scatterplot

I ran an linear regression with the following commands:

lm.intp <- lm(intp.trust~age,data=Scountry)

And then i hope to draw a scatterplot to compare the residuals of different genders. i use the Scountry$res <- lm.intp$residuals commands to put the regression residuals into the data frame, and then use ggplot to draw the scatterplot. But when i run Scountry$res <- lm.intp$residuals, it keep saying the existing data and assigned data have different rows. how can i avoid this situation?

And when i draw the scatterplot, i hope to use the following commands:

ggplot(Scountry, aes(x=, y=res, color=as.factor(gender))) +geom_point()

I know that in this plot, y should be the residuals, and the x should be the observations of this data, but i really have no idea what should be wrote in "x=" since the observations in my data have no ID, it's look like this:enter image description here

Could anyone please help me solve this questions? i'd be really appreciated!

Upvotes: 0

Views: 204

Answers (1)

Bloxx
Bloxx

Reputation: 1560

Hmm... It works here. I created data the fit your description. Do you maybe have NAs in your dataset?

Scountry <- data.frame(intp.trust = seq(200, 205),
                       age = seq(20,25),
                       gender= c("F", "M", "F", "M", "F", NA))

Scountry_lm <- Scountry %>% select(intp.trust, age, gender) %>% na.omit()


lm.intp <- lm(intp.trust~age,data=Scountry_lm)

Scountry_lm$res <- lm.intp$residuals
ggplot(Scountry_lm, aes(x= age,y=res, color=as.factor(gender))) +geom_point()

Upvotes: 1

Related Questions