如何在ggplot中建立线性模型预测以及置信区间?

时间:2016-12-18 03:16:47

标签: r ggplot2 stat

假设我使用my_df来拟合线性模型。获得估算后,我想看看model1能够预测另一个数据集的情况。我不确定以下是否是可视化的正确方法以及如何根据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?!!!

Noisy Ribbon

1 个答案:

答案 0 :(得分:3)

如果您翻转轴,可以使用geom_ribbon。如果你告诉它你也想要一个置信区间,你可以从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

如果你真的想要你的轴,请添加coord_flip