[英]How to visulaize linear model prediction in ggplot along with confidence interval?
Suppose I'm using my_df to fit a linear model. 假设我正在使用my_df拟合线性模型。 After getting the estimates I want to see how well model1 can predict n case of another dataset.
得到估计后,我想看看model1可以很好地预测另一个数据集的n个案例。 I'm not sure if the following is the right way to visualize that and how I can add confidence interval bonds based on model1 estimates.
我不确定以下是否是可视化此方法的正确方法,以及如何根据model1估算值添加置信区间键。
#Make up a dataframe and perform a linear model
res <- c(2,3.1,4.5,5.1,6.5,7.1,8.5,9.11,10.1,11.8,12.3)
predictor1 <- c(4.2, 5.3, 5.68,6.5, 7.77,8.5,9.5, 10.18,11.64,12.15,14.19)
predictor2 <- c(3.1, 5.2, 6.3,7.1, 9.7,11.5,12.99, 14.5 ,15.5,16.41,17.6)
my_df <- data.frame(res, predictor1, predictor2)
model1<- lm(res~predictor1+predictor2,data = my_df)
#____Another dataset
response<- c(12.5,13.5,14.65,16.1,16.5,17.22,18.54,21.31,23.61,25.58,26.43)
x1<- c(14.21, 15.13, 16.25,16.5, 17.37,18.51,19.35, 22.18,23.64,25.12,26.19)
x2<- c(13.11, 15.22, 16.23,17.41, 18.72,21.5,22.99, 24.35 ,25.15,26.21,28.5)
observeddata <- data.frame(y, x1, x2)
#I need to check how model1 predicts the response based on the two new predictors(x1&x2) using the estimates of model1
observeddata$prediction<- predict(model1, newdata = observeddata)
#is this a good way to visulize how good or bad model1 works in case of second dataset?
ggplot(data=observeddata, aes(x=prediction,y=response))+ geom_point()
#How can I add confidence interval in this plot?
#based on what @alistaire suggested I get something like this in case of my dataset, am I doing it right?!!!
If you flip your axes, you can use geom_ribbon
. 如果翻转轴,则可以使用
geom_ribbon
。 You can get the necessary numbers from predict
if you tell it you want a confidence interval, too: 如果您告诉它也想要一个置信区间,则可以从
predict
获得必要的数字:
library(ggplot2)
my_df <- data.frame(
res = c(2,3.1,4.5,5.1,6.5,7.1,8.5,9.11,10.1,11.8,12.3),
predictor1 = c(4.2, 5.3, 5.68,6.5, 7.77,8.5,9.5, 10.18,11.64,12.15,14.19),
predictor2 = c(3.1, 5.2, 6.3,7.1, 9.7,11.5,12.99, 14.5 ,15.5,16.41,17.6))
model1<- lm(res ~ predictor1 + predictor2, data = my_df)
#____Another dataset
observeddata <- data.frame(
response = c(12.5,13.5,14.65,16.1,16.5,17.22,18.54,21.31,23.61,25.58,26.43),
predictor1 = c(14.21, 15.13, 16.25,16.5, 17.37,18.51,19.35, 22.18,23.64,25.12,26.19),
predictor2 = c(13.11, 15.22, 16.23,17.41, 18.72,21.5,22.99, 24.35 ,25.15,26.21,28.5))
# adds 3 columns (fit, lwr, upr), so use `cbind` instead of just adding a column
observeddata <- cbind(observeddata,
predict(model1, newdata = observeddata, interval = 'confidence'))
# add extra aesthetics for geom_ribbon
ggplot(data = observeddata, aes(x = response, y = fit, ymin = lwr, ymax = upr)) +
geom_point() +
geom_ribbon(alpha = .3) # set opacity so points are visible
If you really want your axes as they were, add coord_flip
. 如果您确实想要自己的轴,请添加
coord_flip
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.