为了帮助在这里填充 R 标签,我发布了一些我经常从学生那里收到的问题。多年来,我已经对这些问题提出了自己的答案,但也许还有更好的方法我不知道。
问题:我只是用连续y
和x
但因子f
(其中levels(f)
产生c("level1","level2")
)进行回归
thelm <- lm(y~x*f,data=thedata)
现在我想绘制由定义的组分解的y
预测x
值f
。我得到的所有情节都很丑陋,而且线条太多。
我的回答:试试这个predict()
功能。
##restrict prediction to the valid data
##from the model by using thelm$model rather than thedata
thedata$yhat <- predict(thelm,
newdata=expand.grid(x=range(thelm$model$x),
f=levels(thelm$model$f)))
plot(yhat~x,data=thethedata,subset=f=="level1")
lines(yhat~x,data=thedata,subset=f=="level2")
是否有其他想法(1)对于新手来说更容易理解和/或(2)从其他角度更好?