Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: Simulation for cluster with unequal size

 From "Raja, Dr. Edwin Amalraj" <[email protected]> To "[email protected]" <[email protected]> Subject st: Simulation for cluster with unequal size Date Sun, 6 Apr 2014 15:34:44 +0000

```Dear Statslist,

I am using stata ver 13 MP. I am new to both stata and simulation programming.

My objective of the study is (i) to find out  the association between maternal BMI (categorical)  and Pre-eclampsia (binary) and also (ii) to find out odds of pre-eclampsia for varying prevalence of maternal MBI.  I am planning to do the second part of the objective through simulation.   Use Multilevel logistic regression(both xtlogit  & xtmelogit) to analyse the data.

The dataset has come from maternity registry.  124279 pregnancy details (offsprings) have come from 109592 women.  87% (95638) women have one pregnancy,  12% (13242) women have 2 pregnancy details, 0.6% (691 women) have 3 pregnancies and 2% (21) with 4 pregnancies details.   I could create this through simulation code.

I have got overall distribution of Maternal BMI and could get the same proportion in the simulated data.

I have problem with parity.   The parity ranges from 0 to 4 in the data, 0 representing primipara.

The distribution of pregno  and parity is as follows.  I could not get the data with following distribution for pregnancy and parity  in the simulated data

parity
pregno  |     0                1                 2                3                   4 |     Total
----  -------+-------------------------------------------------------+----------
1 |    55,875     34,933     13,145      3,917      1,722 |   109,592
2 |         0            9,274      3,233        992             455 |    13,954
3 |         0               0              455         166               91 |       712
4 |         0                0                0            9                   12 |        21
-- ---------+----------------------------------------------------+----------
Total |    55,875     44,207     16,833      5,084      2,280 |   124,279

(i)How  to simulate unequal number of pregnancies for each women

If you need any further details I am happy to provide.

Regards
Amalraj

***************  Stata code
clear
clear all
set mem 1g
set obs 109592 //create observations
gen id=_n //Create ID numbers

gen rand1 = uniform()
gen preg= cond(rand1<.87,0, cond(rand1<.993,1, cond(rand1<.998,2,3)))       // generate  offsprings
expand preg+1

sort id
by id: gen pregno=_n-1

gen rand2 = uniform()
gen pari= cond(rand2<.51,0, cond(rand2<.82,1, cond(rand2<.94,2, cond(rand2<.98,3,4))))    //  to generate parity number

gen pari1=-1.181 if pari==1
gen pari2=-1.511 if pari==2
gen pari3=-1.529 if pari==3
gen pari4=-1.362 if pari==4

replace pari1=0 if pari1==.
replace pari2=0 if pari2==.
replace pari3=0 if pari3==.
replace pari4=0 if pari4==.

*      to generate random effect

*      to generate mbmi category
gen rand = uniform()
gen mbmic= cond(rand<.0290,0, cond(rand<.5217,1, cond(rand<.804,2,3))) // generate mbmi : discrete uniform random    variables, to have underwt 2.9%, Normal 49%, overwt 28.2 % & Obese 20%

*  xi i.mbmic, pre(cat_)           // have not use this command in the analysis

gen mbmic0=-.584 if mbmic==0
gen mbmic2=.647 if mbmic==2
gen mbmic3=1.284 if mbmic==3

replace mbmic0=0 if mbmic0==.
replace mbmic2=0 if mbmic2==.
replace mbmic3=0 if mbmic3==.

gen rf=rnormal(0, 2.15)

egen woman_id = group(id)
bys woman_id: ge tag = _n == 1
bys woman_id: replace rf = rf[1]
compress

bys woman_id: gen ei=rnormal(0, 3.29)
*replace ei=0 if preg==0

gen xb = -4.94 + mbmic0+mbmic2+mbmic3+pari1+pari2+pari3+pari4+ rf+ei
* sqrt(0.586 / (1 - 0.586) * _pi^2 / 3) * rnormal()

* Specify population model for log odds that Y=1

gen y = runiform() < invlogit(xb)
sort woman_id pregno
gen yy=.
bys woman_id: replace yy=1 if y[_n-1]==1 | yy==1
replace yy=0 if yy==.
tab y

tab yy y, m
tab y
tab mbmic
tab pari

xtset id pregno
xtdes
tab pregno
drop rand rand1

xtlogit y ib2.mbmic i.pari, i(id) or
xtmelogit y ib2.mbmic i.pari ||id:,

The University of Aberdeen is a charity registered in Scotland, No SC013683.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```