Announcement

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

  • Define nested loops for dependent and independent variables

    I have a panel data set from 1970 to 2014 on yields and prices for four different crops. I need to regress yield on prices and the controls for all four crops separately i.e run four regressions. The conrols are common in all the four crop regressions but the prices are cop specific.
    For this I need a loop to repeat the regression for the four crops and also to include the price specific to the crop which is the dependent variable
    Following several posts on statalist I was able to run the following loop

    Code:
    local m=1
    foreach Yield in Crop1 Crop2 Crop3 Crop4 {
                    foreach Price in Price1 Price2 Price3 Price4 {
                    local Control Control1 Control2 Control3
                    eststo model_`m': xtabond2 `Yield' L(1/2).`Yield' L.`Price'  `Control' i.Year, gmm(L.Yield `Price', eq(diff)laglimits(2 6)collapse) ///
                    gmm(L.Yield L.Price, eq(level)laglimits(1 4)) iv(L.Year, equation(level)) twostep small robust
                    local m `++m'
                    }
     }
    esttab model_*  , se(4) b(4) r(2)wide
    However this executes 16 regressions. One for each of the four dependent variables by including the independent variable i.e. price sequentially. So I have 16 regressions instead of four.
    Evidently something needs to be adjusted in my loop specifying the price variables. Any suggestions?

  • #2
    Right. What you want is a single loop on an index from 1 through 4 that then pulls out the corresponding yield and price variables. If the yields and price variables are really called Crop1 through Crop4 and Price1 through Price4, then it is utterly simple:

    Code:
    local Control Control1 Control2 Control3
    forvalues j = 1/4 {
        local Yield Crop`j'
        local Price Price`j'
       
    eststo model_`m': xtabond2 `Yield' L(1/2).`Yield' L.`Price' `Control' i.Year, gmm(L.Yield `Price', eq(diff) laglimits(2 6) collapse) /// gmm(L.Yield L.Price, eq(level)laglimits(1 4)) iv(L.Year, equation(level)) twostep small robust
    } esttab model_*, se(4) b(4) r(2) wide
    If those aren't the real names of those variables, then it's only slightly more complicated:
    Code:
    local crops wheat oats barley corn
    local prices ble avoine orge mais
    Code:
       
    local control control1 control2 control3
    forvalues j = 1/4 {
    local Yield: word `j' of crops
    local Price: word `j' of prices
    eststo model_`m': xtabond2 `Yield' L(1/2).`Yield' L.`Price' `Control' i.Year, /// gmm(L.Yield `Price', eq(diff) laglimits(2 6) collapse) /// gmm(L.Yield L.Price, eq(level) laglimits(1 4)) iv(L.Year, equation(level)) /// twostep small robust
    }
    esttab model_*, se(4) b(4) r(2) wide
    Added: For some reason, the editor keeps messing up my formatting of the code blocks, in particular splitting what should be one block into two. Whatever...the key thing is that the definitions of local macros crops and prices belong together with the definition of local macro control and all that follows. And all of it must be run together without interruption for the macros to work properly. After four tries, I've given up trying to fix the formatting.
    Last edited by Clyde Schechter; 29 Feb 2020, 00:01.

    Comment


    • #3

      Thank you for your answer. You are right my crops are specified as WHEAT, CPEA, CORN AND BARLEY.
      I run the code specified above.

      Code:
       
      local crops WHEATYIELD CPEAYIELD CORNYIELD BARLEYYIELD
      local prices WHEATPR CPEAPR CORNPR BARLEYPR
      local control control1 control2 control3
      forvalues j = 1/4 {
      local Yield: word `j' of crops
      local Price: word `j' of prices
      eststo model_`m': xtabond2 `Yield' L(1/2).`Yield' L.`Price' `control' i.Year, ///
      gmm(L.Yield `Price', eq(diff) laglimits(2 6) collapse) ///
      gmm(L.Yield L.Price, eq(level) laglimits(1 4)) iv(L.Year, equation(level)) ///
      twostep small robust
      However I get the error
      Code:
      variable crops not found
      Am I missing something?

      Comment


      • #4
        Apparently I was missing the quotes. I corrected the following an the command works.
        local Yield: word `j' of `crops' local Price: word `j' of `prices' Thanks once again Clyde

        Comment

        Working...
        X