我想绘制包含线性和非线性关系的多元回归模型的结果 . 我希望每个预测器有一个绘图,条件是其他预测变量 .
这是我的示例数据:
library(car)
library(broom)
library(jtools)
## fit model:
model <- lm(mpg ~ poly(disp,3) + wt + carb + factor(gear), data = mtcars)
summary(model)
函数“avPlots”创建了附加变量图,这是一个很好的起点:
avPlots(model)
Is there a way to produce single added-variable plots for each predictor (similar to avPlots), that also plots curvlinear relationships? Possibly a solution with ggplot?
我的解决方案就是这个,但这似乎不起作用:
sub <- augment(model, mtcars) # use augment from broom to get fitted values
plot(sub$wt, sub$.fitted) #
abline(sub$.fitted, sub$wt, col="blue", lwd=3)
回归线不适合这些点 . 此外,这不允许绘制非线性关系 .
类似地,“effect_plot”绘制单个添加变量图 . 但是,这只能在没有多项式关系的情况下工作:
model <- lm(mpg ~ wt + carb + factor(gear), data = mtcars) # model without poly
summary(model)
effect_plot(model, pred=wt, plot.points = TRUE)
谢谢你的帮助!!!