How to visulaize linear model prediction in ggplot along with confidence interval?

2.4k views Asked by At

Suppose I'm using my_df to fit a linear model. After getting the estimates I want to see how well model1 can predict n case of another dataset. 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.

#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

There are 1 answers

4
alistaire On BEST ANSWER

If you flip your axes, you can use geom_ribbon. You can get the necessary numbers from predict if you tell it you want a confidence interval, too:

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.