|
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: simulating data for logistic regression: translating R to Stata
From |
Michael McCulloch <[email protected]> |
To |
Statalist <[email protected]> |
Subject |
st: simulating data for logistic regression: translating R to Stata |
Date |
Sun, 1 Jun 2008 14:55:48 -0700 |
Hello,
I'm simulating data for logistic regression (code for variable
generation is below) , and am missing the last step, linking exposure
to disease. Disease will be a y/n variable. I want to translate R
code to Stata, which specifies a population model where
# Specify population model for log odds that Y=1
L <- .1*(comorb-2) + .045*(age-50) +
(log(cholesterol - 10)-5.2)*(-2*(treat=='a') +
3.5*(treat=='b')+2*(treat=='c'))
# Simulate binary y to have Prob(y=1) = 1/[1+exp(-L)]
y <- ifelse(runif(n) < plogis(L), 1, 0)
the log odds that Y=1 is given by:
Might someone suggest how I can write that equation in Stata?
Below is Stata code for variable generation, to be used by that above equation.
clear
set obs 1000 //create 1000 observations
gen id=_n //Create ID numbers
set seed 12358 //set random number seed for
reproducibility
gen treat= 1 + int(3*uniform()) //Generate treatment: discrete
uniform random variables, 1 2 or 3
gen comorb= 1 + int(4*uniform()) //Generate treatment:
discrete uniform random variables, 1,2,3,4
gen age=50+10 * invnorm(uniform())
gen chol = 200+25 * invnorm(uniform()) // create cholesterol
variable, with mean 200 and sd==25
gen wt = 150+20 * invnorm(uniform()) // create cholesterol
variable, with mean 200 and sd==25
gen sex= 1 + int(2*uniform()) //Generate sex: discrete uniform
random variables, 1 or 2
replace sex=0 if sex==2
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/