1
我需要一些幫助來繪製置信區間的預測圖。請看下面的例子當繪製線性模型預測的95%置信區間時出現不良情節
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"))
我在做什麼錯在這裏?謝謝!
你也可以使用專門做的正是這種功能,像visreg包:'庫(visreg); visreg(mfit,xvar =「vs」)' – agenis