Jak*_*ake 15 interaction regression r
为了帮助填充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)从其他角度更好?
Ian*_*ows 17
效果包具有良好的绘图方法,用于可视化回归的预测值.
thedata<-data.frame(x=rnorm(20),f=rep(c("level1","level2"),10))
thedata$y<-rnorm(20,,3)+thedata$x*(as.numeric(thedata$f)-1)
library(effects)
model.lm <- lm(formula=y ~ x*f,data=thedata)
plot(effect(term="x:f",mod=model.lm,default.levels=20),multiline=TRUE)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
33021 次 |
最近记录: |