Announcement

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

  • regression for each year

    Hello stalist member,
    I would need your help: I have some data like ROA, ROE etc for the years 2010-2015, I would like to run some regression in which I would like each time to use for instance the ROA of 2010, then another regression in which I will use the ROA of 2011 etc. I tried to create dummies for each year, for instance:
    Code:
    gen year1=0  replace year1=1 if year=2011
    but it doesn't seem to works. Any idea how I can do it?

    Here is a sample of my data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str8 Country str15 company_id int year float(carSMP carOMT carLTRO carAPP) double Q float(ROA ROE year1 year2 year3 year4)
    "Ireland" "AU000000JHX1(P)" 2008 . .           .            . .6610000000000001   .11238568  -1.9558418 0 0 0 0
    "Ireland" "AU000000JHX1(P)" 2009 . .           .            .             1.326   .12447219  -2.3002543 0 0 0 0
    "Ireland" "AU000000JHX1(P)" 2010 0 0           0            0             1.403   .14021218   -.6048405 0 0 0 0
    "Ireland" "AU000000JHX1(P)" 2011 0 0 -.010404246            0             1.512    .1030303   1.8829114 1 0 0 0
    "Ireland" "AU000000JHX1(P)" 2012 0 0           0            0             2.181   .05176983    6.010989 1 1 0 0
    "Ireland" "AU000000JHX1(P)" 2013 . .           .            .             2.803   .05565589   -.5884422 0 0 1 0
    "Ireland" "AU000000JHX1(P)" 2014 0 0           0            0             2.539   .19613597  -1.9792695 0 0 0 0
    "Ireland" "AU000000JHX1(P)" 2015 0 0           0            0             2.986    .2136346  -1.9356128 0 0 0 1
    "Spain"   "ES0101382036(P)" 2008 . .           .            .                 . -.005899404           . 0 0 0 0
    "Spain"   "ES0101382036(P)" 2009 . .           .            .                 . -.005114491           . 0 0 0 0
    "Spain"   "ES0101382036(P)" 2010 0 0           0            0              5.73 -.005411142           . 0 0 0 0
    "Spain"   "ES0101382036(P)" 2011 0 0           0            0             5.774 -.005466829           . 1 0 0 0
    "Spain"   "ES0101382036(P)" 2012 0 0           0            0             6.019 -.005594254           . 1 1 0 0
    "Spain"   "ES0101382036(P)" 2013 . .           .            .             6.361 -.009010404           . 0 0 1 0
    "Spain"   "ES0101382036(P)" 2014 0 0           0            0             7.085 -.007436499           . 0 0 0 0
    "Spain"   "ES0101382036(P)" 2015 0 0           0  .0021995818             7.658 -.006511902           . 0 0 0 1
    "Spain"   "ES0105015012(P)" 2008 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105015012(P)" 2009 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105015012(P)" 2010 0 0           0            0                 .           .           . 0 0 0 0
    "Spain"   "ES0105015012(P)" 2011 0 0           0            0                 .           .           . 1 0 0 0
    "Spain"   "ES0105015012(P)" 2012 0 0           0            0                 .           .           . 1 1 0 0
    "Spain"   "ES0105015012(P)" 2013 . .           .            .                 .           .           . 0 0 1 0
    "Spain"   "ES0105015012(P)" 2014 0 0           0            0                 .           .           . 0 0 0 0
    "Spain"   "ES0105015012(P)" 2015 0 0           0            0 .6210000000000001           .           . 0 0 0 1
    "Spain"   "ES0105019006(P)" 2008 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105019006(P)" 2009 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105019006(P)" 2010 0 0           0            0                 .           .           . 0 0 0 0
    "Spain"   "ES0105019006(P)" 2011 0 0           0            0                 .           .           . 1 0 0 0
    "Spain"   "ES0105019006(P)" 2012 0 0           0            0                 .           .           . 1 1 0 0
    "Spain"   "ES0105019006(P)" 2013 . .           .            .                 .           .           . 0 0 1 0
    "Spain"   "ES0105019006(P)" 2014 0 0           0            0              .918 -.003789441 -.004396346 0 0 0 0
    "Spain"   "ES0105019006(P)" 2015 0 0           0    .03770396 .6280000000000001  .019424774   .03285882 0 0 0 1
    "Spain"   "ES0105020004(P)" 2008 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105020004(P)" 2009 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105020004(P)" 2010 0 0           0            0                 .           .           . 0 0 0 0
    "Spain"   "ES0105020004(P)" 2011 0 0           0            0                 .           .           . 1 0 0 0
    "Spain"   "ES0105020004(P)" 2012 0 0           0            0                 .           .           . 1 1 0 0
    "Spain"   "ES0105020004(P)" 2013 . .           .            .                 .           .           . 0 0 1 0
    "Spain"   "ES0105020004(P)" 2014 0 0           0            0                 .           .           . 0 0 0 0
    "Spain"   "ES0105020004(P)" 2015 0 0           0            0                 .  -.12196817   -5.086957 0 0 0 1
    end

    Thank you in advance.

  • #2
    I don't understand what you want to do. If you just want to include year indicators in your regressions, you don't need to generate your own "dummy" variables. You should use Stata's factor variable notation instead. For example:

    Code:
    xtregr ROA ROE i.year, fe
    will give you a regression of ROA on ROE (you didn't say what you wanted to regress on what; all you mentioned is that ROA is involved in some way) with year effects included in the model.

    If what you want to do is a separate regression for each year, then you can do this:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str8 Country str15 company_id int year float(carSMP carOMT carLTRO carAPP) double Q float(ROA ROE year1 year2 year3 year4)
    "Ireland" "AU000000JHX1(P)" 2008 . .           .            . .6610000000000001   .11238568  -1.9558418 0 0 0 0
    "Ireland" "AU000000JHX1(P)" 2009 . .           .            .             1.326   .12447219  -2.3002543 0 0 0 0
    "Ireland" "AU000000JHX1(P)" 2010 0 0           0            0             1.403   .14021218   -.6048405 0 0 0 0
    "Ireland" "AU000000JHX1(P)" 2011 0 0 -.010404246            0             1.512    .1030303   1.8829114 1 0 0 0
    "Ireland" "AU000000JHX1(P)" 2012 0 0           0            0             2.181   .05176983    6.010989 1 1 0 0
    "Ireland" "AU000000JHX1(P)" 2013 . .           .            .             2.803   .05565589   -.5884422 0 0 1 0
    "Ireland" "AU000000JHX1(P)" 2014 0 0           0            0             2.539   .19613597  -1.9792695 0 0 0 0
    "Ireland" "AU000000JHX1(P)" 2015 0 0           0            0             2.986    .2136346  -1.9356128 0 0 0 1
    "Spain"   "ES0101382036(P)" 2008 . .           .            .                 . -.005899404           . 0 0 0 0
    "Spain"   "ES0101382036(P)" 2009 . .           .            .                 . -.005114491           . 0 0 0 0
    "Spain"   "ES0101382036(P)" 2010 0 0           0            0              5.73 -.005411142           . 0 0 0 0
    "Spain"   "ES0101382036(P)" 2011 0 0           0            0             5.774 -.005466829           . 1 0 0 0
    "Spain"   "ES0101382036(P)" 2012 0 0           0            0             6.019 -.005594254           . 1 1 0 0
    "Spain"   "ES0101382036(P)" 2013 . .           .            .             6.361 -.009010404           . 0 0 1 0
    "Spain"   "ES0101382036(P)" 2014 0 0           0            0             7.085 -.007436499           . 0 0 0 0
    "Spain"   "ES0101382036(P)" 2015 0 0           0  .0021995818             7.658 -.006511902           . 0 0 0 1
    "Spain"   "ES0105015012(P)" 2008 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105015012(P)" 2009 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105015012(P)" 2010 0 0           0            0                 .           .           . 0 0 0 0
    "Spain"   "ES0105015012(P)" 2011 0 0           0            0                 .           .           . 1 0 0 0
    "Spain"   "ES0105015012(P)" 2012 0 0           0            0                 .           .           . 1 1 0 0
    "Spain"   "ES0105015012(P)" 2013 . .           .            .                 .           .           . 0 0 1 0
    "Spain"   "ES0105015012(P)" 2014 0 0           0            0                 .           .           . 0 0 0 0
    "Spain"   "ES0105015012(P)" 2015 0 0           0            0 .6210000000000001           .           . 0 0 0 1
    "Spain"   "ES0105019006(P)" 2008 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105019006(P)" 2009 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105019006(P)" 2010 0 0           0            0                 .           .           . 0 0 0 0
    "Spain"   "ES0105019006(P)" 2011 0 0           0            0                 .           .           . 1 0 0 0
    "Spain"   "ES0105019006(P)" 2012 0 0           0            0                 .           .           . 1 1 0 0
    "Spain"   "ES0105019006(P)" 2013 . .           .            .                 .           .           . 0 0 1 0
    "Spain"   "ES0105019006(P)" 2014 0 0           0            0              .918 -.003789441 -.004396346 0 0 0 0
    "Spain"   "ES0105019006(P)" 2015 0 0           0    .03770396 .6280000000000001  .019424774   .03285882 0 0 0 1
    "Spain"   "ES0105020004(P)" 2008 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105020004(P)" 2009 . .           .            .                 .           .           . 0 0 0 0
    "Spain"   "ES0105020004(P)" 2010 0 0           0            0                 .           .           . 0 0 0 0
    "Spain"   "ES0105020004(P)" 2011 0 0           0            0                 .           .           . 1 0 0 0
    "Spain"   "ES0105020004(P)" 2012 0 0           0            0                 .           .           . 1 1 0 0
    "Spain"   "ES0105020004(P)" 2013 . .           .            .                 .           .           . 0 0 1 0
    "Spain"   "ES0105020004(P)" 2014 0 0           0            0                 .           .           . 0 0 0 0
    "Spain"   "ES0105020004(P)" 2015 0 0           0            0                 .  -.12196817   -5.086957 0 0 0 1
    end
    
    capture program drop one_year
    program define one_year
        local outcome ROA
        unab predictors: ROE 
        capture noisily regress `outcome' `predictors'
        if c(rc) == 0 {
            foreach p of local predictors {
                gen b_`p' = _b[`p']
                gen se_`p' = _se[`p']
            }
            gen n_obs = e(N)
            gen r2 = e(r2)
            exit 0
        }
        else if inlist(c(rc), 2000, 2001) {
            exit 0
        }
        else {
            foreach p of local predictors {
                gen b_`p' = .x
                gen se_`p' = .x
            }
            gen n_obs = .x
            gen r2 = .x
            exit c(rc)
        }
    end
    
    runby one_year, by(year)
    Notes: Your example data shows that you have many missing values. Since observations containing missing values cannot be included in a regression analysis, it is possible that for some years, there will not be enough valid observations to do the desired regression. (In fact, that happens in your example.) I have set up -capture- in the program so that such years are not counted as errors. They simply show up in the results with ordinary missing values (.) for the coefficients, standard errors, number of observations, and R2. If there are any errors other than that in calculating the regression, those will show up as errors, and you will be able to spot them in the results because the coefficients, standard errors, number of observations and R2 will be shown as an extended missing value (.x).

    To use this code you need to install -runby- from SSC. (-ssc install runby-). This program was written by Robert Picard and me.

    The code regresses ROA and ROE. You will need to change the code to specify the actual outcome and predictor variables you want to use in your regressions. You do that by modifying the lines that are shown in italics. Also, you may want to do an -xtreg- instead of a regress, or something like that. Again, make the corresponding change. You really didn't provide any information about what you wanted to do, so I just picked a lame example to illustrate the approach. In the future, you will get more helpful advice if you provide a fuller explanation of what it is you want.

    It is also possible that none of this is what you wanted. If that's the case, please try again to explain clearly what is desired, perhaps by showing a small example of what the results you want would look like.

    Comment


    • #3
      Hello Clyde,

      Thank you very much for your response. I am so sorry that I didnt give you a better, more detailed description. So I would like to use the xtreg and for instance firstly I would like to run a regression where the dependent variable is carSMP and the independent the ROA,ROE,Q of 2010 only, then a second regression but now to have the ROA,ROE,Q of 2011.

      Comment


      • #4
        So for the year specific regressions, the code I showed in #2 should work for you. You just need to change
        Code:
        local outcome ROA
        unab predictors: ROE 
        


        to

        Code:
        local outcome carSMP
        local predictors ROA ROE Q

        Comment


        • #5
          I tried but it deletes all my data, all my variables

          Comment


          • #6
            Well, I tested the code in #2 in my computer and it ran just fine. However, when I change the regression variables, as in #4, the regression coefficients and standard errors are all either missing values or zeroes. Running it with the -verbose- option added to the -runby- command shows that the data for each year contain too few observations to actually do a regression. Remember that if any variable mentioned in the regression command has missing values, then the observation is excluded from the estimation sample. While I imagine your data set is larger than your example in #2, I do see that there is an enormous amount of missing data in it. So it may just be that the regressions you are trying to carry out are just not mathematically possible.

            Here's what I recommend you try. Change the -runby- command to:

            Code:
            runby one_year, by(year) verbose
            That way, you will see, for each year, any error messages generated and any regression output created. I suspect you will see that the regressions are all failing due to "insufficient observations" or "no observations." If so, the problem is not with the code but with your data and you will either have to fill in some of the missing data somehow, or abandon these regressions. If it is not that, then post back showing what is actually going on. (Please show actual Stata output--descriptions are usually not clear and detailed enough.)

            Added: Also, if you have made any changes to the code in #2, other than what was recommended here and in #4, then copy/paste the entire code you are actually running here. There is no such thing as a "tiny" change to code.
            Last edited by Clyde Schechter; 31 Jan 2018, 16:01.

            Comment

            Working...
            X