2016-05-23 38 views
0

我使用geom_smooth函数来绘制一组点的loess估计值以及置信区间。自定义平滑置信区间的计算方法

现在我需要改变计算置信界限的方法(即我需要改变模糊带的形状)。在geom_smooth有没有办法做到这一点?

或者,我该如何模仿ggplot2?我怎么能这样模糊乐队?

回答

0

如果您需要绘制一个不属于geom_smooth选项的东西,那么最好的方法是自己手动调整模型。

你还没有说你需要什么方法。 但是,这里是一个将黄土与家族对称拟合并计算其标准误差的例子。

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