Announcement

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

  • Issue with esttab and interaction terms in keep option

    Hi everyone,
    I’m facing an issue with the keep option in esttab when trying to include interaction terms generated dynamically in a loop. Here's what I'm doing: I have a list of variables stored in a local macro (variable). For each variable in the list, I generate an interaction term with another variable (dummy_child_aspiration) inside a foreach loop. I then estimate a probit regression for each variable and its interaction term. I save the results with eststo for each regression. After the loop, I use esttab to create a combined table, specifying the variables I want to keep with the keepoption:


    local variable "dummy_Q16a dummy_Q16b dummy_Q16c dummy_Q16d dummy_Q16e"

    foreach x of local variable {
    local clean_name = subinstr("`x'", "_", "", .)

    gen `x'_inter = `x' * dummy_child_aspiration

    eststo regr`clean_name': probit child_succeeds avg_skill_imp_index `x' `x'_inter dummy_child_wanted_succeed sex inter_firstborn_male dummy_first_born log_age_child dummy_child_aspiration dummy_importance_edu i.Macro_region dummy_sector_services_manu dummy_class_large dummy_female_ceo log_age_ceo nchildren, cluster(id)

    esttab regr`clean_name' using "$tables/basic_index_var.tex", replace ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    keep(avg_skill_imp_index dummy_child_wanted_succeed sex inter_firstborn_male log_age_child ///
    dummy_child_aspiration dummy_female_ceo dummy_importance_edu log_age_ceo nchildren `x' `x'_inter) ///
    compress scalars("N Observations") sfmt("%9.0fc")
    }

    esttab using "$tables/final_results_inter_aspiration1_Q16.tex", keep(avg_skill_imp_index dummy_child_wanted_succeed sex inter_firstborn_male log_age_child dummy_child_aspiration dummy_female_ceo dummy_importance_edu log_age_ceo nchildren `x' `x'_inter) addnotes("FE omitted: i.Macro_region dummy_sector_services_manu dummy_class_large") cells(b(star fmt(3)) se(par)) stats(N , fmt(3 3)) replace tex


    I then get this error: coefficient _inter not found r(111);

    However, if I omit the keep option, the table is generated correctly, and both the variable (\x') and its interaction term (`x'_inter`) appear in the output. What I suspect: The macro \x'_interis not expanded correctly withinkeep`. esttab may not recognize the dynamically generated variable names in keep.

    Question: How can I properly include the interaction terms dynamically in the keep option of esttab? Thank you for any suggestions!


  • #2
    Are you running the entire code in one go? If not, the local "x" will not be defined when executing the esttab command. See https://journals.sagepub.com/doi/10....36867X20931028 on the behavior of local macros. Note that estout is from SSC (FAQ Advice #12).

    Comment


    • #3
      Looking at #1 again, you want to define a list of interactions outside the loop and not a single interaction. Therefore, you cannot reference a single interaction. Here is some technique:

      Code:
      local variable "dummy_Q16a dummy_Q16b dummy_Q16c dummy_Q16d dummy_Q16e"
      
      local intervars
      foreach x of local variable {
          local clean_name = subinstr("`x'", "_", "", .)
          gen `x'_inter = `x' * dummy_child_aspiration
          local intervars `intervars' `x'_inter
          eststo regr`clean_name': probit child_succeeds avg_skill_imp_index `x' `x'_inter dummy_child_wanted_succeed sex inter_firstborn_male dummy_first_born log_age_child dummy_child_aspiration dummy_importance_edu i.Macro_region dummy_sector_services_manu dummy_class_large dummy_female_ceo log_age_ceo nchildren, cluster(id)
      
          esttab regr`clean_name' using "$tables/basic_index_var.tex", replace ///
          star(* 0.10 ** 0.05 *** 0.01) ///
          keep(avg_skill_imp_index dummy_child_wanted_succeed sex inter_firstborn_male log_age_child ///
          dummy_child_aspiration dummy_female_ceo dummy_importance_edu log_age_ceo nchildren `x' `x'_inter) ///
          compress scalars("N Observations") sfmt("%9.0fc")
      }
      
      esttab using "$tables/final_results_inter_aspiration1_Q16.tex", ///
      keep(avg_skill_imp_index dummy_child_wanted_succeed sex inter_firstborn_male ///
      log_age_child dummy_child_aspiration dummy_female_ceo dummy_importance_edu ///
      log_age_ceo nchildren `intervars') ///
      addnotes("FE omitted: i.Macro_region dummy_sector_services_manu dummy_class_large") ///
      cells(b(star fmt(3)) se(par)) stats(N , fmt(3 3)) replace tex

      Comment


      • #4
        Thank you so much for #3. I noticed that while, thanks to intervars, the interaction terms appear correctly in the table but the main variables (x)- respectively for each variable in local variable- do not show up. I have tried simply adding x in keep() for the final esttab command, but this seems to cause both x and the interaction terms (intervars) to disappear or have zero coefficients in the output.

        How can I properly fix that? Thanks!

        Comment


        • #5
          If you look at your code, `x' refers to an element of the local "variable".

          local variable "dummy_Q16a dummy_Q16b dummy_Q16c dummy_Q16d dummy_Q16e"
          In the first iteration, `x'= dummy_Q16a; second iteration, `x'= dummy_Q16b, and so on. By the time you exit the loop, `x' refers to the final element of the local "variable", i.e., dummy_Q16e. My local "intervars" collates the names of the interaction variables. For what you want, you just need to add the referenced `x' variables to this collation.

          Code:
          local intervars `intervars' `x' `x'_inter

          Comment

          Working...
          X