R具有鲁棒线性回归模型(RLM)的R置信区间线
我需要用置信区间绘制一个稳定线性回归(RLM)模型的散点图,这是我发现仅与LM一起使用的所有示例。
这是我的代码:
model1 <- rlm(weightsE$brain ~ weightsE$body)
newx <- seq(min(weightsE$body), max(weightsE$body), length.out=70)
newx<-as.data.frame(newx)
colnames(newx)<-"brain"
conf_interval <- predict(model1, newdata = data.frame(x=newx), interval = 'confidence',
level=0.95)
#create scatterplot of values with regression line
plot(weightsE$body, weightsE$body)
abline(model1)
#add dashed lines (lty=2) for the 95% confidence interval
lines(newx, conf_interval[,2], col="blue", lty=2)
lines(newx, conf_interval[,3], col="blue", lty=2)
但是预测的结果不会为上和下层产生直线,它们更像是随机的预测。
I need to plot a Scatterplot with the confidence interval for a robust linear regression (rlm) model, all the examples I had found only work with LM.
This is my code:
model1 <- rlm(weightsE$brain ~ weightsE$body)
newx <- seq(min(weightsE$body), max(weightsE$body), length.out=70)
newx<-as.data.frame(newx)
colnames(newx)<-"brain"
conf_interval <- predict(model1, newdata = data.frame(x=newx), interval = 'confidence',
level=0.95)
#create scatterplot of values with regression line
plot(weightsE$body, weightsE$body)
abline(model1)
#add dashed lines (lty=2) for the 95% confidence interval
lines(newx, conf_interval[,2], col="blue", lty=2)
lines(newx, conf_interval[,3], col="blue", lty=2)
but the results of predict don't produce a straight line for the upper and lower level, they are more like random predictions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一些问题要解决。
rlm(poatese $ brain〜pofese $ body)
,而是使用rlm(brain〜ody,data = positese)
。否则,该模型将无法获取新数据进行预测。 将从原始poatese $ body
值中产生Newx
已经是数据框架,但是由于某种原因,您将其包装在内部newx
。newdata = data.frame(x = newx)
时,只需通过 Body),当它应为绘图(poatese $ hophos,poadse $ brain)
将所有这些放在一起,并使用带有与您自己的名称相同名称的虚拟数据集(请参阅下文) ,我们得到:
顺便说一句,您可以在GGPLOT中完成整个代码:
You have a few problems to fix here.
rlm(weightsE$brain ~ weightsE$body)
, instead userlm(brain ~ body, data = weightsE)
. Otherwise, the model cannot take new data for predictions. Any predictions you get will be produced from the originalweightsE$body
values, not from the new data you pass intopredict
newx
is already a data frame, but for some reason you are wrapping it inside another data frame when you donewdata = data.frame(x=newx)
. Just passnewx
.plot(weightsE$body, weightsE$body)
, when it should beplot(weightsE$body, weightsE$brain)
Putting all this together, and using a dummy data set with the same names as your own (see below), we get:
Incidentally, you could do the whole thing in ggplot in much less code:
Reproducible dummy data