Line "regression line" from multiple regression to R

I conducted a multiple regression with several continuous predictors, some of which turned out significant, and I would like to create a scatter chart or a scatter chart of my DV against one of the predictors, including the "regression line", How can I do this?

My plot is as follows

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

If it were a simple regression, I could add a regression line as follows:

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

But my actual model looks 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 interception from the actual model instead of the simplified one. I think that I would be happy to suggest averages for all other predictors to do this, although I am ready to hear advice on the contrary.

It does not matter, but just in case, I mentioned that the situation is a little complicated by the fact that I probably do not want to build the source data. Instead, I would like to calculate the average DV values ​​for the predictor bin values, for example:

 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 looks a lot cleaner for my data when I do it like this.

+8
source share
3 answers

You need to create a vector of x-values ​​in your plot area and predict their corresponding y values ​​from your model. To do this, you need to insert this vector into the data framework, consisting of variables that correspond to those specified in your model. You stated that you are fine by keeping the other variables fixed in average, so I used this approach in my solution. Regardless of whether the x values ​​you predict are legitimate, since the other values ​​in your plot should probably be what you consider when setting this up.

Without sample data, I can’t be sure that this will work just for you, so I apologize if there are any errors below, but this should at least illustrate the approach.

 # Setup xmin = 0; xmax=10 # domain of your plot D = my.data plot( D$probCategorySame, D$posttestScore, xlim=c(xmin,xmax) ) lmMultiple <- lm( posttestScore ~ pretestScore + probCategorySame + probDataRelated + practiceAccuracy + practiceNumTrials, data=D ) # create a dummy dataframe where all variables = their mean value for each record # except the variable we want to plot, which will vary incrementally over the # domain of the plot. We need this object to get the predicted values we # want to plot. N=1e4 means = colMeans(D) dummyDF = t(as.data.frame(means)) for(i in 2:N){dummyDF=rbind(dummyDF,means)} # There probably a more elegant way to do this. xv=seq(xmin,xmax, length.out=N) dummyDF$probCSBinned = xv # if this gives you a warning about "Coercing LHS to list," use bracket syntax: #dummyDF[,k] = xv # where k is the column index of the variable `posttestScore` # Getting and plotting predictions over our dummy data. yv=predict(lmMultiple, newdata=subset(dummyDF, select=c(-posttestScore))) lines(xv, yv) 
+6
source

To build individual members in a linear or generalized linear model (i.e. adapt to lm or glm ), use termplot . No need for binning or other manipulations.

 # plot everything on one page par(mfrow=c(2,3)) termplot(lmMultiple) # plot individual term par(mfrow=c(1,1)) termplot(lmMultiple, terms="preTestScore") 
+8
source

Look at the Predict.Plot function in the TeachingDemos package for one option to build one predictor compared to the answer given the value of other predictors.

+3
source

Source: https://habr.com/ru/post/949314/


All Articles