Announcement

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

  • Regress for each company by using "foreach" or "forvalues" command

    Hi there,

    My panel data set includes 389 companies and 51 quarter years. I am trying to regress my regression for each company and to save coefficients. If I regress regression as manual, Stata produces coefficients. For example;

    Code:
    // for company 1 :
    
    nlsur (l_q=ln({A})+({s}/(1-{s}))*(ln(({a1}*K^(({s}-1)/{s}))+((1-{a1})*((exp({d}))*L))^(({s}-1)/{s})))) (l_KY=ln({a1}/(1+{mu}))+(({s}-1)/{s})*(ln(Y/K)-ln({A}))) (l_LY=ln((1-{a1})/(1+{mu}))+(({s}-1)/{s})*(ln(Y/L)-ln({A})-{d})), initial(i), if id==1
    
    // for company 2 :
    
    nlsur (l_q=ln({A})+({s}/(1-{s}))*(ln(({a1}*K^(({s}-1)/{s}))+((1-{a1})*((exp({d}))*L))^(({s}-1)/{s})))) (l_KY=ln({a1}/(1+{mu}))+(({s}-1)/{s})*(ln(Y/K)-ln({A}))) (l_LY=ln((1-{a1})/(1+{mu}))+(({s}-1)/{s})*(ln(Y/L)-ln({A})-{d})), initial(i), if id==2
    where id is company id and id =(1,2,....,389)

    However, I do not want to do that for 389 companies. I want to use "foreach" or "forvalues" loop commands. So I am trying to run this code:

    Code:
    gen coeff= . // empty variable for coefficient
    
    levelsof id, local(levels) // id is company id
    foreach i of local levels{
        
        nlsur (l_q=ln({A})+({s}/(1-{s}))*(ln(({a1}*K^(({s}-1)/{s}))+((1-{a1})*((exp({d}))*L))^(({s}-1)/{s})))) (l_KY=ln({a1}/(1+{mu}))+(({s}-1)/{s})*(ln(Y/K)-ln({A}))) (l_LY=ln((1-{a1})/(1+{mu}))+(({s}-1)/{s})*(ln(Y/L)-ln({A})-{d})), initial(i), if id=='i'
        
        replace coeff= _b[A] if id=='i'
    }
    But I received "i invalid name" error.

    What is my mistake? How can I solve this problem? Can I generate a result variable like that:
    Firm ID A a1 s mu
    1 .. .. .. ..
    2 .. .. .. ..
    Thank you for your interest.


    Error:


    Click image for larger version

Name:	error.png
Views:	1
Size:	131.9 KB
ID:	1638576
    Last edited by Necip Bulut; 29 Nov 2021, 04:13.

  • #2
    To indicate looping through i, it is not
    Code:
    'i'
    It is:
    Code:
    `i'
    The first symbol is not a single quote, but a grave accent. It is usually next to "1" at the top row of the keyboard. The last symbol is single quote.

    Comment

    Working...
    X