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

        Working...
        X