Announcement

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

  • Using logistic regression with loops

    Hi, I am using a loop to conduct logistic regression for several outcome variables that are binary. I run the code below but I am getting an error that says "outcome does not vary". What am I doing wrong?

    frame create results str32 Independentvariable float (OR STD_ERR Z PVALUE LB95 UB95)
    foreach v of varlist race married never_married grade collgrad south smsa c_city industry occupation {
    logistic `v' i.union
    matrix M = r(table)
    frame post results ("`v'") (M["b", "`v':union"]) (M["se", "`v':union"]) ///
    (M["z", "`v':union"]) (M["pvalue", "`v':union"]) (M["ll", "`v':union"]) ///
    (M["ul", "`v':union"])
    }

    frame change results
    format OR STD_ERR LB UB %5.3f
    format Z PVALUE %05.3f
    quietly compress
    list, noobs clean


    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int idcode byte(age race married never_married grade collgrad south smsa c_city industry occupation union) float wage byte hours float(ttl_exp tenure)
      1 37 2 0 0 12 0 0 1 0  5  6 1 11.739125 48 10.333334  5.333333
      2 37 2 0 0 12 0 0 1 1  4  5 1  6.400963 40 13.621795      5.25
      3 42 2 0 1 12 0 0 1 1  4  3 .  5.016723 40  17.73077      1.25
      4 43 1 1 0 17 1 0 1 0 11 13 1 9.0338125 42 13.211537      1.75
      6 42 1 1 0 12 0 0 1 0  4  6 0  8.083731 48 17.820513     17.75
      7 39 1 1 0 12 0 0 1 0 11  3 0 4.6296296 30  7.326923      2.25
      9 37 1 0 0 12 0 0 1 1  5  2 1 10.491142 40  19.04487        19
     12 40 1 1 0 18 1 0 1 0 11  2 0 17.206116 45 15.557693 14.166667
     13 40 1 1 0 14 0 0 1 0 11  3 0 13.083735  8     14.25       5.5
     14 40 1 1 0 15 0 0 1 0 11  1 0  7.745568 50  7.384615      2.25
     15 39 1 1 0 16 1 0 1 0 11  1 1 16.795479 16  15.76923  8.416667
     16 40 1 1 0 15 0 0 1 0 11  1 0  15.48309 40 16.384615 13.833333
     18 40 1 1 0 15 0 0 1 0  6  5 0  5.233495 40      7.75      7.75
     19 40 1 0 0 15 0 0 1 0 11  1 1 10.161026 40 14.211538  5.583333
     20 39 1 1 0 15 0 0 1 0 11  1 1  9.661837  4 15.858974 4.6666665
     22 41 1 1 0 15 0 0 1 0 11  1 0  9.057972 32  15.01282         3
     23 42 1 1 0 15 1 0 0 0 11  1 0   8.05153 45 15.346155  8.083333
     24 41 1 1 0 14 1 0 1 0 11  1 0 11.095006 24 16.467947  3.416667
     25 42 1 1 0 14 1 0 1 1 11  1 0  9.581316 40     17.75  5.416667
     36 37 1 0 1 12 0 0 1 0  8  4 .  4.180602 40 15.935898  .9166667
     39 44 1 0 0 16 1 0 1 0 11  1 0  9.790657 40  8.839744  7.916667
     44 41 1 1 0 18 1 0 1 0 12  1 1  28.45666 35 16.205128       3.5
     45 35 1 1 0 12 0 0 1 0  5  3 1  10.18518 38     17.25     17.25
     46 44 1 1 0 18 1 0 1 0 11  3 0  3.051529 25  5.544872  2.916667
     47 35 1 0 0 12 0 0 1 0  5  5 0  3.526568 40  8.737179       .75
     48 35 1 0 0 15 0 0 1 1  7  3 .  5.852843 40  5.557693  .3333333
     50 36 1 0 0 16 1 0 1 1 11  1 . 35.731617 45  9.423077  6.416667
     51 38 1 1 0 12 0 0 1 0 11  1 0 4.4283414 18  10.26923      3.25
     54 40 1 0 1 12 0 0 1 1 11  8 0  5.032206 55 10.397436       .75
     57 42 1 1 0 12 0 0 1 1  6  3 0  5.780996 40 12.846154      7.25
     62 38 1 1 0 10 0 0 0 0  6  3 0   5.11272 40  7.365385  5.916667
     63 44 1 0 0 15 1 0 1 1 11  1 1   25.1606 18 10.384615 2.0833333
     64 38 1 1 0 12 0 0 1 0 11  8 1  4.404185 30  7.384615  3.333333
     66 39 1 1 0 13 0 0 1 1  6  4 0 4.7101436  9  11.48077      9.75
     67 40 1 1 0 15 0 0 0 0 11  1 0   8.05153 24 11.038462  3.166667
     70 36 1 1 0 13 0 0 0 0  5  3 0 16.739124 38 16.083334 16.083334
     71 34 1 1 0 12 0 0 0 0  4  3 0  5.724636 38      10.5       2.5
     72 36 1 1 0 12 0 0 1 0 11  3 1  7.458193 40  9.442307 1.0833334
     73 36 1 0 1 14 0 0 0 0  4  3 .  6.793478 40 14.442307        .5
     75 39 2 0 0 11 0 0 0 0  4  6 1      12.5 40      14.5  9.333333
     78 40 1 1 0 12 0 0 1 1  7  3 0 17.697258 35 16.326923  9.083333
     80 45 1 1 0 14 0 0 1 0 11  3 0 12.077295  8  5.717949         0
     85 38 1 0 0 11 0 0 1 1  4  3 .  9.406354 40  6.192307       1.5
     86 44 1 0 1 17 1 0 1 0 10  8 1  13.17229 44  16.26923      1.25
    103 36 1 1 0 18 1 0 1 1 11 13 1   11.0628 35  9.833333         0
    104 41 1 1 0 17 1 0 1 0 11 13 0 3.7439594 18  8.948718      2.75
    105 41 1 1 0 16 1 0 1 0  6  3 0  4.025765  8  8.788462  5.583333
    106 38 2 0 0 12 0 1 1 1 12  3 0 10.837358 40 17.416666 17.416666
    107 43 2 0 0  8 0 0 1 1  4  6 1  4.347824 40  7.544871 1.0833334
    110 45 1 1 0 12 0 0 1 0 11  3 1 4.7101436 20  6.660256  2.583333
    121 39 2 1 0 12 0 0 1 1  5  3 1  9.726243 38  20.69872 20.666666
    123 41 3 0 0  6 0 0 1 1  8  6 1  2.898549 37  7.320513       .75
    126 39 2 0 0 12 0 0 1 1 11  1 0 10.064413 40     19.75     19.75
    128 36 1 1 0 12 0 0 1 0  7  1 0  8.518515 40 19.032053        19
    129 43 1 0 0 12 0 0 1 0 11  3 0   8.49436 37  14.23077 10.166667
    130 43 1 1 0 12 0 0 1 0  4  3 0  5.600358 40  6.153846  .6666667
    131 41 1 0 0 12 0 0 1 1 11  3 1  4.227053 38  12.26282       .25
    132 36 1 0 0 10 0 0 1 1  4  6 0  5.619967 43  10.24359  2.666667
    134 42 1 1 0 12 0 0 1 0 12  2 0   4.64573 30  6.128205       1.5
    137 36 2 1 0 17 1 0 1 0 11 13 1  15.48309 30  9.833333  9.833333
    139 44 2 0 0  9 0 0 1 1 11  3 1  4.830918 35       8.5       8.5
    141 43 1 0 0 14 0 0 1 0 11  3 .  40.19808 40 12.910256 1.1666666
    142 43 1 1 0 18 1 0 1 0 11  1 1  15.48309 40 15.115384      5.75
    143 37 1 1 0 15 0 0 1 0 11  1 0 10.033444 40  7.076923  .5833333
    144 43 1 1 0 10 0 1 1 0  1  3 0 3.6231885 40 14.326923  .3333333
    147 40 1 1 0 12 0 0 1 0  7  4 0  9.299514 20  7.666667  3.083333
    152 37 1 1 0 18 1 0 1 0 11 13 1 10.217388 50 13.673077  4.833333
    159 36 1 1 0 11 0 0 1 0  7  4 0 10.008045 37 17.397436 17.333334
    166 38 1 1 0 14 0 0 1 0 11 13 0  4.227053  4 10.602565 1.0833334
    167 37 1 1 0 13 0 1 0 0  3  3 0  4.830918 25 16.980768       7.5
    168 41 1 1 0 17 1 1 1 0 11 13 0  7.745568 40 11.301282  3.833333
    169 36 1 0 0 16 1 0 1 0 11 13 1  9.847019 37 14.833333 14.833333
    172 44 1 1 0 15 1 0 1 1 11  1 0 10.869566 30 17.833334  3.416667
    173 43 2 0 0 12 0 0 1 1  9  7 .  3.220612 35  11.50641  2.916667
    176 38 2 0 1 12 0 0 1 1 12  1 1  8.164251 40 12.846153       3.5
    183 43 1 0 0 12 0 0 1 0  4  2 1  8.454107 40 17.121796     13.25
    184 41 1 1 0 12 0 0 1 0 11  3 0  8.293073 40 13.788462 12.083333
    188 41 1 1 0 12 0 0 1 0  4  3 1  9.677936 40  13.26923  8.416667
    195 36 1 1 0 12 0 0 1 0  1  3 0 4.5330095 40 12.801282 2.1666667
    202 36 1 0 0 14 0 1 0 0  6  8 .  3.344482 30 11.551282       .25
    203 41 1 1 0 11 0 1 1 1  7  4 0  8.454107 24  19.00641  3.166667
    204 38 2 1 0 10 0 0 1 1  6  2 0  6.038648 40  5.596154         4
    206 37 1 1 0 14 0 0 1 1 11  1 0  10.32206 18 10.333333 1.3333334
    207 38 2 0 1 18 1 0 1 1 11  1 1 11.610305 50  13.26923 2.0833333
    210 37 2 0 0 11 0 0 1 0  4  6 .  5.016723 40 15.641026 10.916667
    213 43 1 0 0 10 0 0 1 1  4  6 0  6.441224 40 16.903847 14.416667
    215 36 1 0 1 16 1 0 1 0  6  3 0  5.032206 40 15.852564      3.75
    218 35 1 1 0 12 0 0 0 0 11  2 0  4.830918 38 10.115384 2.0833333
    219 35 1 0 1 13 0 0 1 1 11  8 0 11.070854 40 16.858974  8.916667
    221 40 1 1 0 16 1 0 1 0  4  1 0  8.317226 35  9.256411 1.8333334
    223 37 1 0 0 16 1 0 1 0 10  1 0 13.083735 40 15.916667      8.75
    224 38 1 1 0 13 0 0 1 0 11  3 1  5.628018 40 10.788462 4.6666665
    225 35 1 0 0 16 1 0 1 0  4  2 0 12.560386 45 16.692308     15.75
    226 34 1 1 0 16 1 1 1 0 12  1 0  6.682766 40  7.692307  6.666667
    227 42 1 0 0 15 0 1 1 1  4  3 0  5.636071 40 18.788462 1.3333334
    228 42 1 1 0 16 1 1 1 0  7  3 0  4.025765 15  4.980769  .3333333
    231 42 1 1 0 16 1 0 1 0  8  3 0 18.067625 30 10.589743  9.833333
    233 36 1 1 0 16 1 0 0 0 11  1 0  5.805152 40 13.923077 13.916667
    236 36 1 1 0 18 1 0 1 1 11  1 0  24.66183 20  9.480769      3.75
    237 36 1 0 1 17 1 0 1 1 11  3 1   7.21417 40 12.096154      1.75
    end
    label values race racelbl
    label def racelbl 1 "White", modify
    label def racelbl 2 "Black", modify
    label def racelbl 3 "Other", modify
    label values married marlbl
    label def marlbl 0 "Single", modify
    label def marlbl 1 "Married", modify
    label values never_married nev_mar
    label def nev_mar 0 "Has been married", modify
    label def nev_mar 1 "Never married", modify
    label values collgrad gradlbl
    label def gradlbl 0 "Not college grad", modify
    label def gradlbl 1 "College grad", modify
    label values south southlbl
    label def southlbl 0 "Not south", modify
    label def southlbl 1 "South", modify
    label values smsa smsalbl
    label def smsalbl 0 "Not SMSA", modify
    label def smsalbl 1 "SMSA", modify
    label values c_city ccitylbl
    label def ccitylbl 0 "Not central city", modify
    label def ccitylbl 1 "Central city", modify
    label values industry indlbl
    label def indlbl 1 "Ag/Forestry/Fisheries", modify
    label def indlbl 3 "Construction", modify
    label def indlbl 4 "Manufacturing", modify
    label def indlbl 5 "Transport/Comm/Utility", modify
    label def indlbl 6 "Wholesale/Retail trade", modify
    label def indlbl 7 "Finance/Ins/Real estate", modify
    label def indlbl 8 "Business/Repair svc", modify
    label def indlbl 9 "Personal services", modify
    label def indlbl 10 "Entertainment/Rec svc", modify
    label def indlbl 11 "Professional services", modify
    label def indlbl 12 "Public administration", modify
    label values occupation occlbl
    label def occlbl 1 "Professional/Technical", modify
    label def occlbl 2 "Managers/Admin", modify
    label def occlbl 3 "Sales", modify
    label def occlbl 4 "Clerical/Unskilled", modify
    label def occlbl 5 "Craftsmen", modify
    label def occlbl 6 "Operatives", modify
    label def occlbl 7 "Transport", modify
    label def occlbl 8 "Laborers", modify
    label def occlbl 13 "Other", modify
    label values union unionlbl
    label def unionlbl 0 "Nonunion", modify
    label def unionlbl 1 "Union", modify
    ------------------ copy up to and including the previous line ------------------

  • #2
    For a variable to be a suitable dependent variable for logistic regression it must be dichotomous (i.e. two and only two different values) and those values must be 0 and 1.
    If you run
    Code:
    tab1 race married never_married grade collgrad south smsa c_city industry occupation, nolabel
    you will see that several of these variables do not fill these requirements. For example you will see that race has values 1, 2, and 3. Logistic regression interprets any non-missing value other than 0 in a dependent variable of a logistic regression as equivalent to 1. So as far as -logistic- is concerned, the value of race is always 1, and, therefore, does not vary. The same is true of the other variables except married, never_married, collgrad, south, smsa, and c_city.

    Comment


    • #3
      Thank you Clyde, This helped me identify the problem and I was able to solve it. Thank you.

      Comment

      Working...
      X