[英]How to plot a confidence interval in R
So I need to graph a confidence interval for a prediction I ran. 所以我需要绘制我运行的预测的置信区间。 I can run the prediction, but when I go to graph the prediction I get a line through all of my data points as opposed to getting the actual 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")
These line functions are supposed to graph my CI, but instead I get the following graph 这些行函数应该用于绘制我的CI,但是我得到了下面的图表
Here's an example of what's going wrong, using the built-in mtcars
data frame: 以下是使用内置
mtcars
数据框出现问题的示例:
# Regression model
m1 = lm(mpg ~ wt + hp + cyl, data=mtcars)
Now let's get predictions of mpg
vs. wt
, but with 2 different alternating values of hp
and 3 different alternating values of cyl
: 现在让我们得到
mpg
与wt
预测,但是有两个不同的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"))
Note how the prediction jumps around, because hp
and cyl
change for each successive value of wt
: 注意预测是如何跳跃的,因为
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")
But when we keep hp
and cyl
fixed, we get a straight line prediction for mpg
vs. wt
: 但是当我们保持
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")
Instead of a single line, you can also plot predicted mpg vs. wt lines for several values of another variable. 您还可以为另一个变量的多个值绘制预测的mpg与wt行,而不是单行。 Below is an example where we plot a line for each value of
cyl
that we used to create predData
. 下面是一个示例,我们为用于创建
predData
的cyl
每个值绘制一条线。 This is easier with ggplot2
so I've used that package. 使用
ggplot2
这很容易,所以我使用了这个包。 Using lines for the confidence intervals would make the plot difficult to understand, so I've shown the CI with a fill instead: 使用线条作为置信区间会使得图表难以理解,因此我已经使用填充显示了CI:
library(ggplot2)
ggplot(subset(predData, hp==200), aes(wt, fit, fill=factor(cyl), colour=factor(cyl))) +
geom_ribbon(aes(ymin=lwr, max=upr), alpha=0.2, colour=NA) +
geom_line() +
labs(x="Weight", y="Predicted MPG", colour="Cylinders", fill="Cylinders") +
theme_bw()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.