简体   繁体   English

R 用稳健的线性回归模型 (rlm) 绘制置信区间线

[英]R plot confidence interval lines with a robust linear regression model (rlm)

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.我需要为稳健的线性回归 (rlm) 模型绘制带有置信区间的散点图,我发现的所有示例仅适用于 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.但是predict的结果并没有为上下层产生一条直线,它们更像是随机预测。

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) .生成模型时,不要使用rlm(weightsE$brain ~ weightsE$body) ,而是使用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您获得的任何预测都将由原始weightsE$body值产生,而不是由您传递给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"您正在尝试使用名为“brain”的列创建预测数据框,但您正在尝试预测“brain”的值,因此您需要一个名为“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) . newx已经是一个数据框,但由于某种原因,当您执行newdata = data.frame(x=newx)时,您将其包装在另一个数据框中。 Just pass newx .只需通过newx
  4. You are plotting with plot(weightsE$body, weightsE$body) , when it should be plot(weightsE$body, weightsE$brain)你正在用plot(weightsE$body, weightsE$body)绘图,它应该是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)

在此处输入图像描述

Incidentally, you could do the whole thing in ggplot in much less code:顺便说一句,您可以用更少的代码在 ggplot 中完成所有工作:

library(ggplot2)

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

在此处输入图像描述

Reproducible dummy data可重现的虚拟数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R 中的置信区间 plot 的线性回归预测 - linear regression predictions with confidence interval plot in R R 中线性回归 model 后的置信区间 - Confidence Interval after linear regression model in R 如何计算R中线性回归模型中斜率的95%置信区间 - How to calculate the 95% confidence interval for the slope in a linear regression model in R 非线性回归模型的置信区间 - Confidence Interval for Non Linear Regression Model R中线性回归的截距的置信区间 - The confidence interval by the intercept with linear regression in R 线性回归的置信区间 - Confidence interval for linear regression R 中的 Function 计算线性回归的异方差稳健置信区间 - Function in R that computes heteroskedasticity-robust confidence intervals for a linear regression "获得稳健回归系数的置信区间 (MASS::rlm)" - Getting confidence intervals for robust regression coefficient (MASS::rlm) 将线性回归 model(&gt; 3 个预测变量)中的两条(!)回归线绘制到具有置信区间(sjPlot 或 ggplot)的相同 plot 中 - Plotting two(!) regression lines from a linear regression model ( > 3 predictors) into the same plot with confidence intervals (sjPlot or ggplot) 如何在R中制作回归估计器线性组合的置信区间? - How to make confidence interval of linear combination of regression estimator in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM