R具有鲁棒线性回归模型(RLM)的R置信区间线

发布于 2025-01-27 23:03:39 字数 701 浏览 3 评论 0原文

我需要用置信区间绘制一个稳定线性回归(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2025-02-03 23:03:39

您有一些问题要解决。

  1. 当您生成模型时,请勿使用rlm(poatese $ brain〜pofese $ body),而是使用rlm(brain〜ody,data = positese)。否则,该模型将无法获取新数据进行预测。 将从原始poatese $ body值中产生
  2. 您得到的任何预测都 称为“大脑”,但您正在尝试预测“大脑”的价值,因此您需要一个称为“ Body”的列
  3. Newx已经是数据框架,但是由于某种原因,您将其包装在内部 newx
  4. 当您进行newdata = data.frame(x = newx)时,只需通过 Body),当它应为绘图(poatese $ hophos,poadse $ brain)

将所有这些放在一起,并使用带有与您自己的名称相同名称的虚拟数据集(请参阅下文) ,我们得到:

library(MASS)

model1 <- rlm(brain ~ body, data = weightsE)

newx <- data.frame(body = seq(min(weightsE$body), 
                              max(weightsE$body), length.out=70))

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$brain)
abline(model1)

#add dashed lines (lty=2) for the 95% confidence interval
lines(newx$body, conf_interval[, 2], col = "blue", lty = 2)
lines(newx$body, conf_interval[, 3], col = "blue", lty = 2)

“在这里输入图像描述”

顺便说一句,您可以在GGPLOT中完成整个代码:

library(ggplot2)

ggplot(weightsE, aes(body, brain)) + 
  geom_point() + 
  geom_smooth(method = MASS::rlm)

“在此处输入图像说明”

可重复可重复的虚拟数据

data(mtcars)
weightsE <- setNames(mtcars[c(1, 6)], c("brain", "body"))
weightsE$body <- 10 - weightsE$body

You have a few problems to fix here.

  1. When you generate a model, don't use rlm(weightsE$brain ~ weightsE$body), instead use rlm(brain ~ body, data = weightsE). Otherwise, the model cannot take new data for predictions. Any predictions you get will be produced from the original weightsE$body values, not from the new data you pass into predict
  2. You are trying to create a prediction data frame with a column called "brain', but you are trying to predict the value of "brain", so you need a column called "body"
  3. newx is already a data frame, but for some reason you are wrapping it inside another data frame when you do newdata = data.frame(x=newx). Just pass newx.
  4. You are plotting with plot(weightsE$body, weightsE$body), when it should be plot(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:

library(MASS)

model1 <- rlm(brain ~ body, data = weightsE)

newx <- data.frame(body = seq(min(weightsE$body), 
                              max(weightsE$body), length.out=70))

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$brain)
abline(model1)

#add dashed lines (lty=2) for the 95% confidence interval
lines(newx$body, conf_interval[, 2], col = "blue", lty = 2)
lines(newx$body, conf_interval[, 3], col = "blue", lty = 2)

enter image description here

Incidentally, you could do the whole thing in ggplot in much less code:

library(ggplot2)

ggplot(weightsE, aes(body, brain)) + 
  geom_point() + 
  geom_smooth(method = MASS::rlm)

enter image description here

Reproducible dummy data

data(mtcars)
weightsE <- setNames(mtcars[c(1, 6)], c("brain", "body"))
weightsE$body <- 10 - weightsE$body
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文