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

问题描述 投票:3回答:1

我有一个2 x 2阶乘数据集,已使用mean_cl_boot函数为其绘制了置信区间。我想使用适当的函数在R中进行计算。我该怎么办?

我的数据集的一个样本为:

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)
    )    

我知道有几种方法可以从图中提取CI点,但是我确实[[not想要这样做。我想使用计算这个的函数。

r ggplot2 confidence-interval
1个回答
2
投票
mean_cl_boot建立在Hmisc::smean.cl.boot()上。

如果要为所有值(无论级别如何)都计算自举CI,则smean.cl.boot(df$repro)应该这样做。

这是在基数R中执行split-apply-combine的方式:

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

结果:

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

如果要在tidyverse中执行此操作:

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

(这并不完全令人满意:可能有一种更清洁的方法来做]
© www.soinside.com 2019 - 2024. All rights reserved.