是否可以使用 lm() 将其他变量保持在它们的平均值并在双变量散点图上绘制回归线?

Can one use lm() to hold other variables at their mean and plot the regression line on a bivariate scatter plot?

我只是想知道是否可以从多元回归(使用 lm())中绘制特定变量的回归线,同时保持其他变量的均值,并将回归线拟合到双变量散点图? margins 包中有一个名为 cplot() 的函数可以解决问题,但该函数似乎无法在 X-Y 平面上包含数据点。所以我想知道是否有人尝试过使用 lm() 函数来做到这一点?

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")

是否可以在双变量图上沿 SalesIncome 的维度拟合 lm.fit,同时保持其他变量的均值?

您可以毫不费力地计算这条直线。 您已经在 lm.fit 中内置了坡度。 唯一的问题是你应该使用什么作为拦截? 我们想看看广告和价格所在的平面 量力而为。我们想要的点是 该平面上的回归线与销售轴相交, 所以截距将是

的值

广告=均值(汽车座椅$广告)
价格 = 均值(Carseats$Price)

收入 = 0

所以只需使用您的模型来计算该值。

## 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])