简体   繁体   English

绘制线性模型预测的95%置信区间时的错误图表

[英]Bad plot when plotting 95% confidence interval of linear model prediction

I need a little help with plotting prediction with its confidence interval. 在用其置信区间绘制预测时,我需要一些帮助。 Consider the following example 考虑以下示例

library(Hmisc)
data("mtcars") 

mfit = lm(mpg ~ vs + disp + cyl, data = mtcars)

#disp and cyl at their mean
newcar = data.frame(vs = c(0,1), disp = 230, cyl = 6.188)

pmodel <- predict(mfit, newcar, se.fit=TRUE)

I want to plot the effect of vs (when 0 and 1) when all other variables are held constant (mean / mode). 当所有其他变量保持恒定(均值/模式)时,我想绘制vs (在0和1时)的效果。

To do this I run this code below: 为此,我在下面运行此代码:

plot(1:2, pmodel$fit[1:2], ylim=c(0,1), pch=19, xlim=c(.5,2.5), xlab="X",
     ylab = "Predicted values", xaxt = "n", main = "Figure1")
arrows(1:2, (pmodel$fit[1:2] - 1.96 * pmodel$fit[1:2]),
       1:2, (pmodel$fit[1,1] + 1.96 * pmodel$fit[1:2]),
       length=0.05, angle=90, code=3)
axis(1, at=c(1,2), labels=c("0","1"))

What am I doing wrong here? 我在这里做错了什么? Thanks! 谢谢!

Note you have ylim = c(0, 1) , which is incorrect. 请注意,您有ylim = c(0, 1) ,这是不正确的。 When drawing confidence interval, we have to make sure ylim covers the lower and upper bound of CI. 在绘制置信区间时,我们必须确保ylim覆盖CI的上下边界。

## lower and upper bound of CI
lower <- with(pmodel, fit - 1.96 * se.fit)
upper <- with(pmodel, fit + 1.96 * se.fit)

## x-location to plot
xx <- 0:1

## set `xlim` and `ylim`
xlim <- range(xx) + c(-0.5, 0.5)  ## extends an addition 0.5 on both sides
ylim <- range(c(lower, upper))

## produce figure
plot(xx, pmodel$fit, pch = 19, xlim = xlim, ylim = ylim, xaxt = "n",
     xlab = "X", ylab = "Predicted values", main = "Figure1")
arrows(xx, lower, xx, upper, length = 0.05, angle = 90, code = 3)
axis(1, at = xx)

在此处输入图片说明

Several other comments on your code: 关于您的代码的其他一些注释:

  • it is fit - 1.96 * se.fit not fit - 1.96 * fit ; fit - 1.96 * se.fitfit - 1.96 * fit fit - 1.96 * se.fit fit - 1.96 * fit ;
  • you can plot directly on x-location 0:1 , rather than 1:2 ; 您可以直接在x位置0:1而不是1:2上绘制;
  • it is fit[1:2] not fit[1,1] . fit[1:2]fit[1,1]

In ggplot: 在ggplot中:

df <- data.frame(x=1:2, pred=pmodel$fit[1:2])
df$lower <- df$pred - 1.96 * pmodel$se.fit[1:2] 
df$upper <- df$pred + 1.96 * pmodel$se.fit[1:2]
ggplot(df, aes(x, pred, group=x, col=as.factor(x))) + geom_point(size=2) + 
  geom_errorbar(aes(ymin=lower, ymax=upper), width=0.1)

在此处输入图片说明

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

相关问题 在线性混合 model 的预测回归线周围绘制 95% 置信区间带 - Plotting a 95% confidence interval band around a predicted regression line from a linear mixed model 如何计算R中线性回归模型中斜率的95%置信区间 - How to calculate the 95% confidence interval for the slope in a linear regression model in R 如何对ggplot中的线性模型预测以及置信区间进行可视化? - How to visulaize linear model prediction in ggplot along with confidence interval? 绘制 lm 对象的 95% 置信区间 - Plotting a 95% confidence interval for a lm object survfit()阴影95%置信区间生存图 - survfit() Shade 95% confidence interval survival plot 垂直 95% 置信区间 plot 2 组比较 - Vertical 95% Confidence Interval plot 2 groups comparison 向 NMDS plot 添加 95% 置信区间 - Adding a 95% confidence interval to NMDS plot R 用稳健的线性回归模型 (rlm) 绘制置信区间线 - R plot confidence interval lines with a robust linear regression model (rlm) R - 通过自举带线性模型计算R平方和残差标准误差的95%置信区间 - R - calculating 95% confidence interval for R-squared and residual standard error by boot strap linear model 绘制对数对数线性 model 预测值的 95% 置信区间 - Plotting 95% confidence intervals of log-log linear model predicted values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM