0

I'm just wondering if it is possible to plot the regression line for a particular variable from a multivariate regression (using lm()), while holding other variables at their mean, and fit the regression line on a bivariate scatter plot? There's a function called cplot() from margins package that will do the trick but the function doesn't seem to be able to include data points on the X-Y plane. So I am wondering if someone has tried using lm() function to do this?

library(ISLR)
data(Carseats)

lm.fit <- lm(Sales ~ Income + Advertising + Price, data = Carseats)

plot(Carseats$Income, Carseats$Sales, cex = 1.3, col = "red", pch = 19, main = "The relationship between Car Sales and Income", xlab = "Income", ylab = "Car Sales")

Is it possible to fit lm.fit on the bivariate plot along the dimension of Sales and Income, while holding other variables at their means?

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Chris T.
  • 1,699
  • 7
  • 23
  • 45

1 Answers1

1

You can compute this line without much trouble. You already have the slope built into lm.fit. The only issue is what should you use as the intercept? We want to look at the plane where Advertising and Price are held at their means. We want the point where the regression line in this plane crosses the Sales-axis, so the intercept will be the value where

Advertising = mean(Carseats$Advertising)
Price = mean(Carseats$Price)
and
Income = 0

so just use your model to compute that value.

## Assuming that you made your plot as in the question
IntPoint = data.frame(Income = 0, 
    Advertising = mean(Carseats$Advertising), 
    Price = mean(Carseats$Price))
Int2 = predict(lm.fit, IntPoint)
abline(Int2, lm.fit$coefficients[2])

Graph with added regression line

G5W
  • 36,531
  • 10
  • 47
  • 80