0

I'm trying to create a simulation for a proportion test to see how often I would get a significant results (i.e. p/value < 0.05). I'm assuming unequal population sizes, so for example

test <- rbinom(100, 5000, 0.05)

ctrl <- rbinom(100, 10000, 0.04)

Here I have 100 simulated tests, with 5000 in the test population and 10000 in the control, and an actual difference in conversion rate (0.05 for test and 0.04 for control).

I now want to run a proportion test for each of these simulated tests and get the p value. So for the very first test I would do:

prop.test(c(test[1], ctrl[2]), c(5000, 10000))$p.value

and then I'm looping through this with a for loop to get the p value for each of the simulated tests.

I'm looking to do this in a more compact way. I found a way to do it for a 1-sample proportion test with Map here , but I'm stuck how to do it for a 2-sample test. It does seem like I should be able to do it in a single line, and just get a vector of p-values as the output.

I tried the following

Map(prop.test, c(test,ctrl), c(5000, 10000), alternative="two.sided", correct = F)

But it's giving me 200 1-sample proportion tests, in stead of 100 2-sample proportion tests...any ideas what I can do to fix it?

Ideally I woul get a vector of length 100 with p.values so I can just check what portion is under 5% and that would be the tests power..

Community
  • 1
  • 1
L Xandor
  • 1,659
  • 4
  • 24
  • 48
  • Try `sapply(1:100, function(i) {prop.test(c(rbinom(1, 5000, 0.05), rbinom(1, 1000, 0.04)), c(5000, 1000))$p.value})`. Do you have 500 or 5000 and 1000 or 10000? – ekstroem Mar 14 '16 at 21:17
  • Thanks!! Sorry, was just typing example numbers and must have dropped a 0 in a few places. That worked perfectly though - thanks – L Xandor Mar 14 '16 at 21:35

1 Answers1

1

if i get you correct, you want each p.value of each prop. test. did you try this? prop.test(test,ctrl)[[4]]