我需要一点帮助,用它的置信区间绘制预测。请考虑以下示例
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)
当所有其他变量保持不变(均值/模式)时,我想绘制vs
(当为0和1时)的效果。
为此,我运行以下代码:
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"))
我在这里做错了什么?谢谢!
答案 0 :(得分:1)
请注意,您有ylim = c(0, 1)
,这是不正确的。在绘制置信区间时,我们必须确保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)
关于您的代码的其他一些评论:
fit - 1.96 * se.fit
而不是fit - 1.96 * fit
; 0:1
上绘图,而不是1:2
; fit[1:2]
而不是fit[1,1]
。答案 1 :(得分:1)