Announcement

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

  • Help with nested loops in regression and interaction terms

    Hi,
    I am trying to create a nested loop to run a series of interactions in a regression command, and I have the following questions:
    1. I have tried the following command and receive the error “invalid vce”
    local hetvar “prosociality intrinsic combined_motivation high_income pracdoc male”

    foreach t of local hetvar {
    eststo clear
    foreach y of global treatment {
    eststo, title("`y'"):reghdfe `y' nodrug_b nodrug_a `t’##(nodrug_b nodrug_a) if levelcode==2, vce(robust) absorb(diseasecode clinicid)
    su `y' if nodrug_b==0 & nodrug_a==0 & e(sample)==1
    estadd scalar conmean = `r(mean)'
    }
    }

    However, on running it in the following format, it runs fine

    eststo clear
    foreach y of global treatment {
    eststo, title("`y'"):reghdfe `y' nodrug_b nodrug_a prosociality##(nodrug_b nodrug_a) intrinsic##(nodrug_b nodrug_a)combined##(nodrug_b nodrug_a) ///
    high_income##(nodrug_b nodrug_a) pracdoc##(nodrug_b nodrug_a) male##(nodrug_b nodrug_a) if levelcode==2, vce(robust) absorb(diseasecode clinicid)
    su `y' if nodrug_b==0 & nodrug_a==0 & e(sample)==1
    estadd scalar conmean = `r(mean)'
    }

    Could you please tell me why this is happening? And how can I generate a nested loop where each term in the local "hetvar" with the variables "nodrug_b" "nodrug_a" in separate regressions.

    2. When outputting using esttab and the following commands, while it appears to run fine, there is no output file generated. How can I generate an output file for this?

    esttab using "$path/Output/Motivation/treat_motivation_thc.csv", b(%9.3fc) se(%9.3fc) ///
    starlevels( * 0.1 ** 0.05 *** 0.01) ar2(2) keep(nodrug_b nodrug_a 1.prosociality##(1.nodrug_b 1.nodrug_a)) intrinsic##(nodrug_b nodrug_a) ///
    combined##(nodrug_b nodrug_a) high_income##(nodrug_b nodrug_a) pracdoc##(nodrug_b nodrug_a) ///

    3. Is there anyway for me to keep only the valid interaction terms in the output file, i.e. 1.prosociality#1.nodrug_a. And how can I label these interaction terms in the output file?

    Many thanks,
    Karishma
    Last edited by Karishma DSouza; 13 Jul 2019, 23:40.

  • #2
    To address question #1, in the command
    Code:
    eststo, title("`y'"):reghdfe `y' nodrug_b nodrug_a `t##(nodrug_b nodrug_a) if levelcode==2, vce(robust) absorb(diseasecode clinicid)
    you will see that you used the wrong symbol in your reference to the local macro t. Try fixing that and seeing if the vce error problem persists, and if it solved, are your other questions resolved?

    Let me add a piece of advice to improve your future posts. Please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], as described in section 12 of the FAQ.

    In post #1, it would have improved the presentation had you copied the commands and the full error message including the return code from Stata's Results window into your post. It is not clear to me if the typographical error I found actually existed in the code you ran, or if you retyped your code into your post and in so doing, introduced the typographical error at that point. I don't quite understand the error message you got if the error actually was in your code, but I'm hoping that perhaps Stata got confused.


    Comment


    • #3
      Thank you, William. Apologies for not adhering to protocol.


      Unfortunately, I still get the same error about invalid vce. Below are the commands from the results window.


      Code:
      local hetvar "prosociality intrinsic combined_motivation income pracdoc male"
      
      eststo clear
      
      foreach t of local hetvar{
                               eststo clear
                               foreach y of global treatment {
                               eststo, title("`y'"):reghdfe `y' nodrug_b nodrug_a `t’##(nodrug_b nodrug_a) if levelcode==2, vce(robust) absorb(diseasecode clinicid 
                                        su `y' if nodrug_b==0 & nodrug_a==0 & e(sample)==1
                                                estadd scalar conmean = `r(mean)'
        }
        }
      
      invalid 'vce' 
      r(198);


      Comment


      • #4
        Unfortunately, you still have the same typographical error around your "t" local macro reference that I highlighted in post #2. Compare the symbol following the reference to t to the symbols following the references to y. They are not the same, and the ones after the references to y are correct.

        Comment


        • #5
          Thanks, William. That helped and this now works.

          Comment


          • #6
            Hello,

            I have daily data on energy consumption (“consumption”) of households (“house”) for two years.
            I regress the consumption on a policy change indicator (“change”, 1 or 0) for each month (“month”, 1…12).

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input int house float consumption byte change int year byte(month day)
            1 30.6 0 2017 1  1
            1 20.6 0 2017 1  2
            1 22.9 0 2017 1  3
            1 20.3 0 2017 1  4
            1 22.6 0 2017 1  5
            1 25.8 0 2017 1  6
            1 24.9 0 2017 1  7
            1 27.1 0 2017 1  8
            1 12.2 0 2017 1  9
            1 11.2 0 2017 1 10
            1 18.2 0 2017 1 11
            1 17.4 0 2017 1 12
            1 12.6 0 2017 1 13
            1 18.5 0 2017 1 14
            1 26.9 0 2017 1 15
            end
            So, I have a regression equation:
            reghdfe consumption i.change#i.month, noabsorb vce (cluster house)


            My task is to obtain the same results using a loop.

            I tried the following:
            forvalues m = 1/12 {
            reghdfe consumption i.change#i.month if month == `m', noabsorb vce (cluster house)
            }

            and the following (with a dummy for each month):
            tab month, gen(dum_month_)
            foreach m of varlist dum_month_* {
            reghdfe consumption change#`m', noabsorb vce (cluster house)
            }

            But the estimated coefficients were completely different from those for the original regression equation.

            I also tried this:
            levelsof month, local(levels)
            foreach m of local levels {
            reghdfe consumption i.change#i.`m', noabsorb vce (cluster house)
            }

            In that case, Stata gave the error “1 invalid name r(198)”.


            Could you please help me?

            Thank you.

            Comment

            Working...
            X