为了帮助填充R标签,我发布了一些我经常从学生那里收到的问题.多年来,我已经对这些问题提出了自己的答案,但也许还有更好的方法,我不知道.
问题:我只是跑了一个回归连续y和x,但因子f(如levels(f)生产c("level1","level2"))
thelm <- lm(y~x*f,data=thedata)
Run Code Online (Sandbox Code Playgroud)
现在我想绘制的预测值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")
Run Code Online (Sandbox Code Playgroud)
是否有其他想法(1)对于新手更容易理解和/或(2)从其他角度更好?