Announcement

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

  • Loop for logistic regression

    Hi,

    I am trying to run a loop for a regression analysis and get one table with just the line with the "odds ratios", p-values and confidence intervals for all the variables on one table like below. I used the following code below but I get all individual tables with a lot of statistics I don't need.

    foreign | Odds Ratio Std. Err. z P>|z| [95% Conf. Interval]
    displacement | .9612389 .0102503 -3.71 0.000 .9413572 .9815405
    gear_ratio | 342.704 447.5543 4.47 0.000 26.50286 4431.448
    turn | .5857299 .0742041 -4.22 0.000 .4569426 .7508154


    sysuse auto.dta
    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str18 make int(price mpg rep78) float headroom int(trunk weight length turn displacement) float gear_ratio byte foreign
    "AMC Concord"        4099 22 3 2.5 11 2930 186 40 121 3.58 0
    "AMC Pacer"          4749 17 3   3 11 3350 173 40 258 2.53 0
    "AMC Spirit"         3799 22 .   3 12 2640 168 35 121 3.08 0
    "Buick Century"      4816 20 3 4.5 16 3250 196 40 196 2.93 0
    "Buick Electra"      7827 15 4   4 20 4080 222 43 350 2.41 0
    "Buick LeSabre"      5788 18 3   4 21 3670 218 43 231 2.73 0
    "Buick Opel"         4453 26 .   3 10 2230 170 34 304 2.87 0
    "Buick Regal"        5189 20 3   2 16 3280 200 42 196 2.93 0
    "Buick Riviera"     10372 16 3 3.5 17 3880 207 43 231 2.93 0
    "Buick Skylark"      4082 19 3 3.5 13 3400 200 42 231 3.08 0
    "Cad. Deville"      11385 14 3   4 20 4330 221 44 425 2.28 0
    "Cad. Eldorado"     14500 14 2 3.5 16 3900 204 43 350 2.19 0
    "Cad. Seville"      15906 21 3   3 13 4290 204 45 350 2.24 0
    "Chev. Chevette"     3299 29 3 2.5  9 2110 163 34 231 2.93 0
    "Chev. Impala"       5705 16 4   4 20 3690 212 43 250 2.56 0
    "Chev. Malibu"       4504 22 3 3.5 17 3180 193 31 200 2.73 0
    "Chev. Monte Carlo"  5104 22 2   2 16 3220 200 41 200 2.73 0
    "Chev. Monza"        3667 24 2   2  7 2750 179 40 151 2.73 0
    "Chev. Nova"         3955 19 3 3.5 13 3430 197 43 250 2.56 0
    "Dodge Colt"         3984 30 5   2  8 2120 163 35  98 3.54 0
    "Dodge Diplomat"     4010 18 2   4 17 3600 206 46 318 2.47 0
    "Dodge Magnum"       5886 16 2   4 17 3600 206 46 318 2.47 0
    "Dodge St. Regis"    6342 17 2 4.5 21 3740 220 46 225 2.94 0
    "Ford Fiesta"        4389 28 4 1.5  9 1800 147 33  98 3.15 0
    "Ford Mustang"       4187 21 3   2 10 2650 179 43 140 3.08 0
    "Linc. Continental" 11497 12 3 3.5 22 4840 233 51 400 2.47 0
    "Linc. Mark V"      13594 12 3 2.5 18 4720 230 48 400 2.47 0
    "Linc. Versailles"  13466 14 3 3.5 15 3830 201 41 302 2.47 0
    "Merc. Bobcat"       3829 22 4   3  9 2580 169 39 140 2.73 0
    "Merc. Cougar"       5379 14 4 3.5 16 4060 221 48 302 2.75 0
    "Merc. Marquis"      6165 15 3 3.5 23 3720 212 44 302 2.26 0
    "Merc. Monarch"      4516 18 3   3 15 3370 198 41 250 2.43 0
    "Merc. XR-7"         6303 14 4   3 16 4130 217 45 302 2.75 0
    "Merc. Zephyr"       3291 20 3 3.5 17 2830 195 43 140 3.08 0
    "Olds 98"            8814 21 4   4 20 4060 220 43 350 2.41 0
    "Olds Cutl Supr"     5172 19 3   2 16 3310 198 42 231 2.93 0
    "Olds Cutlass"       4733 19 3 4.5 16 3300 198 42 231 2.93 0
    "Olds Delta 88"      4890 18 4   4 20 3690 218 42 231 2.73 0
    "Olds Omega"         4181 19 3 4.5 14 3370 200 43 231 3.08 0
    "Olds Starfire"      4195 24 1   2 10 2730 180 40 151 2.73 0
    "Olds Toronado"     10371 16 3 3.5 17 4030 206 43 350 2.41 0
    "Plym. Arrow"        4647 28 3   2 11 3260 170 37 156 3.05 0
    "Plym. Champ"        4425 34 5 2.5 11 1800 157 37  86 2.97 0
    "Plym. Horizon"      4482 25 3   4 17 2200 165 36 105 3.37 0
    "Plym. Sapporo"      6486 26 . 1.5  8 2520 182 38 119 3.54 0
    "Plym. Volare"       4060 18 2   5 16 3330 201 44 225 3.23 0
    "Pont. Catalina"     5798 18 4   4 20 3700 214 42 231 2.73 0
    "Pont. Firebird"     4934 18 1 1.5  7 3470 198 42 231 3.08 0
    "Pont. Grand Prix"   5222 19 3   2 16 3210 201 45 231 2.93 0
    "Pont. Le Mans"      4723 19 3 3.5 17 3200 199 40 231 2.93 0
    "Pont. Phoenix"      4424 19 . 3.5 13 3420 203 43 231 3.08 0
    "Pont. Sunbird"      4172 24 2   2  7 2690 179 41 151 2.73 0
    "Audi 5000"          9690 17 5   3 15 2830 189 37 131  3.2 1
    "Audi Fox"           6295 23 3 2.5 11 2070 174 36  97  3.7 1
    "BMW 320i"           9735 25 4 2.5 12 2650 177 34 121 3.64 1
    "Datsun 200"         6229 23 4 1.5  6 2370 170 35 119 3.89 1
    "Datsun 210"         4589 35 5   2  8 2020 165 32  85  3.7 1
    "Datsun 510"         5079 24 4 2.5  8 2280 170 34 119 3.54 1
    "Datsun 810"         8129 21 4 2.5  8 2750 184 38 146 3.55 1
    "Fiat Strada"        4296 21 3 2.5 16 2130 161 36 105 3.37 1
    "Honda Accord"       5799 25 5   3 10 2240 172 36 107 3.05 1
    "Honda Civic"        4499 28 4 2.5  5 1760 149 34  91  3.3 1
    "Mazda GLC"          3995 30 4 3.5 11 1980 154 33  86 3.73 1
    "Peugeot 604"       12990 14 . 3.5 14 3420 192 38 163 3.58 1
    "Renault Le Car"     3895 26 3   3 10 1830 142 34  79 3.72 1
    "Subaru"             3798 35 5 2.5 11 2050 164 36  97 3.81 1
    "Toyota Celica"      5899 18 5 2.5 14 2410 174 36 134 3.06 1
    "Toyota Corolla"     3748 31 5   3  9 2200 165 35  97 3.21 1
    "Toyota Corona"      5719 18 5   2 11 2670 175 36 134 3.05 1
    "VW Dasher"          7140 23 4 2.5 12 2160 172 36  97 3.74 1
    "VW Diesel"          5397 41 5   3 15 2040 155 35  90 3.78 1
    "VW Rabbit"          4697 25 4   3 15 1930 155 35  89 3.78 1
    "VW Scirocco"        6850 25 4   2 16 1990 156 36  97 3.78 1
    "Volvo 260"         11995 17 5 2.5 14 3170 193 37 163 2.98 1
    end
    label values foreign origin
    label def origin 0 "Domestic", modify
    label def origin 1 "Foreign", modify
    ------------------ copy up to and including the previous line ------------------


    [/CODE]
    foreach var of varlist price-gear_ratio{
    logistic foreign `var'
    }
    ----------------------- copy starting from the next line -----------------------

  • #2
    Code:
    clear*
    sysuse auto
    
    frame create results str32 indepvar float (or std_err z pvalue lb95 ub95)
    
    foreach v of varlist price-gear_ratio {
        logistic foreign `v'
        matrix M = r(table)
        frame post results ("`v'") (M["b", "foreign:`v'"]) (M["se", "foreign:`v'"]) ///
            (M["z", "foreign:`v'"]) (M["pvalue", "foreign:`v'"]) (M["ll", "foreign:`v'"]) ///
            (M["ul", "foreign:`v'"])
    }
    
    frame change results
    format or std_err lb ub %2.1f
    format z pvalue %05.3f
    quietly compress
    list, noobs clean
    Note: This code uses frames, as well as string matrix coefficients. This requires version 16 or later. You are supposed to state in your post if you are not using the current version (17) of Stata. So if you are using an older version than 16 you will have to change this code. This will require using a -postfile- instead of a -frame-, and replacing the string subscripts in the -post- command with the corresponding row and column numbers in the matrix M. If you just run -logistic foreign price- separately and then -matrix list -r(table)- you will be able to see what those corresponding numbers are.

    Comment


    • #3
      Hi Clyde,

      I have STATA vision 17 and this code gave me exactly whaat I needed. Thank you so much.

      Comment


      • #4
        Clyde, would this code work with a continuous outcome for the table? I change thee following in the code and got th error "price:mpg"not found.

        frame create results str32 indepvar float (coef std_err t pvalue lb95 ub95) foreach v of varlist mpg-gear_ratio { regress price `v' matrix M = r(table) frame post results ("`v'") (M["b", "price:`v'"]) (M["se", "price:`v'"]) /// (M["t", "price:`v'"]) (M["pvalue", "price:`v'"]) (M["ll", "price:`v'"]) /// (M["ul", "price:`v'"]) } frame change results format coef std_err lb ub %2.1f format z pvalue %05.3f quietly compress list, noobs clean

        Comment


        • #5
          My code is not posting correctly

          frame create results str32 indepvar float (coef std_err t pvalue lb95 ub95)
          foreach v of varlist mpg-gear_ratio { regress price `v' matrix M = r(table) frame post results ("`v'") (M["b", "price:`v'"]) (M["se", "price:`v'"]) /// (M["t", "price:`v'"]) (M["pvalue", "price:`v'"]) (M["ll", "price:`v'"]) /// (M["ul", "price:`v'"]) } frame change results format coef std_err lb ub %2.1f format z pvalue %05.3f quietly compress list, noobs clean

          Comment


          • #6
            You have to adapt the code to run it with a continuous outcome variable. You will be using, I assume, regress, not -logistic-. Remember that I pointed out that you need to run one of the analyses by itself first to see how the rows and columns of matrix M = r(table) are named. Watch:
            Code:
            . regress price mpg
            
                  Source |       SS           df       MS      Number of obs   =        74
            -------------+----------------------------------   F(1, 72)        =     20.26
                   Model |   139449474         1   139449474   Prob > F        =    0.0000
                Residual |   495615923        72  6883554.48   R-squared       =    0.2196
            -------------+----------------------------------   Adj R-squared   =    0.2087
                   Total |   635065396        73  8699525.97   Root MSE        =    2623.7
            
            ------------------------------------------------------------------------------
                   price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
            -------------+----------------------------------------------------------------
                     mpg |  -238.8943   53.07669    -4.50   0.000    -344.7008   -133.0879
                   _cons |   11253.06   1170.813     9.61   0.000     8919.088    13587.03
            ------------------------------------------------------------------------------
            
            . matrix list r(table)
            
            r(table)[9,2]
                           mpg       _cons
                 b  -238.89435   11253.061
                se   53.076687   1170.8128
                 t   -4.500928   9.6113239
            pvalue   .00002546   1.535e-14
                ll  -344.70079   8919.0881
                ul   -133.0879   13587.033
                df          72          72
              crit   1.9934636   1.9934636
             eform           0           0
            Notice that the column names do not mention price:, just straight up mpg. So you have to change the -frame post- command accordingly. Different estimation commands do these things differently, and sometimes it changes with new versions of Stata. So you have to check these things out.

            Comment


            • #7
              I was able to remedy my code. Thank you so much Clyde. So blessed to have your expertise.

              Comment

              Working...
              X