所以我需要绘制我运行的预测的置信区间。我可以运行预测,但是当我进行预测图时,我得到了一条直线来处理我的所有数据点,而不是获得实际的置信区间。
GunRate <- seq(0,100, length = 51)
LinearPredictionA <- predict(ModelA,
interval = "confidence",
newdata = data.frame(ProportionAdultsLivingWithGun = GunRate,
LogMedianIncome = FinalSet$LogMedianIncome,
PctofPeopleinMetro = FinalSet$PctofPeopleinMetro,
PovertyRate = FinalSet$PovertyRate))
##This is my prediction model
plot(x = FinalSet$ProportionAdultsLivingWithGun,
y = FinalSet$ViolentCrime1K,
col = "red",
xlim = c(0, 80), ylim = c(0, 15),
xlab ="Proportion of Adults Living With a Gun",
ylab = "Violent Crime Rate per 1000",
main = "Violent Crime vs. Gun Ownership",
sub = "All 50 States & D.C.")
## This plot shows the actual data we used to obtain the prediction
lines(GunRate, LinearPredictionA[, "fit"], type = "l")
lines(GunRate, LinearPredictionA[, "lwr"], lty = "dashed", col = "green")
lines(GunRate, LinearPredictionA[, "upr"], lty = "dashed", col = "green")
这些行函数应该用于绘制我的CI,但我得到以下图表
答案 0 :(得分:3)
以下是使用内置mtcars
数据框出现问题的示例:
# Regression model
m1 = lm(mpg ~ wt + hp + cyl, data=mtcars)
现在让我们预测mpg
与wt
,但有2个不同的交替值hp
和3个不同的交替值cyl
:
predData = data.frame(wt=seq(1,5,length=60), hp=rep(c(200,300), 30), cyl=rep(c(4,6,8), 20))
predData = cbind(predData, predict(m1, newdata=predData, interval="confidence"))
请注意预测会如何跳跃,因为hp
和cyl
会因wt
的每个连续值而发生变化:
plot(predData$wt, predData$fit, type="l")
lines(predData$wt, predData$lwr, type="l", col="red")
lines(predData$wt, predData$upr, type="l", col="red")
但是,当我们保持hp
和cyl
不变时,我们会对mpg
与wt
进行直线预测:
predData2 = data.frame(wt=seq(1,5,length=60), hp=rep(300,60), cyl=rep(6, 60))
predData2 = cbind(predData2, predict(m1, newdata=predData2, interval="confidence"))
plot(predData2$wt, predData2$fit, type="l")
lines(predData2$wt, predData2$lwr, type="l", col="red")
lines(predData2$wt, predData2$upr, type="l", col="red")
除了单行外,您还可以为另一个变量的多个值绘制预测的mpg与wt行。下面是一个示例,我们为我们用于创建cyl
的{{1}}的每个值绘制一条线。使用predData
会更容易,所以我使用了该包。使用线条作为置信区间会使得图表难以理解,所以我用填充法显示了CI:
ggplot2