Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot "regression line" from multiple regression in R

Tags:

plot

r

regression

I ran a multiple regression with several continuous predictors, a few of which came out significant, and I'd like to create a scatterplot or scatter-like plot of my DV against one of the predictors, including a "regression line". How can I do this?

My plot looks like this

D = my.data; plot( D$probCategorySame, D$posttestScore )

If it were simple regression, I could add a regression line like this:

lmSimple <- lm( posttestScore ~ probCategorySame, data=D )
abline( lmSimple ) 

But my actual model is like this:

lmMultiple <- lm( posttestScore ~ pretestScore + probCategorySame + probDataRelated + practiceAccuracy + practiceNumTrials, data=D )

I would like to add a regression line that reflects the coefficient and intercept from the actual model instead of the simplified one. I think I'd be happy to assume mean values for all other predictors in order to do this, although I'm ready to hear advice to the contrary.

This might make no difference, but I'll mention just in case, the situation is complicated slightly by the fact that I probably will not want to plot the original data. Instead, I'd like to plot mean values of the DV for binned values of the predictor, like so:

D[,'probCSBinned'] = cut( my.data$probCategorySame, as.numeric( seq( 0,1,0.04 ) ), include.lowest=TRUE, right=FALSE, labels=FALSE )
D = aggregate( posttestScore~probCSBinned, data=D, FUN=mean )
plot( D$probCSBinned, D$posttestScore )

Just because it happens to look much cleaner for my data when I do it this way.

like image 516
baixiwei Avatar asked Jul 12 '13 13:07

baixiwei


1 Answers

To plot the individual terms in a linear or generalised linear model (ie, fit with lm or glm), use termplot. No need for binning or other manipulation.

# plot everything on one page
par(mfrow=c(2,3))
termplot(lmMultiple)

# plot individual term
par(mfrow=c(1,1))
termplot(lmMultiple, terms="preTestScore")
like image 157
Hong Ooi Avatar answered Nov 13 '22 21:11

Hong Ooi