Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Logistic Regression

    Here is my analytic sample and variables code:

    *1. Variables for analysis*
    *Maternal Age: 5 year age groups*
    tab mager, mis
    sum mager, detail
    gen mager_cat=1 if mager<20
    replace mager_cat=2 if mager>=20&mager<=24
    replace mager_cat=3 if mager>=25&mager<=29
    replace mager_cat=4 if mager>=30&mager<=34
    replace mager_cat=5 if mager>=35&mager<=39
    replace mager_cat=6 if mager>=40&!missing(mager)
    label var mager_cat "Mother's Age Categories"
    label val mager_cat mager_c
    label define mager_cat 1"1<20" 2"2:20-24" 3"3:25-29" 4"4:30-34" 5"5:35-39" 6"6:40+"

    *Mother's Nativity*
    tab mbstate_rec
    gen nativity=1 if mbstate_rec==1
    replace nativity=2 if mbstate_rec==2
    replace nativity=3 if mbstate_rec==3
    label var nativity "Citizenship"
    label val nativity nativity
    label define nativity 1"1:US Born" 2"2:Foreign Born" 3"3:Unknown"
    tab nativity
    drop if nativity==.

    *Black ethnicity groups*
    tab mrace15 mrace6
    tab mrace31 mrace6
    gen matrace=1 if (mrace6==2&!missing(mrace6)|mrace15==2&!missing(mr ace15)|mrace31==2&!missing(31))
    tab matrace

    *Marital Status*
    tab dmar, mis
    gen married=dmar
    recode married 2=0
    label var married "Marital status"
    label val married yesno
    label define yesno 1"1:Yes" 0"0:No"
    tab married, mis


    *Maternal Education: < HS, HS, Some college, College+*
    tab meduc, mis
    replace meduc=. if meduc==9
    gen meduc_cat=1 if meduc<=2
    replace meduc_cat=2 if meduc==3
    replace meduc_cat=3 if meduc==4
    replace meduc_cat=4 if meduc>=5&!missing(meduc)
    label var meduc_cat "Mother's Education Categories"
    label val meduc_cat meduc_cat
    label define meduc_cat 1"1:Less than high school" 2"2:High School" 3"3:Some college" 4"4:College or more"
    tab mrstate meduc if f_meduc==1,mis
    tab dob_yy meduc if f_meduc==1, mis row

    *Source of Payment: pay_rec*
    tab pay_rec
    rename pay pay_og
    gen pay=pay_rec
    replace pay=. if pay==9
    label var pay "Payment"
    label val pay pay
    label define pay 1"1:Medicaid" 2"2:Private Insurance" 3"3:Self-pay" 4"4:Other"
    tab dob_yy pay if f_meduc==1, mis
    tab mrstate pay if f_meduc==1, mis

    *WIC: Yes/No*
    gen wicuse=wic
    replace wicuse="2" if wicuse=="U"
    replace wicuse="1" if wicuse=="Y"
    replace wicuse="0" if wicuse=="N"
    destring wicuse, replace
    replace wicuse=. if wicuse==2
    label var wicuse "Wic Use"
    label val wicuse yesno
    tab dob_yy wicuse if f_meduc==1, mis row

    *Prenatal care: 1st trimester, 2nd tri, 3rd or No PNC; precare5 with 5 to missing*
    replace precare5=. if precare5==5
    gen precare_cat=1 if precare5==1c
    replace precare_cat=2 if precare5>=2&precare5<=3
    replace precare_cat=3 if precare5==4
    label var precare_cat "Prenatal Care Initiation"
    label val precare_cat precare_cat
    label define precare_cat 1"1:Early" 2"2:Late" 3"3:None"
    tab dob_yy precare_cat if f_meduc==1, mis row

    /*Prenatal care any - Y/N*
    gen pnc=1 if precare5>=1&precare5<=3
    replace pnc=0 if precare5==4
    label var pnc "Prenatal care"
    label val pnc yesno
    tab precare5, mis
    tab pnc,mis*/

    *Prepregnancy BMI: Underweight, Normal, Overweight, Obese*
    replace bmi_r=. if bmi_r==9
    gen bmi_cat4=1 if bmi_r==1
    replace bmi_cat4=2 if bmi_r==2
    replace bmi_cat4=3 if bmi_r==3
    replace bmi_cat4=4 if bmi_r>=4&!missing(bmi_r)
    label var bmi_cat4 "BMI Categories"
    label val bmi_cat4 bmi_cat4
    label define bmi_cat4 1"1:Underweight" 2"2:Normal" 3"3:Overweight" 4"4:Obese"
    tab dob_yy bmi_cat4 if f_meduc==1, mis row

    *Pre-pregnancy smoking: Yes/No -cig_0*
    tab cig_0, mis
    replace cig_0=. if cig_0==99
    gen cigprepreg_cat2=0 if cig_0==0
    replace cigprepreg_cat2=1 if cig_0>=1&!missing(cig_0)
    label var cigprepreg_cat2 "Cigarette Use Prepregnancy"
    label val cigprepreg_cat2 yesno
    tab dob_yy cigprepreg_cat2 if f_meduc==1, mis row

    *Pregnancy smoking: Any smoking during pregnancy - cig_rec*
    gen ciguse=cig_rec
    replace ciguse="2" if ciguse=="U"
    replace ciguse="1" if ciguse=="Y"
    replace ciguse="0" if ciguse=="N"
    destring ciguse, replace
    replace ciguse=. if ciguse==2
    label var ciguse "Cigarette Use During Pregrnancy"
    label val ciguse ciguse
    label define ciguse 0"0:No" 1"1:Yes"
    tab dob_yy ciguse if f_meduc==1, mis row

    *Combined pregnancy smoking status:
    tab cigprepreg_cat2 ciguse, mis
    gen ciguse_tot=0 if cigprepreg_cat2==0&ciguse==0
    replace ciguse_tot=1 if cigprepreg_cat2==1&ciguse==0
    replace ciguse_tot=2 if cigprepreg_cat2==0&ciguse==1
    replace ciguse_tot=3 if cigprepreg_cat2==1&ciguse==1
    tab ciguse_tot, mis
    label var ciguse_tot "Cig use before/during preg"
    label val ciguse_tot cigusetot
    label define cigusetot 0"0:Never" 1"1:before only" 2"2:during only" 3"3:before/during"

    gen ciguse_tot3=ciguse_tot
    recode ciguse_tot3 3=2
    label var ciguse_tot3 "Cig use before/during preg"
    label val ciguse_tot3 cigusetot3
    label define cigusetot3 0"0:Never" 1"1:before only" 2"2:during and/or before"

    /*Pre-diabetes Y/N*
    tab rf_pdiab,mis
    gen prediab=1 if rf_pdiab=="Y"
    replace prediab=0 if rf_pdiab=="N"
    replace prediab=. if rf_pdiab=="U"&!missing(rf_pdiab)
    label var prediab "Pre-diabetes"
    label val prediab yesno
    tab rf_pdiab prediab,mis
    tab dob_yy prediab if f_meduc==1, mis row
    */
    /*Gestational diabetes Y/N*
    tab rf_gdiab,mis
    gen gestdiab=1 if rf_gdiab=="Y"
    replace gestdiab=0 if rf_gdiab=="N"
    replace gestdiab=. if rf_gdiab=="U"&!missing(rf_gdiab)
    label var gestdiab "Gestational diabetes"
    label val gestdiab yesno
    tab rf_gdiab gestdiab,mis
    tab dob_yy gestdiab if f_meduc==1, mis row
    */
    /*Diabetes*
    gen diabetes=1 if (prediab==1|gestdiab==1)
    replace diabetes=0 if (prediab==0&gestdiab==0)
    replace diabetes=. if (prediab==.&gestdiab==.)
    label var diabetes "Diabetes"
    label val diabetes yesno
    tab diabetes,mis
    */

    *Diabetes categories*
    tab prediab gestdiab, missing
    gen diabetescat=1 if prediab==0&gestdiab==0
    replace diabetescat=2 if prediab==1&gestdiab==0
    replace diabetescat=3 if prediab==0&gestdiab==1
    tab diabetescat prediab, missing
    tab diabetescat gestdiab, missing
    label var diabetescat "Diabetes categories"
    label val diabetescat diabcat
    label define diabcat 1"1:None" 2"2:Prediab" 3"3:GDM"

    *Hypertension: gestational, pre-pregnancy, none (use rf_phype, rf_ghype, rf_ehype)*
    tab rf_phype rf_ghype, mis
    tab rf_ehype rf_ghype, mis col
    tab rf_ehype rf_phype, mis col
    gen hyperten1=0 if rf_ghype=="N"&rf_phype=="N"
    replace hyperten1=1 if rf_phype=="Y"&rf_ghype=="N"
    replace hyperten1=2 if rf_phype=="N"&rf_ghype=="Y"
    tab hyperten1, mis
    tab hyperten1 rf_ehype, mis
    gen hyperten=hyperten1
    replace hyperten=2 if hyperten1==0&rf_ehype=="Y"
    tab hyperten, mis
    label var hyperten "Hypertension"
    label val hyperten hyperten
    label define hyperten 0"0:None" 1"1:Prepregnancy" 2"2:Gestational"

    *Birth order: 1,2, 3, 4+; 1 or 2+*
    replace lbo_rec=. if lbo_rec==9
    gen lbo_cat=1 if lbo_rec==1
    replace lbo_cat=2 if lbo_rec==2
    replace lbo_cat=3 if lbo_rec==3
    replace lbo_cat=4 if lbo_rec>=4&!missing(lbo_rec)
    label var lbo_cat "Birth Order 1, 2, 3, 4+"
    label val lbo_cat lbo_cat
    label define lbo_cat 1"1:1" 2"2:2" 3"3:3" 4"4:4+"
    tab dob_yy lbo_rec, mis row

    *Gestational week at birth*
    tab oegest_comb, mis
    replace oegest_comb=. if oegest_comb==99
    gen gestage=1 if oegest_comb<37
    replace gestage=2 if (oegest_comb==37|oegest_comb==38)
    replace gestage=3 if (oegest_comb==39|oegest_comb==40)
    replace gestage=4 if oegest_comb==41
    replace gestage=5 if oegest_comb>=42&!missing(oegest_comb)
    tab oegest_comb gestage,mis
    label var gestage "Gest Age Cat"
    label val gestage gestage
    label define gestage 1"1:<37" 2"2:37-38" 3"3: 39-40" 4"4:41" 5"5:42+"

    *Infant Sex: M/F*
    gen infsex=sex
    replace infsex="0" if infsex=="M"
    replace infsex="1" if infsex=="F"
    destring infsex, replace
    label var infsex "Infant Sex"
    label val infsex infsex
    label define infsex 0"0:Male" 1"1:Female"


    *Birthweight*
    tab dbwt
    recode dbwt 9999=.
    gen bw_cat=1 if dbwt<1500
    replace bw_cat=2 if dbwt>=1500&dbwt<2500
    replace bw_cat=3 if dbwt>=2500&dbwt<4000
    replace bw_cat=4 if dbwt>=4000&!missing(dbwt)
    label var bw_cat "Birthweight Cat"
    label val bw_cat bw_cat
    label define bw_cat 1"1:<1500" 2"2:1500-2499" 3"3:2500-3999" 4"4:4000+"

    **Breastfeeding on discharge*
    generate bfed_new=1 if bfed=="Y"
    replace bfed_new=0 if bfed=="N"
    replace bfed_new=. if bfed=="U"&!missing(bfed)
    label var bfed_new "Breastfeeding on discharge"
    label val bfed_new yesno
    tab bfed_new,mis
    tab dob_yy bfed_new if f_meduc==1, mis row
    tab dob_yy bfed_new if f_meduc==1& ostate!="CA", mis row
    tab dob_yy bfed_new if f_meduc==1& ostate!="MI", mis row
    *dropped California and Michigan because they did not collect breastfeeding data for years analyzed*

    /*Admission to NICU*
    tab ab_nicu,mis
    gen adm_nicu=1 if ab_nicu=="Y"
    replace adm_nicu=0 if ab_nicu=="N"
    replace adm_nicu=. if ab_nicu=="U"&!missing(ab_nicu)
    label var adm_nicu "Admission to NICU"
    label val adm_nicu yesno
    tab ab_nicu adm_nicu,mis
    tab dob_yy adm_nicu if f_meduc==1, mis row

    *Infant transferred*
    tab itran,mis
    gen inftrans=1 if itran=="Y"
    replace inftrans=0 if itran=="N"
    replace inftrans=. if itran=="U"&!missing(itran)
    label var inftrans "Infant transferred"
    label val inftrans yesno
    tab itran inftrans,mis
    tab dob_yy inftrans if f_meduc==1, mis row

    tab bfed_new itran, mis
    */

    *NICU admission and/or infant transferred*
    tab ab_nicu itran, mis
    gen nicu=1 if ab_nicu=="Y"
    replace nicu=1 if itran=="Y"
    replace nicu=0 if (itran=="N"&ab_nicu=="N")|(ab_nicu=="U"&itran=="N" )|(ab_nicu=="N"&itran=="U")
    tab nicu, mis
    label var nicu "NICU Admission"
    label val nicu nicu
    label define nicu 0"0:No" 1"1:Yes"
    tab dob_yy nicu if f_meduc==1, mis row


    *2. Analytic sample: residents, Medicaid births, term births, all states reporting breastfeeding (excludes California/Michigan)*
    tab restatus
    drop if restatus==4

    tab pay
    keep if pay==1

    tab bfed_new, mis
    tab mrstate bfed_new, mis row
    tab ostate bfed_new, mis row
    drop if mrstate=="CA"
    drop if mrstate=="MI"

    Im trying to run this logistic regression:
    logit gestage i.hyperten i.dob_yy i.precare_cat i.wicuse ib2.mager_cat ib2.meduc_cat ib1.lbo_cat ib2.bmi_cat4 i.ciguse_tot3 i.infsex i.diabetescat if nativity==1, or

    But getting this error message:


    outcome does not vary; remember:
    0 = negative outcome,
    all other nonmissing values = positive outcome
    r(2000);

    please help

  • #2
    The error message you received is relatively direct: The -logit- command requires that your outcome variable be coded with two values, 0 and not-zero. It appears that you have tried to code your gestage variable into the values 1 through 5. This makes your original variable oegest_comb into an ordered categorical variable. You could analyze it with an ordinal or multinomial logit model, but not the binary logit model you are trying to use. Or, you could code it into a 0/1 binary variable and use -logit-. However, while I'm not experienced in the substance of your research area, I would not recommend that, as collapsing an outcome or explanatory variable discards information. There are some situations in which that's a good idea, but not typically. Some other statistical approach would likely be preferable.

    Beyond that, the code (syntax) you has various oddities. Here are two suggestions:

    1) There are shorter and less error-prone ways to change the categorization of a variable than what you have done with a series of -replace- commands. Among the possibilities would be the -recode- command. For example, your mager_cat variable might be created like this:
    Code:
    recode mager (min/20 = 1) (21/24 = 2) (25/29 = 3) (30/34 = 4) (35/39 = 5) (40/max = 6) (missing = .)
    However, again, I'm not recommending a categorization practice like this unless you have some good reason.

    2) /* and */ will make everything in between them be ignored by Stata. The fact that you have left those in at several places in your code is a strange thing to do if you are presenting example code, which makes me wonder whether you know what these character pairs do. See -help comment-

    Comment

    Working...
    X