# [R] Simulate phi-coefficient

David Duffy David.Duffy at qimr.edu.au
Mon Sep 12 02:34:20 CEST 2005

```> From: "Bliese, Paul D LTC USAMH" <paul.bliese at us.army.mil>
>
> Given a sample of zeros and ones, for example:
> > VECTOR1<-rep(c(1,0),c(15,10))
> How would I create a new sample (VECTOR2) also containing zeros and
> ones, in which the phi-coefficient between the two sample vectors was
> drawn from a population with a known phi-coefficient value?
>
> I know there are ways to do this with normally distributed numbers (for
> example the mvrnorm function in MASS), but am stumped when dealing with
> dichotomous variables.
>
> Paul

One way is to sample from the 2x2 table with the specified means and pearson
correlation (phi):

for a fourfold table, a b
c d
with marginal proportions p1 and p2
cov <- phi * sqrt(p1*(1-p1)*p2*(1-p2))
a <- p1*p2 + cov
b <- p1*(1-p2) - cov
c <- (1-p1)*p2 - cov
d <- (1-p1)*(1-p2) + cov
expand.grid(0:1,0:1)[sample(1:4, size=25, replace=TRUE, prob=c(a,b,c,d)),]

David.

| David Duffy (MBBS PhD)                                         ,-_|\
| email: davidD at qimr.edu.au  ph: INT+61+7+3362-0217 fax: -0101  /     *
| Epidemiology Unit, Queensland Institute of Medical Research   \_,-._/
| 300 Herston Rd, Brisbane, Queensland 4029, Australia  GPG 4D0B994A v

```