简体   繁体   English

如何使用 ggplot2 中使用的 mean_CI_boot 计算自举置信区间?

[英]How to calculate bootstrapped confidence interval using the mean_CI_boot used in ggplot2?

I have a 2 x 2 factorial data set for which I have plotted the confidence intervals using mean_cl_boot function.我有一个 2 x 2 阶乘数据集,我已经使用mean_cl_boot函数为其绘制了置信区间。 I want to calculate this in R using the appropriate function.我想使用适当的函数在 R 中计算它。 How can I do that?我怎样才能做到这一点?

A sample of my data set is as:我的数据集示例如下:

df <- data.frame(
      fertilizer = c("N","N","N","N","N","N","N","N","N","N","N","N","P","P","P","P","P","P","P","P","P","P","P","P","N","N","N","N","N","N","N","N","N","N","N","N","P","P","P","P","P","P","P","P","P","P","P","P"), 
      level = c("low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","high","low","low","high","low"), 
      repro = c(0,90,2,4,0,80,1,90,2,33,56,0,99,100,66,80,1,0,2,33,0,0,1,2,90,5,2,2,5,8,0,1,90,2,4,66,0,0,0,0,1,2,90,5,2,5,8,55)
    )    

I know there are ways of extracting the CI points from the graph, but I do not want to do this.我知道有一些方法可以从图中提取 CI 点,但我不想这样做。 I want to use the function that calculates this.我想使用计算这个的函数。

mean_cl_boot is built on Hmisc::smean.cl.boot() . mean_cl_boot建立在Hmisc::smean.cl.boot()

If you want to compute the bootstrapped CI for all of the values (regardless of level), smean.cl.boot(df$repro) should do it.如果您想为所有值(无论级别如何)计算自举 CI, smean.cl.boot(df$repro)应该这样做。

This is how you would do the split-apply-combine in base R:这就是在 base R 中执行拆分-应用-组合的方法:

library(Hmisc)
ss <- with(df, split(df, list(fertilizer,level)))
bb <- lapply(ss, function(x) smean.cl.boot(x$repro))
do.call(rbind,bb)

Results:结果:

           Mean     Lower    Upper
N.high 19.00000  5.747917 36.58750
P.high 26.09091  8.631818 47.27273
N.low  33.75000 12.416667 58.26042
P.low  20.38462  1.615385 42.69423

If you want to do this in tidyverse:如果你想在 tidyverse 中这样做:

library(tidyverse)
(df 
    %>% group_split(fertilizer,level) 
    %>% map_dfr(~as_tibble(rbind(smean.cl.boot(.[["repro"]]))))

(this is not entirely satisfactory: there's probably a cleaner way to do it) (这并不完全令人满意:可能有更简洁的方法来做到这一点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ggplot2中使用不同的置信区间mean_cl_boot - Use a different confidence interval in ggplot2 mean_cl_boot 如何从ggplot2 R中的自举模型绘制具有估计均值和置信区间的原始数据点? - How to plot raw datapoints with estimated means and confidence interval from bootstrapped model in ggplot2 R? ggplot2 - 将平均值添加到我的置信区间 - ggplot2 - adding the mean to my Confidence Interval 手动使用 ggplot2 的置信区间 - Confidence interval using ggplot2 manually 使用 'boot' 计算 R 中的自举平均值 - Using 'boot' to calculate bootstrapped mean in R ggplot2:具有均值/95% 置信区间线的密度图 - ggplot2: Density plot with mean / 95% confidence interval line 在 ggplot2 中,使用现有 CI 变量指定 geom_smooth(或任何趋势线)周围的置信区间 (95% CI) - In ggplot2, specify a confidence interval (95% CI) around geom_smooth (or any trend line) using existing CI variables 如何在不使用 confint 的情况下计算 R 中均值的置信区间 - How can I calculate confidence interval for a mean in R not using confint 如何绘制置信区间以更好地展示ggplot2的置信区间? - How to draw confidence bounds to better demonstrate the confidence interval on ggplot2? 使用stat_quantile时ggplot2中的置信区间带? - Confidence interval bands in ggplot2 when using stat_quantile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM