0

I have a population 2x2 frequency table, with specific values: 20, 37, 37, 20. I need to generate N number of samples from this population (for simulation purposes). How can I do it in R?

Tanja
  • 1
  • 2
    So if your category labels are $A,B$ on the first dimension and $c,d$ on the second dimension then you have 20 $Ac$'s, 37 $Ad$'s, 37 $Bc$'s and 20 $Bd$'s -- do I have that right? Are you sampling with replacement or without replacement? Are any of the margins fixed? – Glen_b Jan 08 '16 at 10:04
  • 1
    This strikes me as possibly a statistical question, not just a coding one, so I think it might well be on-topic for CrossValidated. But at the moment I do not think the question is clear, because it lacks the clarifications that @Glen_b suggests. – Silverfish Jan 08 '16 at 12:43
  • 20 $Ac$'s, 37 $Ad$'s, 37 $Bc$'s and 20 $Bd$'s -- do I have that right? Yes, this is correct. Sampling with replacement, margins not fixed. Thank you very much for your help. – Tanja Jan 08 '16 at 18:13
  • I basically need to perform Monte Carlo simulation study. It has to do with simulation from multinomial distribution. – Tanja Jan 08 '16 at 18:16

1 Answers1

1

Try this. In the example, the integers represent cell 1, 2, 3, and 4 of the 2x2 table. As you can see the relative frequencies closely resemble those in your 20, 37, 37, 20 table.

probs<-c(20, 37, 37, 20)  
N<-1000    #sample size
mysample<-sample(x=c(1,2,3,4), size=N, replace = TRUE, prob = probs/sum(probs))
table(mysample)/N

#Run Again for 100,000 samples
N<-100000
mysample<-sample(x=c(1,2,3,4), size=N, replace = TRUE, prob = probs/sum(probs))
#The relative probabilities should be similar to those in the original table
table(mysample)/N
StatsStudent
  • 1,384
  • 2
  • 10
  • 28