2016-05-23 10 views
0

私は、geom_smooth関数を使用して、信頼区間と共に点束の推定をloessのプロットに使用します。スムース信頼区間の計算方法をカスタマイズする

ここで、信頼境界を計算する方法を変更する必要があります(つまり、ぼかしバンドの形状を変更する必要があります)。 geom_smoothでこれを行う方法はありますか?

または、どうすればggplot2でエミュレートできますか?どのように私はそのようなぼかしバンドですか?

答えて

0

geom_smoothのオプションの1つではないものをプロットする必要がある場合は、自分でモデルを手動で手動で取り付けてください。

あなたはどのような方法が必要かは述べていません。 しかし、ここでは黄土を家族の対称性に合わせて標準誤差を計算する例を示します。

d <- data.frame(x = rnorm(100), y = rnorm(100)) 

# The original plot using the default loess method 
p <- ggplot(d, aes(x, y)) + geom_smooth(method = 'loess', se = TRUE) 

# Fit loess model with family = 'symmetric' 
# Replace the next 2 lines with whatever different method you need 
loess_smooth <- loess(d$x ~ d$y, family = 'symmetric') 

# Predict the model over the range of data you are interested in. 
loess_pred <- predict(loess_smooth, 
         newdata = seq(min(d$x), max(d$x), length.out = 1000), 
         se = TRUE) 

loess.df <- data.frame(fit = loess_pred$fit, 
         x = seq(min(d$x), max(d$x), length.out = 1000), 
         upper = loess_pred$fit + loess_pred$se.fit, 
         lower = loess_pred$fit - loess_pred$se.fit) 

# plot to compare 
p + 
    geom_ribbon(data = loess.df, aes(x = x, y = fit, ymax = upper, ymin = lower), alpha = 0.6) + 
    geom_line(data = loess.df, aes(x = x, y = fit)) 

Two loess fits

関連する問題