Announcement

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

  • Esttab Using Loop Not Working Properly for Categorical Variables in Logit Models

    Hi,

    I read posts related to the esttab carefully, but I did not find the exact answer to my question.

    I am running a simple loop on many countries with a logit model that uses the categorical variables country of birth, age group, education, and their interactions. I am interested in the odds ratios for birth-related variables across different countries. Therefore, I use esttab to obtain these odds ratios

    The categorical variable country of birth is not balanced across countries, resulting in varying numbers of categories. Consequently, esttab returns a table that uses only the labels for the categorical variable birth from the last model/country. Is there a simple way to prevent this and use the correct labels for the variable?

    Loop:
    Code:
    cd "....."
    local files: dir . files "*dta"
    
    foreach file of local files {
        use "`file'", clear
        quietly: logit employ  i.birth i.edu  i.age i.birth##i.edu, or noconstant baselevels nolog
        eststo
    }
    esttab, label eform nostar keep (*birth *birth#*edu)
    Last edited by Kirk Harvey; 31 Jul 2024, 08:56.

  • #2
    estout is from SSC (FAQ Advice #12). The labels must come from somewhere. It appears you are not running the regressions using a common dataset. Therefore, you should load the dataset with no missing countries before running the final esttab command. Secondly, ensure that the labels are consistently defined across the datasets. If you encoded the country variable independently in each dataset, you risk having mismatches since some countries may be missing in some datasets.

    Code:
    cd "....."
    estimates clear
    local files: dir . files "*dta"
    
    foreach file of local files {
        use "`file'", clear
        quietly: logit employ  i.birth i.edu  i.age i.birth##i.edu, or noconstant baselevels nolog
        eststo
    }
    use dataset_with_all_countries_labeled, clear
    esttab est*, label eform nostar keep (*birth *birth#*edu)
    Last edited by Andrew Musau; 31 Jul 2024, 09:50.

    Comment


    • #3
      ensure that the labels are consistently defined across the datasets
      Code:
      use dataset_with_all_countries_labeled, clear
      label save `:val lab birth' using mylabels
      Then open mylabels.do saved in your current directory, get the labels and relabel the birth variable within the loop.
      Last edited by Andrew Musau; 31 Jul 2024, 10:25.

      Comment


      • #4
        Andrew, thank you very much for your help. However, I believe my problem arises from a different source.

        The issue is that the categorical variable -birth- (representing regions of birth) covers different regions in various countries. For example, there are 13 regions in country A but 18 regions in country B.

        I assume my problem is somewhat similar to the problem mentioned here: https://www.statalist.org/forums/forum/general-stata-discussion/general/1692-preventing-estout-from-overwriting-e-labels-in-successive-runs , but I have no idea how to approach that in my case.

        When I run the loop, the -Esttab- output uses labels from the last dataset, and I do not know how to prevent this. One solution is to append datasets into one file, but the file is too large to handle. Given that no dataset has all labels for categorical variable birth, I can not use your "use dataset_with_all_countries_labeled, clear command".

        Comment


        • #5
          Do you need to report the coefficients on the region dummies or are these just control variables? If not, don't include them in the tables and just indicate their presence. Otherwise, it is difficult to imagine how a table reporting regional dummies from different countries (based on the model) would look like.

          For example, there are 13 regions in country A but 18 regions in country B.
          It is possible to write code that will handle this, but first let me know what kind of table you want to produce. If you can post an example of such a table, that would be helpful.
          Last edited by Andrew Musau; 31 Jul 2024, 14:24.

          Comment


          • #6
            This worked for me.

            Code:
            clear all
            
            sysuse auto, clear
            drop if foreign
            save auto_nf, replace
            
            sysuse auto, clear
            keep if foreign
            save auto_f, replace
            
            foreach file in auto_nf auto_f {
                use "`file'", clear
                qui reg mpg weight i.rep78 i.rep78#c.turn
                eststo
            }
            
            esttab, label
            Have you tried it without the "keep" option? I wonder if something funky is going on there.

            Comment

            Working...
            X