Announcement

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

  • #16
    Thank you, Andrew , for one more time!

    Sorry if I am insisting too much, but I am not getting the last part
    So, given that in my case
    nlcom -_b[indicator]/(2*_b[indicator#indicator])
    I wrote the code as follows in order to get 1-a .


    foreach var of varlist decade?{
    xtset id ts
    xtregar D.gdp cpi u dummy1 dummy2 c.indicator##c.indicator if `var', fe
    // get the x where the minimum occurs
    local x = -_b[indicator]/(2*_b[indicator1#indicator])
    // get the y for that minimim
    qui margins, at(indicator = `x' cpi=0 u=2 dummy1=0 dummy2=0 indicator=-4)
    // the predictions are stored in the matrix r(b)
    // el(r(b),1,1) extracts the cell 1,1 from the matrix r(b)
    local y = el(r(b),1,1)
    gen indmax2_`var'= `x'
    gen gdpmax2_`var'=`y'
    gen a_`var' = -_b[indicator]/(2*_b[indicator#indicator])
    gen dep_`var' = 1-a

    // prepare for our plot
    nlcom -_b[indicator]/(2*_b[indicator#indicator])

    qui margins, at(indicator=(-16(4)29) cpi=0 u=2 dummy1=0 dummy2=0 indicator=-4)
    // with scatteri we can add a point and a label to our graph
    marginsplot, noci plotopts(msymbol(i)) legend(off) ///
    addplot(scatteri `y' `x' (12) "(`xlab'; `ylab')") ///
    ylab(,format(%9.3fc) angle(0)) ytitle("predicted gdp ") title("Predicted Marings pair")
    }
    It does work for the entire sample, but again It fails when I add the if option, in the regression .I get no observation. What did I do wrong?
    It is similar to what I had referred in #14
    Last edited by Giorgio Di Stefano; 18 Mar 2022, 19:23.

    Comment


    • #17
      Exit the loop and investigate why the regression is failing, e.g., by running

      Code:
      xtset id ts
      xtregar D.gdp cpi u dummy1 dummy2 c.indicator##c.indicator if decade1, fe

      You are using a panel data estimator and you have a differenced variable. Differencing results in the loss of one cross-section assuming a balanced panel as \(\Delta \text{ GDP}_t\;= \text{ GDP}_t\;-\;\text{ GDP}_{t-1}\), or the current level of GDP minus its one-period lag. For a fixed effects model, you need a minimum of 2 observations, so with the differencing, this becomes 3. With an unbalanced panel, perhaps you are left with no observations in some decades after differencing. Or it could be something else.

      Comment


      • #18
        Originally posted by Andrew Musau View Post
        Exit the loop and investigate why the regression is failing, e.g., by running

        Code:
        xtset id ts
        xtregar D.gdp cpi u dummy1 dummy2 c.indicator##c.indicator if decade1, fe

        You are using a panel data estimator and you have a differenced variable. Differencing results in the loss of one cross-section assuming a balanced panel as \(\Delta \text{ GDP}_t\;= \text{ GDP}_t\;-\;\text{ GDP}_{t-1}\), or the current level of GDP minus its one-period lag. For a fixed effects model, you need a minimum of 2 observations, so with the differencing, this becomes 3. With an unbalanced panel, perhaps you are left with no observations in some decades after differencing. Or it could be something else.
        Now I got it! I am so grateful to you for your help and time!
        May I add a last query before endings this thread.
        How can I include in the loop a margins save plot for each graph produced, and secondly produce pubbication tables by entire period, subperiods, subgroups etc, including the
        (1-a) defined as 1-nlcom in #16
        Thank you so much

        Comment


        • #19
          For saving and exporting graphs, see

          Code:
          help gr export
          You need to name the graphs differently so that some are not overwritten. For storing estimates, see

          Code:
          help estimates store
          You can use the same prefixes to make things easier, e.g.,

          Code:
          foreach var of varlist decade?{
          xtset id ts
          ...
          xtregar D.gdp cpi u dummy1 dummy2 c.indicator##c.indicator if `var', fe
          est store est_`var'
          ...
          marginsplot, noci plotopts(msymbol(i)) legend(off) ///
          addplot(scatteri `y' `x' (12) "(`xlab'; `ylab')") ///
          ylab(,format(%9.3fc) angle(0)) ytitle("predicted gdp ") title("Predicted Marings pair: `var'")
          gr export gr_`var', as(png) replace
          }
          Then with a command such as estout from SSC

          Code:
          esttab est*

          Comment


          • #20
            Originally posted by Andrew Musau View Post
            For saving and exporting graphs, see

            Code:
            help gr export
            You need to name the graphs differently so that some are not overwritten. For storing estimates, see

            Code:
            help estimates store
            You can use the same prefixes to make things easier, e.g.,

            Code:
            foreach var of varlist decade?{
            xtset id ts
            ...
            xtregar D.gdp cpi u dummy1 dummy2 c.indicator##c.indicator if `var', fe
            est store est_`var'
            ...
            marginsplot, noci plotopts(msymbol(i)) legend(off) ///
            addplot(scatteri `y' `x' (12) "(`xlab'; `ylab')") ///
            ylab(,format(%9.3fc) angle(0)) ytitle("predicted gdp ") title("Predicted Marings pair: `var'")
            gr export gr_`var', as(png) replace
            }
            Then with a command such as estout from SSC

            Code:
            esttab est*
            Thank you very much, Andrew! I had a such thing in mind, ;but was not sure about the loop. Really, you helped me a lot. Two last thing easy to answers, I promise.

            1. I need to derive (collapse) the weighted average of their each entire period and by groups, and then for each decade and for each group. Including the (1-a) values defined as 1-nlcom in #16

            and

            2. Will the loop still work if I substitute the indicators with a local say called ind

            local Ind indicator1 indicator2 etc


            as I have 12 of them, and some have different names? Same question for the margins

            Comment


            • #21
              I do not follow #1. As I said, I do not understand what you are doing. You can post in a new thread and see if there is anyone that can follow. For #2, yes, you can store variables in locals and call them in a loop. Here is one way to do it:

              Code:
              sysuse auto, clear
              set seed 03222022
              forval i=1/4{
                  gen indicator`i'= runiformint(0,1)
                  gen dummy`i'= runiformint(0,1)
              }
              local indicators indicator1 indicator2 indicator3 indicator4
              local dummies dummy1 dummy2 dummy3 dummy4
              foreach var in `indicators' `dummies' {
                  display "Regression for `var'"
                  regress mpg weight if `var'
              }
              Res.:

              Code:
              Regression for indicator1
              
                    Source |       SS           df       MS      Number of obs   =        37
              -------------+----------------------------------   F(1, 35)        =     78.03
                     Model |  812.742905         1  812.742905   Prob > F        =    0.0000
                  Residual |  364.554393        35  10.4158398   R-squared       =    0.6903
              -------------+----------------------------------   Adj R-squared   =    0.6815
                     Total |   1177.2973        36  32.7027027   Root MSE        =    3.2274
              
              ------------------------------------------------------------------------------
                       mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
                    weight |  -.0061288   .0006938    -8.83   0.000    -.0075373   -.0047203
                     _cons |   39.84612   2.118409    18.81   0.000     35.54552    44.14672
              ------------------------------------------------------------------------------
              Regression for indicator2
              
                    Source |       SS           df       MS      Number of obs   =        29
              -------------+----------------------------------   F(1, 27)        =     55.79
                     Model |  641.898254         1  641.898254   Prob > F        =    0.0000
                  Residual |   310.65347        27  11.5056841   R-squared       =    0.6739
              -------------+----------------------------------   Adj R-squared   =    0.6618
                     Total |  952.551724        28  34.0197044   Root MSE        =     3.392
              
              ------------------------------------------------------------------------------
                       mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
                    weight |  -.0056985   .0007629    -7.47   0.000    -.0072639   -.0041331
                     _cons |   39.68576   2.365498    16.78   0.000     34.83216    44.53936
              ------------------------------------------------------------------------------
              Regression for indicator3
              
                    Source |       SS           df       MS      Number of obs   =        40
              -------------+----------------------------------   F(1, 38)        =     58.11
                     Model |  858.289674         1  858.289674   Prob > F        =    0.0000
                  Residual |  561.310326        38  14.7713244   R-squared       =    0.6046
              -------------+----------------------------------   Adj R-squared   =    0.5942
                     Total |      1419.6        39        36.4   Root MSE        =    3.8433
              
              ------------------------------------------------------------------------------
                       mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
                    weight |  -.0063738   .0008362    -7.62   0.000    -.0080666   -.0046811
                     _cons |   40.89364   2.603014    15.71   0.000     35.62411    46.16316
              ------------------------------------------------------------------------------
              Regression for indicator4
              
                    Source |       SS           df       MS      Number of obs   =        40
              -------------+----------------------------------   F(1, 38)        =     69.10
                     Model |   729.01922         1   729.01922   Prob > F        =    0.0000
                  Residual |   400.88078        38  10.5494942   R-squared       =    0.6452
              -------------+----------------------------------   Adj R-squared   =    0.6359
                     Total |      1129.9        39  28.9717949   Root MSE        =     3.248
              
              ------------------------------------------------------------------------------
                       mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
                    weight |  -.0058179   .0006999    -8.31   0.000    -.0072347   -.0044011
                     _cons |   38.79975   2.313221    16.77   0.000     34.11688    43.48262
              ------------------------------------------------------------------------------
              Regression for dummy1
              
                    Source |       SS           df       MS      Number of obs   =        40
              -------------+----------------------------------   F(1, 38)        =     68.35
                     Model |  929.289738         1  929.289738   Prob > F        =    0.0000
                  Residual |  516.685262        38  13.5969806   R-squared       =    0.6427
              -------------+----------------------------------   Adj R-squared   =    0.6333
                     Total |    1445.975        39  37.0762821   Root MSE        =    3.6874
              
              ------------------------------------------------------------------------------
                       mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
                    weight |  -.0062697   .0007584    -8.27   0.000     -.007805   -.0047344
                     _cons |   40.58582   2.354745    17.24   0.000     35.81888    45.35275
              ------------------------------------------------------------------------------
              Regression for dummy2
              
                    Source |       SS           df       MS      Number of obs   =        32
              -------------+----------------------------------   F(1, 30)        =     39.30
                     Model |  583.545776         1  583.545776   Prob > F        =    0.0000
                  Residual |  445.422974        30  14.8474325   R-squared       =    0.5671
              -------------+----------------------------------   Adj R-squared   =    0.5527
                     Total |  1028.96875        31  33.1925403   Root MSE        =    3.8532
              
              ------------------------------------------------------------------------------
                       mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
                    weight |  -.0054957   .0008766    -6.27   0.000     -.007286   -.0037054
                     _cons |   37.93845   2.791229    13.59   0.000       32.238     43.6389
              ------------------------------------------------------------------------------
              Regression for dummy3
              
                    Source |       SS           df       MS      Number of obs   =        39
              -------------+----------------------------------   F(1, 37)        =     77.01
                     Model |  466.687495         1  466.687495   Prob > F        =    0.0000
                  Residual |  224.235582        37  6.06042113   R-squared       =    0.6755
              -------------+----------------------------------   Adj R-squared   =    0.6667
                     Total |  690.923077        38  18.1821862   Root MSE        =    2.4618
              
              ------------------------------------------------------------------------------
                       mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
                    weight |  -.0045875   .0005228    -8.78   0.000    -.0056467   -.0035282
                     _cons |   34.61545   1.685958    20.53   0.000     31.19937    38.03152
              ------------------------------------------------------------------------------
              Regression for dummy4
              
                    Source |       SS           df       MS      Number of obs   =        34
              -------------+----------------------------------   F(1, 32)        =     55.05
                     Model |  843.788171         1  843.788171   Prob > F        =    0.0000
                  Residual |  490.447123        32  15.3264726   R-squared       =    0.6324
              -------------+----------------------------------   Adj R-squared   =    0.6209
                     Total |  1334.23529        33  40.4313725   Root MSE        =    3.9149
              
              ------------------------------------------------------------------------------
                       mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
              -------------+----------------------------------------------------------------
                    weight |  -.0065315   .0008803    -7.42   0.000    -.0083246   -.0047385
                     _cons |   41.61033   2.650133    15.70   0.000     36.21219    47.00848
              ------------------------------------------------------------------------------
              
              .

              Comment


              • #22
                Originally posted by Andrew Musau View Post
                I do not follow #1. As I said, I do not understand what you are doing. You can post in a new thread and see if there is anyone that can follow. For #2, yes, you can store variables in locals and call them in a loop. Here is one way to do it:

                Code:
                sysuse auto, clear
                set seed 03222022
                forval i=1/4{
                gen indicator`i'= runiformint(0,1)
                gen dummy`i'= runiformint(0,1)
                }
                local indicators indicator1 indicator2 indicator3 indicator4
                local dummies dummy1 dummy2 dummy3 dummy4
                foreach var in `indicators' `dummies' {
                display "Regression for `var'"
                regress mpg weight if `var'
                }
                Res.:

                Code:
                Regression for indicator1
                
                Source | SS df MS Number of obs = 37
                -------------+---------------------------------- F(1, 35) = 78.03
                Model | 812.742905 1 812.742905 Prob > F = 0.0000
                Residual | 364.554393 35 10.4158398 R-squared = 0.6903
                -------------+---------------------------------- Adj R-squared = 0.6815
                Total | 1177.2973 36 32.7027027 Root MSE = 3.2274
                
                ------------------------------------------------------------------------------
                mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                weight | -.0061288 .0006938 -8.83 0.000 -.0075373 -.0047203
                _cons | 39.84612 2.118409 18.81 0.000 35.54552 44.14672
                ------------------------------------------------------------------------------
                Regression for indicator2
                
                Source | SS df MS Number of obs = 29
                -------------+---------------------------------- F(1, 27) = 55.79
                Model | 641.898254 1 641.898254 Prob > F = 0.0000
                Residual | 310.65347 27 11.5056841 R-squared = 0.6739
                -------------+---------------------------------- Adj R-squared = 0.6618
                Total | 952.551724 28 34.0197044 Root MSE = 3.392
                
                ------------------------------------------------------------------------------
                mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                weight | -.0056985 .0007629 -7.47 0.000 -.0072639 -.0041331
                _cons | 39.68576 2.365498 16.78 0.000 34.83216 44.53936
                ------------------------------------------------------------------------------
                Regression for indicator3
                
                Source | SS df MS Number of obs = 40
                -------------+---------------------------------- F(1, 38) = 58.11
                Model | 858.289674 1 858.289674 Prob > F = 0.0000
                Residual | 561.310326 38 14.7713244 R-squared = 0.6046
                -------------+---------------------------------- Adj R-squared = 0.5942
                Total | 1419.6 39 36.4 Root MSE = 3.8433
                
                ------------------------------------------------------------------------------
                mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                weight | -.0063738 .0008362 -7.62 0.000 -.0080666 -.0046811
                _cons | 40.89364 2.603014 15.71 0.000 35.62411 46.16316
                ------------------------------------------------------------------------------
                Regression for indicator4
                
                Source | SS df MS Number of obs = 40
                -------------+---------------------------------- F(1, 38) = 69.10
                Model | 729.01922 1 729.01922 Prob > F = 0.0000
                Residual | 400.88078 38 10.5494942 R-squared = 0.6452
                -------------+---------------------------------- Adj R-squared = 0.6359
                Total | 1129.9 39 28.9717949 Root MSE = 3.248
                
                ------------------------------------------------------------------------------
                mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                weight | -.0058179 .0006999 -8.31 0.000 -.0072347 -.0044011
                _cons | 38.79975 2.313221 16.77 0.000 34.11688 43.48262
                ------------------------------------------------------------------------------
                Regression for dummy1
                
                Source | SS df MS Number of obs = 40
                -------------+---------------------------------- F(1, 38) = 68.35
                Model | 929.289738 1 929.289738 Prob > F = 0.0000
                Residual | 516.685262 38 13.5969806 R-squared = 0.6427
                -------------+---------------------------------- Adj R-squared = 0.6333
                Total | 1445.975 39 37.0762821 Root MSE = 3.6874
                
                ------------------------------------------------------------------------------
                mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                weight | -.0062697 .0007584 -8.27 0.000 -.007805 -.0047344
                _cons | 40.58582 2.354745 17.24 0.000 35.81888 45.35275
                ------------------------------------------------------------------------------
                Regression for dummy2
                
                Source | SS df MS Number of obs = 32
                -------------+---------------------------------- F(1, 30) = 39.30
                Model | 583.545776 1 583.545776 Prob > F = 0.0000
                Residual | 445.422974 30 14.8474325 R-squared = 0.5671
                -------------+---------------------------------- Adj R-squared = 0.5527
                Total | 1028.96875 31 33.1925403 Root MSE = 3.8532
                
                ------------------------------------------------------------------------------
                mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                weight | -.0054957 .0008766 -6.27 0.000 -.007286 -.0037054
                _cons | 37.93845 2.791229 13.59 0.000 32.238 43.6389
                ------------------------------------------------------------------------------
                Regression for dummy3
                
                Source | SS df MS Number of obs = 39
                -------------+---------------------------------- F(1, 37) = 77.01
                Model | 466.687495 1 466.687495 Prob > F = 0.0000
                Residual | 224.235582 37 6.06042113 R-squared = 0.6755
                -------------+---------------------------------- Adj R-squared = 0.6667
                Total | 690.923077 38 18.1821862 Root MSE = 2.4618
                
                ------------------------------------------------------------------------------
                mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                weight | -.0045875 .0005228 -8.78 0.000 -.0056467 -.0035282
                _cons | 34.61545 1.685958 20.53 0.000 31.19937 38.03152
                ------------------------------------------------------------------------------
                Regression for dummy4
                
                Source | SS df MS Number of obs = 34
                -------------+---------------------------------- F(1, 32) = 55.05
                Model | 843.788171 1 843.788171 Prob > F = 0.0000
                Residual | 490.447123 32 15.3264726 R-squared = 0.6324
                -------------+---------------------------------- Adj R-squared = 0.6209
                Total | 1334.23529 33 40.4313725 Root MSE = 3.9149
                
                ------------------------------------------------------------------------------
                mpg | Coef. Std. Err. t P>|t| [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                weight | -.0065315 .0008803 -7.42 0.000 -.0083246 -.0047385
                _cons | 41.61033 2.650133 15.70 0.000 36.21219 47.00848
                ------------------------------------------------------------------------------
                
                .
                Thank you again for everything, Andrew! I truly appreciate your help and time!
                Thanks a ton!

                Giorgio!

                Comment

                Working...
                X