![](/img/trans.png)
[英]Plotting a 95% confidence interval band around a predicted regression line from a linear mixed model
[英]Bad plot when plotting 95% confidence interval of linear model prediction
在用其置信區間繪制預測時,我需要一些幫助。 考慮以下示例
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"))
我在這里做錯了什么? 謝謝!
請注意,您有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
fit - 1.96 * se.fit
fit - 1.96 * fit
; 0:1
而不是1:2
上繪制; fit[1:2]
不fit[1,1]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.