0

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

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Hmmm, so you _know_ that `x` and `n` have the same length, but the computer thinks otherwise. And the winner is....... :D Your `indices` vector on the second run is a random sample of integers between 1 and 8 _with replacement_ (eg. `6 2 2 8 1 2 8 4`). Thus the `sample_data` is likely to contain unequal rows of smokers and patients. Hence, unequal `x` and `n` in the `prop.test` command. But that command looks weird to me. Check the documentation carefully for this function. `x` must be a vector of counts of successes and `n` is the total number of trials. – Edward Apr 01 '20 at 03:49
  • Thank you for your thoughtful response. – user3426705 Apr 01 '20 at 16:24

0 Answers0