如何在R中绘制置信区间

时间:2016-12-18 23:53:42

标签: r plot confidence-interval

所以我需要绘制我运行的预测的置信区间。我可以运行预测,但是当我进行预测图时,我得到了一条直线来处理我的所有数据点,而不是获得实际的置信区间。

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​​,但我得到以下图表

plot

1 个答案:

答案 0 :(得分:3)

以下是使用内置mtcars数据框出现问题的示例:

# Regression model
m1 = lm(mpg ~ wt + hp + cyl, data=mtcars)

现在让我们预测mpgwt,但有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"))

请注意预测会如何跳跃,因为hpcyl会因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")

enter image description here

但是,当我们保持hpcyl不变时,我们会对mpgwt进行直线预测:

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")

enter image description here

除了单行外,您还可以为另一个变量的多个值绘制预测的mpg与wt行。下面是一个示例,我们为我们用于创建cyl的{​​{1}}的每个值绘制一条线。使用predData会更容易,所以我使用了该包。使用线条作为置信区间会使得图表难以理解,所以我用填充法显示了CI:

ggplot2

enter image description here