Reputation: 2429
I have a 2 x 2 factorial data set for which I have plotted the confidence intervals using mean_cl_boot
function. I want to calculate this in R using the appropriate function. 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. I want to use the function that calculates this.
Upvotes: 4
Views: 627
Reputation: 226087
mean_cl_boot
is built on 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.
This is how you would do the split-apply-combine in 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:
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)
Upvotes: 2