I adapted the following code from here. I am trying to simulate power for a proportion test. I keep getting the following error message:
Error in prop.test(treat, ctrl) : 'x' and 'n' must have the same length
I don't quite understand why, since I do know that both my x
and n
are the same length. May I please ask for someone's help? Thank you!
library('paramtest')
library('pwr')
df <- data.frame(trt = rep(c("smokers","patients"), each = 4),
values= c(83, 90, 129, 70, 86, 93, 136, 82))
p_func_boot <- function(data, indices) {
sample_data <- data[indices, ]
treat <- sample_data[sample_data$trt == 'smokers', 'values']
ctrl <- sample_data[sample_data$trt == 'patients', 'values']
ptest <- prop.test(treat,ctrl)
stat <- ptest$statistic
p <- ptest$p.value
return(c(X_squared =stat, p=p, sig=(p < .05)))
}
power_proptest_boot <- run_test(p_func_boot, n.iter= 100, output='data.frame', boot=TRUE,
bootParams=list(data=df))