Announcement

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

  • foreach loop with fe (fixed effects) and constraints

    Hi

    I want to run fixed effects regression together with some restrictions(constraints) for 250 cross-sections that I have. the data is unbalanced quarterly from year 2005-2019. I want to run this regression individually for each cross-section i..e is should be a regression of "rimes-series of cross-sections".

    I am not able to specify fe with cnsreg for constraints regression.

    Any help should be appreciated

  • #2
    You cannot talk about fixed effects in the context of a cross-section since you have data for each individual/firm/country at one point in time. You need data for at least two periods. In that case, you can add dummies and use cnsreg to run a constrained fixed effects regression.

    Comment


    • #3
      Dear Andrew

      Thanks for you reply. As I mentioned above, it is time-series of cross sections, it is not one point in time. I have quarterly observations for each cross-section/panel ID. But I don't want to run FE regression on the entire panel but on each IDs individually in a loop.

      Comment


      • #4
        Just to clear up terminology: If I have many firms observed at a single point in time, then I have cross-sectional data. On the other hand, if I have one firm observed over several time periods, then I have time series data. So what you describe in #3 is the latter. Similar to the cross-sectional case, you cannot talk about firm and time fixed effects in the context of a time series. The reason, again, is that you have only one set of observations at a given period (you need more than one to estimate time effects). Therefore, you need panel data (at least 2 firms, at least 2 years) to run a fixed effects regression.
        Last edited by Andrew Musau; 14 Feb 2020, 07:42.

        Comment


        • #5
          Yes Andrew I have a panel data, but I want to obtain estimates for individual IDs in a panel at each point in time, which I believe can be done by running individual regressions for each ID using foreach loop. I want to do this using FE and constraints.

          To brief again, I have a panel of 250 firms, with quarterly observations from year 2005-2019.

          I hope I tried to make this clear now.

          Comment


          • #6
            estimates for individual IDs in a panel at each point in time
            This refers to a single observation to me. If you mean obtain estimates for individual IDs (i.e. estimate a coefficient for each time series), then yes, you can run OLS regressions. In this case, the intercept is firm specific, so the concept of fixed effects does not exist.

            Code:
            webuse grunfeld, clear
            constraint 1 mvalue = 0.12
            bys firm:  cnsreg invest mvalue kstock, c(1)
            Res.: (part)

            Code:
            . bys company: cnsreg invest mvalue kstock, c(1)
            
            ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            -> company = 1
            
            Constrained linear regression                   Number of obs     =         20
                                                            Root MSE          =    89.1978
            
             ( 1)  mvalue = .12
            ------------------------------------------------------------------------------
                  invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
                  mvalue |        .12  (constrained)
                  kstock |   .3709977   .0324731    11.42   0.000     .3027743    .4392212
                   _cons |  -152.6093   29.00338    -5.26   0.000    -213.5432   -91.67547
            ------------------------------------------------------------------------------
            
            ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
            -> company = 2
            
            Constrained linear regression                   Number of obs     =         20
                                                            Root MSE          =    95.2122
            
             ( 1)  mvalue = .12
            ------------------------------------------------------------------------------
                  invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
                  mvalue |        .12  (constrained)
                  kstock |   .4042616   .1391999     2.90   0.009     .1118135    .6967096
                   _cons |   54.65746   46.23699     1.18   0.253    -42.48285    151.7978
            ------------------------------------------------------------------------------
            
            ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

            Comment


            • #7
              Thanks Andrew

              Can we do something like you mentioned in #2. By introducing dummies and then running cnsreg with constraints. Can you please show how one can achieve that?

              Comment


              • #8
                This works only with panel data.

                Code:
                webuse grunfeld, clear
                
                *CONSTRAINED LSDV (WITH FIRM EFFECTS)
                constraint 1 mvalue = 0.12
                cnsreg invest mvalue kstock i.company, c(1)
                
                *CONSTRAINED LSDV (WITH TIME EFFECTS)
                cnsreg invest mvalue kstock i.year, c(1)
                
                *CONSTRAINED LSDV (WITH FIRM EFFECTS AND TIME EFFECTS)
                cnsreg invest mvalue kstock i.company i.year, c(1)
                Res.:

                Code:
                 
                . *CONSTRAINED LSDV (WITH FIRM EFFECTS)
                
                . 
                . constraint 1 mvalue = 0.12
                
                . 
                . cnsreg invest mvalue kstock i.company, c(1)
                
                Constrained linear regression                   Number of obs     =        200
                                                                Root MSE          =    52.7252
                
                 ( 1)  mvalue = .12
                ------------------------------------------------------------------------------
                      invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                      mvalue |        .12  (constrained)
                      kstock |    .304623   .0160646    18.96   0.000     .2729341    .3363119
                             |
                     company |
                          2  |    193.606   17.61415    10.99   0.000     158.8604    228.3516
                          3  |  -142.9973   17.14358    -8.34   0.000    -176.8147     -109.18
                          4  |    75.5739   18.70081     4.04   0.000     38.68478     112.463
                          5  |  -4.684098   16.87424    -0.28   0.782    -37.97014    28.60195
                          6  |   82.82921   18.82576     4.40   0.000      45.6936    119.9648
                          7  |   43.25083   17.51275     2.47   0.014     8.705272    77.79638
                          8  |   45.86401    18.9667     2.42   0.017      8.45039    83.27762
                          9  |   20.67343   17.59844     1.17   0.242    -14.04117    55.38803
                         10  |   102.3337   19.60933     5.22   0.000     63.65241     141.015
                             |
                       _cons |  -109.5696   15.73238    -6.96   0.000    -140.6032     -78.536
                ------------------------------------------------------------------------------
                
                . 
                . 
                . 
                . *CONSTRAINED LSDV (WITH TIME EFFECTS)
                
                . 
                . cnsreg invest mvalue kstock i.year, c(1)
                
                Constrained linear regression                   Number of obs     =        200
                                                                Root MSE          =    97.8950
                
                 ( 1)  mvalue = .12
                ------------------------------------------------------------------------------
                      invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                      mvalue |        .12  (constrained)
                      kstock |   .2109728   .0272352     7.75   0.000     .1572295    .2647161
                             |
                        year |
                       1936  |  -18.30004   43.78116    -0.42   0.676    -104.6937    68.09356
                       1937  |  -36.20182    43.7939    -0.83   0.410    -122.6206    50.21693
                       1938  |  -28.21668   43.83029    -0.64   0.521    -114.7072    58.27387
                       1939  |  -56.62254   43.85353    -1.29   0.198     -143.159    29.91388
                       1940  |  -31.02771    43.8588    -0.71   0.480    -117.5745     55.5191
                       1941  |  -2.845272   43.89093    -0.06   0.948    -89.45548    83.76494
                       1942  |  -.7413994   43.94929    -0.02   0.987    -87.46676    85.98397
                       1943  |  -21.40103   43.97348    -0.49   0.627    -108.1741    65.37208
                       1944  |  -21.83574   43.96935    -0.50   0.620    -108.6007    64.92922
                       1945  |  -33.61125    43.9881    -0.76   0.446    -120.4132     53.1907
                       1946  |  -7.121208   44.02806    -0.16   0.872    -94.00201     79.7596
                       1947  |  -3.805042   44.28801    -0.09   0.932    -91.19881    83.58872
                       1948  |  -2.022456   44.47078    -0.05   0.964    -89.77689    85.73197
                       1949  |  -26.65344   44.64988    -0.60   0.551    -114.7613    61.45442
                       1950  |  -26.22871   44.76095    -0.59   0.559    -114.5557    62.09831
                       1951  |  -10.09332   44.89015    -0.22   0.822     -98.6753    78.48865
                       1952  |  -3.666731    45.2731    -0.08   0.936    -93.00438    85.67092
                       1953  |   4.371608   45.86975     0.10   0.924    -86.14342    94.88663
                       1954  |  -9.564463   46.56799    -0.21   0.838    -101.4573     82.3284
                             |
                       _cons |  -25.29624   31.00359    -0.82   0.416    -86.47579    35.88332
                ------------------------------------------------------------------------------
                
                . 
                . 
                . 
                . *CONSTRAINED LSDV (WITH FIRM EFFECTS AND TIME EFFECTS)
                
                . 
                . cnsreg invest mvalue kstock i.company i.year, c(1)
                
                Constrained linear regression                   Number of obs     =        200
                                                                Root MSE          =    51.5764
                
                 ( 1)  mvalue = .12
                ------------------------------------------------------------------------------
                      invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                      mvalue |        .12  (constrained)
                      kstock |   .3568319   .0216985    16.45   0.000     .3139987    .3996651
                             |
                     company |
                          2  |    212.066   18.02427    11.77   0.000     176.4858    247.6462
                          3  |  -130.0352   17.17656    -7.57   0.000     -163.942   -96.12836
                          4  |   103.0979   19.92155     5.18   0.000     63.77242    142.4234
                          5  |   3.756508   16.68287     0.23   0.822    -29.17576    36.68878
                          6  |   111.2387   20.13512     5.52   0.000      71.4916    150.9857
                          7  |   60.66196   17.84307     3.40   0.001     25.43943    95.88449
                          8  |   75.24689   20.37499     3.69   0.000     35.02631    115.4675
                          9  |   38.97446   17.99624     2.17   0.032     3.449576    74.49934
                         10  |   135.8775   21.45618     6.33   0.000     93.52268    178.2324
                             |
                        year |
                       1936  |  -20.03445    23.0671    -0.87   0.386    -65.56929    25.50039
                       1937  |  -42.11888   23.08245    -1.82   0.070    -87.68402    3.446255
                       1938  |  -39.46183   23.12624    -1.71   0.090    -85.11342    6.189756
                       1939  |  -70.22069   23.15419    -3.03   0.003    -115.9275   -24.51392
                       1940  |  -45.10471   23.16053    -1.95   0.053    -90.82398    .6145511
                       1941  |  -19.54934   23.19913    -0.84   0.401    -65.34481    26.24612
                       1942  |  -21.38235   23.26915    -0.92   0.359    -67.31604    24.55134
                       1943  |  -43.47067   23.29815    -1.87   0.064    -89.46161    2.520257
                       1944  |  -43.66807    23.2932    -1.87   0.063    -89.64923    2.313095
                       1945  |  -56.50106   23.31565    -2.42   0.016    -102.5265   -10.47557
                       1946  |  -32.11751   23.36348    -1.37   0.171     -78.2374    14.00238
                       1947  |  -39.62802   23.67328    -1.67   0.096    -86.35948    7.103436
                       1948  |  -43.83849   23.88978    -1.84   0.068    -90.99732    3.320332
                       1949  |  -73.62574    24.1009    -3.05   0.003    -121.2013   -26.05016
                       1950  |   -76.1408   24.23131    -3.14   0.002    -123.9738   -28.30779
                       1951  |  -63.22933   24.38255    -2.59   0.010    -111.3609   -15.09777
                       1952  |  -65.42243   24.82791    -2.64   0.009    -114.4331   -16.41171
                       1953  |  -68.93248   25.51358    -2.70   0.008    -119.2967   -18.56824
                       1954  |   -94.5628   26.30421    -3.59   0.000    -146.4878   -42.63785
                             |
                       _cons |  -95.47319   21.78757    -4.38   0.000    -138.4822   -52.46417
                ------------------------------------------------------------------------------
                
                .

                Comment


                • #9
                  Hi Andrew

                  Thanks again for the elaborative explanation. However, I am getting really confused around how to run cnsreg for individual firms in a panel data (over quarterly observations from year 2005-2019). I will give the constraints and the cnsreg command that I am trying to run here:

                  constraint 1 c_var1+c_var2=1
                  constraint 2 c_var3+c_var4 =0

                  (c_var are the coefficients of the respective variables vars)


                  capture program drop one_id
                  program define one_id
                  cnsreg lnDep_var lnvar1 lnvar2 lnvar3 lnvar4, c(1-2)
                  foreach v of varlist lnvar1 lnvar2 lnvar3 lnvar4{
                  gen c_`v' = _b[`v']
                  gen se_`v' = _se[`v']
                  gen t_`v' = c_`v'/s_`v'
                  gen p_`v' = 2*t(e(df_r), -abs( t_`v'))
                  }
                  exit
                  end
                  runby one_id, by(FI) status

                  when I run these commands with 'runby one_id, by(FI) verbose', the errors shown are:

                  (note: constraint number 1 caused error r(111))
                  (note: constraint number 2 caused error r(198))
                  matrix e(Cns) not found
                  lnW12_PLC_w omitted because of collinearity
                  note: lnvar1 omitted because of collinearity
                  note: lnvar2 omitted because of collinearity
                  note: lnvar3 omitted because of collinearity


                  I want to know how to resolve these errors to obtain the coefficients of the variables for each firm at each tie-period through this constraint regression.
                  I must say that I am able to run this simple regression without constraint in this loop, but with constraints it is not getting me anywhere.

                  Thanking in anticipation.

                  Comment


                  • #10
                    .
                    Last edited by Aamina Khurraa; 15 Feb 2020, 14:00.

                    Comment


                    • #11
                      Hi

                      I have sorted the constraints issue and used the following syntax with fixed effects. But not sure if this makes sense as is is mentioned that FE is suitable in panel data. Although I have panel data but I am running individual cnsreg for each panel ID. Hereis the code:

                      constraint define _b[var1+var2]=1
                      constraint define _b[var3+var4]=0

                      (c_var are the coefficients of the respective variables (vars))


                      capture program drop one_id
                      program define one_id
                      cnsreg logDep_var logvar1 logvar2 logvar3 logvar4 i.ID, c(1-2)
                      foreach v of varlist lnvar1 lnvar2 lnvar3 lnvar4{
                      gen c_`v' = _b[`v']
                      gen se_`v' = _se[`v']
                      gen t_`v' = c_`v'/s_`v'
                      gen p_`v' = 2*t(e(df_r), -abs( t_`v'))
                      }
                      exit
                      end
                      runby one_id, by(FI) status

                      Out of 250 IDs, I got error in 79, however got the convergence for the rest of groups. I am inquisitive if fe (i.ID) is meaningful here? and what difference it makes with the cnsreg without fe here?

                      Comment


                      • #12
                        I am inquisitive if fe (i.ID) is meaningful here? and what difference it makes with the cnsreg without fe here?
                        No (explained in #6, read again!), you will just end up with the OLS regression because you have just 1 ID. See below:

                        Code:
                        webuse grunfeld
                        constraint def 1 mvalue+kstock=1
                        cnsreg invest mvalue kstock if company==1, c(1)
                        cnsreg invest mvalue kstock i.company if company==1, c(1)
                        Res.:

                        Code:
                        . cnsreg invest mvalue kstock if company==1, c(1)
                        
                        Constrained linear regression                   Number of obs     =         20
                                                                        Root MSE          =   328.7883
                        
                         ( 1)  mvalue + kstock = 1
                        ------------------------------------------------------------------------------
                              invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                        -------------+----------------------------------------------------------------
                              mvalue |   .2253842    .088823     2.54   0.021      .038774    .4119945
                              kstock |   .7746158    .088823     8.72   0.000     .5880055     .961226
                               _cons |  -871.0483   335.5035    -2.60   0.018    -1575.915   -166.1816
                        ------------------------------------------------------------------------------
                        
                        . cnsreg invest mvalue kstock i.company if company==1, c(1)
                        note: 1.company omitted because of collinearity
                        
                        Constrained linear regression                   Number of obs     =         20
                                                                        Root MSE          =   328.7883
                        
                         ( 1)  mvalue + kstock = 1
                        ------------------------------------------------------------------------------
                              invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                        -------------+----------------------------------------------------------------
                              mvalue |   .2253842    .088823     2.54   0.021      .038774    .4119945
                              kstock |   .7746158    .088823     8.72   0.000     .5880055     .961226
                           1.company |          0  (omitted)
                               _cons |  -871.0483   335.5035    -2.60   0.018    -1575.915   -166.1816
                        ------------------------------------------------------------------------------

                        Comment

                        Working...
                        X