2016-12-18 37 views
1

my_dfを使用して線形モデルに適合しているとします。見積もりを取得した後、model1が別のデータセットのn件をどれだけ予測できるかを見たいと思う。私は次のことがそれを視覚化する正しい方法であるかどうか、そしてモデル1の見積もりに基づいて信頼区間債券をどのように追加できるかはわかりません。ggplotの線形モデル予測を信頼区間とともに視覚化する方法は?

#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

答えて

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を追加します。

+0

そして、あなたのデータのほとんどはダークゾーンの外で発生した場合、あなたは右、モデルがうまく予測するものではないことを推測することができますか?これは、モデルの予測パフォーマンスを表現する典型的な方法ですか? – Jack

+0

実際にはありません。それが良いモデルかどうかを知るためには、最終的にテストエラーに気をつけます。実際のものと予測されたものをプロットしているので、そのグラフをy = xの線に沿って整列させたいとします。 R^2とp値は、残差プロットと同様に正しい方向に向けるのに役立ちます。 – alistaire

+0

ありがとうございました。私はいつもうまくいっているモデルを、フィットのVの散布図を持つと考えました。 1:1ラインに非常に近いことが観察された。明らかに、モデルの予測力を評価する適切な方法ではありません。 – Jack

関連する問題