簡體   English   中英

如何對ggplot中的線性模型預測以及置信區間進行可視化?

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

假設我正在使用my_df擬合線性模型。 得到估計后,我想看看model1可以很好地預測另一個數據集的n個案例。 我不確定以下是否是可視化此方法的正確方法,以及如何根據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?!!!

嘈雜的絲帶

如果翻轉軸,則可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM