Announcement

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

  • Using loop for regression and saving results as a new variable

    Happy new year to Satatlist!


    I'm stuck with an old problem (using loop for regression). The variables are smoking (DV), country, education and income. The goal is to regress 'smoking' on 'education' and 'income' for all seven countries, and to save the ORs for each country. I tried the following code, which is not producing anything:

    Code:
    local saving n.dta
    
     foreach n in ` country' {
    statsby _b, by(country): logit smoker i.education i.income
      eststo
    }

    Thanks in advance for you insights. Here is the dataex:

    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(country education income smoker)
    4 0 3 1
    2 0 3 1
    4 0 3 1
    5 0 3 1
    4 0 3 1
    3 0 3 1
    7 0 3 1
    3 1 4 1
    3 0 4 1
    5 0 4 1
    3 0 4 1
    2 0 4 1
    4 0 4 1
    5 0 2 1
    4 0 2 1
    7 0 3 1
    1 1 3 1
    7 0 2 1
    2 0 2 1
    6 0 2 0
    1 2 2 1
    7 0 2 1
    3 0 2 1
    3 0 2 1
    6 0 3 1
    2 0 3 1
    5 0 2 1
    1 0 2 1
    5 0 2 1
    5 0 2 0
    7 0 2 1
    7 0 2 1
    1 0 2 .
    7 0 2 1
    1 0 2 .
    3 0 2 1
    4 0 2 1
    4 0 2 1
    2 0 2 .
    1 0 2 .
    4 0 2 1
    6 0 3 1
    6 0 3 1
    6 0 3 1
    1 0 3 .
    2 0 3 1
    7 0 2 0
    3 0 2 1
    1 0 2 1
    1 0 2 .
    1 0 3 .
    3 0 4 1
    7 0 4 1
    1 0 4 .
    2 0 4 1
    2 0 1 1
    1 0 3 1
    1 0 3 1
    3 0 1 1
    3 0 2 1
    2 0 3 1
    1 0 3 1
    1 0 3 .
    7 0 2 1
    3 0 2 1
    4 0 3 1
    3 0 3 1
    2 0 3 1
    2 0 3 1
    3 0 3 1
    2 0 3 .
    1 0 3 .
    5 0 1 1
    2 0 1 1
    5 0 2 1
    2 0 2 1
    7 0 1 1
    2 0 1 1
    1 0 1 1
    7 0 1 1
    1 0 1 .
    1 1 1 .
    3 0 1 1
    3 0 1 1
    4 0 1 1
    4 0 1 1
    2 0 1 1
    1 0 1 1
    3 0 1 1
    5 0 1 1
    1 0 1 1
    5 0 1 1
    3 2 2 1
    5 0 1 1
    4 0 1 1
    5 0 1 1
    2 0 1 .
    1 0 1 .
    2 0 3 1
    2 0 3 1
    end




  • #2
    If you refer to a user written command, you need to identify the source (see FAQs). estout is from SSC.

    Code:
    levelsof country, local(list)
    foreach country of local list{
        logit smoker ib0.education ib1.income if country==`country'
        eststo
    }
    esttab est*
    Last edited by Andrew Musau; 01 Jan 2020, 15:24.

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      If you refer to a user written command, you need to identify the source (see FAQs). estout is from SSC.

      Code:
      levelsof country, local(list)
      foreach country of local list{
      logit smoker ib0.education ib1.income if country==`country'
      eststo
      }
      esttab est*
      Hi Andrew,
      Thanks for the code. This method is not saving the estimates as a new variable.

      Comment


      • #4
        Code:
        capture program drop one_country
        program define one_country
            logit smoker ib0.education ib1.income
            levelsof education if e(sample), local(eds)
            foreach e of local eds {
                gen or_education_`e' = exp(_b[`e'.education])
            }
            levelsof income if e(sample), local(incs)
            foreach i of local incs {
                gen or_income_`i' = exp(_b[`i'.education])
            }
            exit
        end
        
        runby one_country, by(country) verbose
        -runby- is written by Robert Picard and me, and is available from SSC

        Note: This code does not run in your example data, because in every country you show, either the outcome, smoker, is a constant so that there is no logistic regression, or you have perfect prediction by some values of income. Also, in most of your example countries, the education variable takes on only value 0, so it never enters the regression. I presume that your actual data set does not exhibit this kind of pathology. ( If it does, you won't be able to run logistic regressions at all, let alone capture any coefficients.)

        Comment


        • #5
          This method is not saving the estimates as a new variable.
          I would favor Clyde's approach here, but keeping with esttab, you just need to extract the matrix of coefficients, t-values and p-values. You can do something like the following.

          Code:
          levelsof country, local(list)
          foreach country in local list{
              logit smoker ib0.education ib1.income if country==`country'
              eststo
          }
          esttab est*
          mat c= r(coefs)
          foreach country of local list{
               local names "`names' coef`country' tval`country' pval`country'"
          }
          mat colnames c =`names'
          svmat c, names(col)
          The variable numbers correspond to your country ids. If you do not need the t-values and p-values, then

          Code:
          drop tval* pval*
          Last edited by Andrew Musau; 02 Jan 2020, 00:48.

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            Code:
            capture program drop one_country
            program define one_country
            logit smoker ib0.education ib1.income
            levelsof education if e(sample), local(eds)
            foreach e of local eds {
            gen or_education_`e' = exp(_b[`e'.education])
            }
            levelsof income if e(sample), local(incs)
            foreach i of local incs {
            gen or_income_`i' = exp(_b[`i'.education])
            }
            exit
            end
            
            runby one_country, by(country) verbose
            -runby- is written by Robert Picard and me, and is available from SSC

            Note: This code does not run in your example data, because in every country you show, either the outcome, smoker, is a constant so that there is no logistic regression, or you have perfect prediction by some values of income. Also, in most of your example countries, the education variable takes on only value 0, so it never enters the regression. I presume that your actual data set does not exhibit this kind of pathology. ( If it does, you won't be able to run logistic regressions at all, let alone capture any coefficients.)
            Thanks so much Clyde! This must have taken a lot of time.

            I used a similar dataset that doesn't contain any 'Zero', and it worked like magic. In fact its giving far more than I was expecting, it saved the ORs for every single observation, while I was expecting a result that'd will look like this (same number of observation as country):

            ------------------------------------------------------------------------------------------------------------------------------------
            Country education(OR) education(UCI) education(LCI) income(OR) income(UCI) income(LCI)
            ----------------------------------------------------------------------------------------------------------------------------------
            Country 1
            Country 2
            Country 3
            Country 4
            Country 5
            Country 6
            Country 7
            ------------------------------------------------------------------------------------------------------------------------------------

            I am realising my description was not detailed enough. Is it possible to achieve the result in the format as the table above.

            Also, I recoded education from (0 1 2 3) to (1 2 3 4). But it's showing an error: [5.education] not found
            This is not happening with other variables.


            Code:
            . use  "/Users/sonnen/Desktop/Stata.dta", replace
            
            . capture program drop one_country
            
            . program define one_country
              1.     logit smoker ib1.education ib1.income
              2.     levelsof education if e(sample), local(eds)
              3.     foreach e of local eds {
              4.         gen or_education_`e' = exp(_b[`e'.education])
              5.     }
              6.     levelsof income if e(sample), local(incs)
              7.     foreach i of local incs {
              8.         gen or_income_`i' = exp(_b[`i'.education])
              9.     }
             10.     exit
             11. end
            
            . 
            . runby one_country, by(country) verbose
            
            Iteration 0:   log likelihood = -2480.1698  
            Iteration 1:   log likelihood = -2406.8117  
            Iteration 2:   log likelihood = -2400.8593  
            Iteration 3:   log likelihood = -2400.8436  
            Iteration 4:   log likelihood = -2400.8436  
            
            Logistic regression                             Number of obs     =      7,040
                                                            LR chi2(7)        =     158.65
                                                            Prob > chi2       =     0.0000
            Log likelihood = -2400.8436                     Pseudo R2         =     0.0320
            
            ------------------------------------------------------------------------------
                  smoker |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
               education |
              Secondary  |    .662324   .0884421     7.49   0.000     .4889807    .8356673
                 Higher  |   .5612048   .1282858     4.37   0.000     .3097693    .8126403
                      4  |   .4132062    .513286     0.81   0.421    -.5928158    1.419228
                         |
                  income |
                 Poorer  |  -.2175148   .1249359    -1.74   0.082    -.4623846    .0273549
                 Middle  |   -.356171   .1281389    -2.78   0.005    -.6073187   -.1050234
                 Richer  |  -.1397112   .1200118    -1.16   0.244    -.3749299    .0955075
                Richest  |   .5576464   .1177329     4.74   0.000      .326894    .7883987
                         |
                   _cons |  -2.251786   .0903782   -24.92   0.000    -2.428925   -2.074648
            ------------------------------------------------------------------------------
            1 2 3 4
            1 2 3 4 5
            [5.education] not found

            [CODE]
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input byte(smoker region education income) float(country or_region_1 or_region_2 or_region_3 or_region_4 or_region_5 or_region_6 or_region_7 or_income_1 or_income_2 or_income_3 or_income_4 or_income_5)
            0 5 2 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 5 2 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 6 2 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 3 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            1 6 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 7 2 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 2 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 2 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 3 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 7 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 5 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 6 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 2 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            1 4 2 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 3 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 3 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 2 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 2 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 6 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 6 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 5 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 5 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 1 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 5 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 1 2 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 1 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 6 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 6 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 6 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 2 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 2 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 6 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 2 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 2 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 7 2 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 2 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 1 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 1 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 1 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 5 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 5 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 1 1 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            1 6 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 2 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 2 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 2 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 3 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 3 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 3 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            1 4 1 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 6 2 5 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 1 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 3 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 1 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 4 1 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 2 1 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 3 1 2 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            0 5 1 4 29 1 1.1238878 1.105319 1.0134941 1.1859336 1.583443 1.5166625 1 1.1238878 1.105319 1.0134941 1.1859336
            end

            Comment


            • #7
              Originally posted by Andrew Musau View Post

              I would favor Clyde's approach here, but keeping with esttab, you just need to extract the matrix of coefficients, t-values and p-values. You can do something like the following.

              Code:
              levelsof country, local(list)
              foreach country in local list{
              logit smoker ib0.education ib1.income if country==`country'
              eststo
              }
              esttab est*
              mat c= r(coefs)
              foreach country of local list{
              local names "`names' coef`country' tval`country' pval`country'"
              }
              mat colnames c =`names'
              svmat c, names(col)
              The variable numbers correspond to your country ids. If you do not need the t-values and p-values, then

              Code:
              drop tval* pval*
              Thanks again Andrew. It seems like I'm missing something. The estimates are being stored as '0' and '1'.

              Code:
               levelsof country, local(list)
              foreach country of local list{
                  logit smoker ib1.education ib1.income if country==`country'
                  eststo
              }
              qui esttab est*
              mat c= r(coefs)
              foreach country of local list{
                   local names "`names' coef`country' tval`country' pval`country'"
              }
              mat colnames c =`names'
              svmat c, names(col)
              After that its showing an error at the mat step:

              Code:
              . qui esttab est*
              
              . mat c= r(coefs)
              
              . foreach country of local list{
                2.      local names "`names' coef`country' tval`country' pval`country'"
                3. }
              
              . mat colnames c =`names'
              conformability error
              r(503);
              I am using a difffernt (but similar structure), may be the new code is no fitting the new data...

              [CODE]
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input byte(smoker region) float education byte income float country byte(_est_est43 _est_est44 _est_est45 _est_est46 _est_est47 _est_est48 _est_est49) float c1
              0 5 2 4 1 1 0 0 0 0 0 0 .
              0 5 2 4 1 1 0 0 0 0 0 0 .
              0 6 2 1 1 1 0 0 0 0 0 0 .
              0 2 3 5 1 1 0 0 0 0 0 0 .
              1 6 1 1 1 1 0 0 0 0 0 0 .
              0 7 2 2 1 1 0 0 0 0 0 0 .
              0 4 2 4 1 1 0 0 0 0 0 0 .
              0 4 2 5 1 1 0 0 0 0 0 0 .
              0 2 3 5 1 1 0 0 0 0 0 0 .
              0 7 1 4 1 1 0 0 0 0 0 0 .
              0 5 1 4 1 1 0 0 0 0 0 0 .
              0 6 1 4 1 1 0 0 0 0 0 0 .
              0 4 2 4 1 1 0 0 0 0 0 0 .
              1 4 2 4 1 1 0 0 0 0 0 0 .
              0 2 3 4 1 1 0 0 0 0 0 0 .
              0 2 3 4 1 1 0 0 0 0 0 0 .
              0 4 2 1 1 1 0 0 0 0 0 0 .
              0 2 1 1 1 1 0 0 0 0 0 0 .
              0 2 2 1 1 1 0 0 0 0 0 0 .
              0 3 1 1 1 1 0 0 0 0 0 0 .
              0 2 1 1 1 1 0 0 0 0 0 0 .
              0 6 1 1 1 1 0 0 0 0 0 0 .
              0 6 1 1 1 1 0 0 0 0 0 0 .
              0 2 1 2 1 1 0 0 0 0 0 0 .
              0 5 1 1 1 1 0 0 0 0 0 0 .
              0 5 1 1 1 1 0 0 0 0 0 0 .
              0 3 1 1 1 1 0 0 0 0 0 0 .
              0 3 1 1 1 1 0 0 0 0 0 0 .
              0 3 1 1 1 1 0 0 0 0 0 0 .
              0 3 1 1 1 1 0 0 0 0 0 0 .
              0 1 1 1 1 1 0 0 0 0 0 0 .
              0 4 1 1 1 1 0 0 0 0 0 0 .
              0 5 1 4 1 1 0 0 0 0 0 0 .
              0 2 1 3 1 1 0 0 0 0 0 0 .
              0 1 2 4 1 1 0 0 0 0 0 0 .
              0 1 1 3 1 1 0 0 0 0 0 0 .
              0 6 1 4 1 1 0 0 0 0 0 0 .
              0 6 1 4 1 1 0 0 0 0 0 0 .
              0 6 1 4 1 1 0 0 0 0 0 0 .
              0 2 2 4 1 1 0 0 0 0 0 0 .
              0 2 1 3 1 1 0 0 0 0 0 0 .
              0 3 2 3 1 1 0 0 0 0 0 0 .
              0 6 1 3 1 1 0 0 0 0 0 0 .
              0 3 1 4 1 1 0 0 0 0 0 0 .
              0 3 2 3 1 1 0 0 0 0 0 0 .
              0 3 2 3 1 1 0 0 0 0 0 0 .
              0 4 1 3 1 1 0 0 0 0 0 0 .
              0 7 2 1 1 1 0 0 0 0 0 0 .
              0 4 2 1 1 1 0 0 0 0 0 0 .
              0 2 1 4 1 1 0 0 0 0 0 0 .
              0 2 1 4 1 1 0 0 0 0 0 0 .
              0 3 1 4 1 1 0 0 0 0 0 0 .
              0 3 1 4 1 1 0 0 0 0 0 0 .
              0 1 1 4 1 1 0 0 0 0 0 0 .
              0 1 1 4 1 1 0 0 0 0 0 0 .
              0 3 1 3 1 1 0 0 0 0 0 0 .
              0 3 1 3 1 1 0 0 0 0 0 0 .
              0 3 1 3 1 1 0 0 0 0 0 0 .
              0 1 1 3 1 1 0 0 0 0 0 0 .
              0 3 1 4 1 1 0 0 0 0 0 0 .
              0 3 1 4 1 1 0 0 0 0 0 0 .
              0 3 1 4 1 1 0 0 0 0 0 0 .
              0 4 1 2 1 1 0 0 0 0 0 0 .
              0 5 1 3 1 1 0 0 0 0 0 0 .
              0 5 1 3 1 1 0 0 0 0 0 0 .
              0 4 1 2 1 1 0 0 0 0 0 0 .
              0 3 1 2 1 1 0 0 0 0 0 0 .
              0 2 1 4 1 1 0 0 0 0 0 0 .
              0 4 1 4 1 1 0 0 0 0 0 0 .
              0 4 1 3 1 1 0 0 0 0 0 0 .
              0 2 1 2 1 1 0 0 0 0 0 0 .
              0 3 1 4 1 1 0 0 0 0 0 0 .
              0 1 1 2 1 1 0 0 0 0 0 0 .
              1 6 1 4 1 1 0 0 0 0 0 0 .
              0 4 1 4 1 1 0 0 0 0 0 0 .
              0 3 1 3 1 1 0 0 0 0 0 0 .
              0 2 1 4 1 1 0 0 0 0 0 0 .
              0 4 1 3 1 1 0 0 0 0 0 0 .
              0 3 1 3 1 1 0 0 0 0 0 0 .
              0 3 1 4 1 1 0 0 0 0 0 0 .
              0 2 1 4 1 1 0 0 0 0 0 0 .
              0 4 1 5 1 1 0 0 0 0 0 0 .
              0 3 2 5 1 1 0 0 0 0 0 0 .
              0 3 1 5 1 1 0 0 0 0 0 0 .
              0 3 1 5 1 1 0 0 0 0 0 0 .
              0 3 2 5 1 1 0 0 0 0 0 0 .
              0 3 2 5 1 1 0 0 0 0 0 0 .
              0 2 3 5 1 1 0 0 0 0 0 0 .
              0 3 3 5 1 1 0 0 0 0 0 0 .
              0 2 3 5 1 1 0 0 0 0 0 0 .
              1 4 1 5 1 1 0 0 0 0 0 0 .
              0 6 2 5 1 1 0 0 0 0 0 0 .
              0 1 1 1 1 1 0 0 0 0 0 0 .
              0 4 1 3 1 1 0 0 0 0 0 0 .
              0 2 1 2 1 1 0 0 0 0 0 0 .
              0 2 1 1 1 1 0 0 0 0 0 0 .
              0 4 1 2 1 1 0 0 0 0 0 0 .
              0 2 1 2 1 1 0 0 0 0 0 0 .
              0 3 1 2 1 1 0 0 0 0 0 0 .
              0 5 1 4 1 1 0 0 0 0 0 0 .
              end

              Comment


              • #8
                Thanks again Andrew. It seems like I'm missing something. The estimates are being stored as '0' and '1'.
                Those are not the estimates. I will add a line to delete these 0/1 variables at the end.

                . mat colnames c =`names' conformability error r(503);
                Probably you are running the code several times and the local simply keeps on expanding. I will also add a line to prevent this. Your example data in #7 has just one country but based on that, the following works. Additions to the code are highlighted in red below.

                Code:
                eststo clear
                levelsof country, local(list)
                foreach country of local list{
                    logit smoker ib1.education ib1.income if country==`country'
                    eststo
                }
                qui esttab est*
                mat c= r(coefs)
                local names ""
                foreach country of local list{
                     local names "`names' coef`country' tval`country' pval`country'"
                }
                mat colnames c =`names'
                drop _est*
                svmat c, names(col)
                Res.:

                Code:
                . l in 1/10, sep(10)
                
                     +---------------------------------------------------------------------------------------+
                     | smoker   region   educat~n   income   country   c1       coef1       tval1      pval1 |
                     |---------------------------------------------------------------------------------------|
                  1. |      0        5          2        4         1    .           0           .          . |
                  2. |      0        5          2        4         1    .   -.3260053   -.2585688   .7959679 |
                  3. |      0        6          2        1         1    .           0           .          . |
                  4. |      0        2          3        5         1    .           0           .          . |
                  5. |      1        6          1        1         1    .           0           .          . |
                  6. |      0        7          2        2         1    .           0           .          . |
                  7. |      0        4          2        4         1    .    .2473058    .1964868   .8442292 |
                  8. |      0        4          2        5         1    .    1.017847    .6674063   .5045126 |
                  9. |      0        2          3        5         1    .   -2.926425    -2.78117   .0054163 |
                 10. |      0        7          1        4         1    .           .           .          . |
                     +---------------------------------------------------------------------------------------+



                Comment


                • #9
                  Originally posted by Andrew Musau View Post

                  Those are not the estimates. I will add a line to delete these 0/1 variables at the end.



                  Probably you are running the code several times and the local simply keeps on expanding. I will also add a line to prevent this. Your example data in #7 has just one country but based on that, the following works. Additions to the code are highlighted in red below.

                  Code:
                  eststo clear
                  levelsof country, local(list)
                  foreach country of local list{
                  logit smoker ib1.education ib1.income if country==`country'
                  eststo
                  }
                  qui esttab est*
                  mat c= r(coefs)
                  local names ""
                  foreach country of local list{
                  local names "`names' coef`country' tval`country' pval`country'"
                  }
                  mat colnames c =`names'
                  drop _est*
                  svmat c, names(col)
                  Res.:

                  Code:
                  . l in 1/10, sep(10)
                  
                  +---------------------------------------------------------------------------------------+
                  | smoker region educat~n income country c1 coef1 tval1 pval1 |
                  |---------------------------------------------------------------------------------------|
                  1. | 0 5 2 4 1 . 0 . . |
                  2. | 0 5 2 4 1 . -.3260053 -.2585688 .7959679 |
                  3. | 0 6 2 1 1 . 0 . . |
                  4. | 0 2 3 5 1 . 0 . . |
                  5. | 1 6 1 1 1 . 0 . . |
                  6. | 0 7 2 2 1 . 0 . . |
                  7. | 0 4 2 4 1 . .2473058 .1964868 .8442292 |
                  8. | 0 4 2 5 1 . 1.017847 .6674063 .5045126 |
                  9. | 0 2 3 5 1 . -2.926425 -2.78117 .0054163 |
                  10. | 0 7 1 4 1 . . . . |
                  +---------------------------------------------------------------------------------------+


                  It seems the error is on my side. I reopened the Stata and cleared all histories before running this analysis, but the 'mat' error persists.

                  Code:
                  . qui esttab est*
                  
                  . 
                  . mat c= r(coefs)
                  
                  . local names ""
                  
                  . foreach country of local list{
                    2.      local names "`names' coef`country' tval`country' pval`country'"
                    3. }
                  
                  . mat colnames c =`names'
                  conformability error
                  r(503);
                  
                  . drop _est*
                  
                  . svmat c, names(col)

                  Comment


                  • #10
                    It seems the error is on my side. I reopened the Stata and cleared all histories before running this analysis, but the 'mat' error persists.
                    It should not, at least with the data posted in #7. I suspect that in your full data set, there may be additional problems of the sort noted by Clyde in #4. For example, if the outcome does not vary for at least one country, the logistic regression will fail and therefore, you will have a lesser number of coefficients than implied by the names to be assigned to the columns of the compiled matrix (hence the conformability error). I think the most efficient route is to upload your full data set as it is too big.

                    Comment


                    • #11
                      Responding to #6:

                      I made a mistake in the code. It should be:

                      Code:
                      capture program drop one_country
                      program define one_country
                          logit smoker ib1.education ib1.income
                          levelsof education if e(sample), local(eds)
                          foreach e of local eds {
                              gen or_education_`e' = exp(_b[`e'.education])
                          }
                          levelsof income if e(sample), local(incs)
                          foreach i of local incs {
                              gen or_income_`i' = exp(_b[`i'.income])
                          }
                          exit
                      end
                      
                      
                      runby one_country, by(country) verbose
                      (Change in italics.)

                      As for your final desired output, the OR variables are all constant within country. So, once you have run the above:

                      Code:
                      collapse (first) or_*, by(country)
                      will reduce the data to one observation per country with all of the odds ratios.

                      Since you are not familiar with the -collapse- command, I recommend you take a break from what you are doing and get up to speed with the fundamental commands that everyone working with Stata should know, in order to use Stata effectively on a regular basis. Read the Getting Started [GS] and User's Guide [U] volumes of the PDF documentation that is part of your Stata installation. It's a somewhat lengthy read, but you will be introduced to things that you will use over and over again, and your time will be amply repaid soon thereafter.

                      Comment


                      • #12
                        Originally posted by Andrew Musau View Post

                        It should not, at least with the data posted in #7. I suspect that in your full data set, there may be additional problems of the sort noted by Clyde in #4. For example, if the outcome does not vary for at least one country, the logistic regression will fail and therefore, you will have a lesser number of coefficients than implied by the names to be assigned to the columns of the compiled matrix (hence the conformability error). I think the most efficient route is to upload your full data set as it is too big.
                        Thanks a lot Andrew! I'll retry this method with a new dataset.

                        Comment


                        • #13
                          Originally posted by Clyde Schechter View Post
                          Responding to #6:

                          I made a mistake in the code. It should be:

                          Code:
                          capture program drop one_country
                          program define one_country
                          logit smoker ib1.education ib1.income
                          levelsof education if e(sample), local(eds)
                          foreach e of local eds {
                          gen or_education_`e' = exp(_b[`e'.education])
                          }
                          levelsof income if e(sample), local(incs)
                          foreach i of local incs {
                          gen or_income_`i' = exp(_b[`i'.income])
                          }
                          exit
                          end
                          
                          
                          runby one_country, by(country) verbose
                          (Change in italics.)

                          As for your final desired output, the OR variables are all constant within country. So, once you have run the above:

                          Code:
                          collapse (first) or_*, by(country)
                          will reduce the data to one observation per country with all of the odds ratios.

                          Since you are not familiar with the -collapse- command, I recommend you take a break from what you are doing and get up to speed with the fundamental commands that everyone working with Stata should know, in order to use Stata effectively on a regular basis. Read the Getting Started [GS] and User's Guide [U] volumes of the PDF documentation that is part of your Stata installation. It's a somewhat lengthy read, but you will be introduced to things that you will use over and over again, and your time will be amply repaid soon thereafter.
                          Thanks a lot Clyde! This is really a big help. I'll go through the User's Guide for these details. There is one thing I wanted to ask, you use this a lot: '`i''. Where can I find explanations on this?

                          Comment


                          • #14
                            The i. is fully explained in -help fvvarlist-.

                            Comment

                            Working...
                            X