Announcement

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

  • gen variable containing coefficients in panel data set

    Dear all,

    I need to generate a new variable for each coefficient estimated from a xtreg in a panel data. I used the commands:
    Code:
    by country (date), sort: xtreg y x1 x2 x3, fe
    by country (date), sort: gen b1 = _b[x1]
    by country (date), sort: gen b2 = _b[x2]
    by country (date), sort: gen b3 = _b[x3]
    While this code works when my data is not xtset, here I get a new var containing all identical observations that correspond to the last xtreg.

    How can I get different coefficients for each country (id)?

    Thank You

    Stefano

  • #2
    The problem is that you are running all of the different regressions before you save any coefficients. As a result, only the last of the regressions' results remain available, so you get the same answers in every observations. This cannot be done using the -by:- prefix, unless you write a short program that both carries out the regression on a single country and then generates the values of the coefficients. Actually, rather than using -by:-, it is probably simpler to use the -runby- command, written by Robert Picard and me, and available from SSC:

    Code:
    capture program drop one_country
    program define one_country
        regress y x1 x2 x3
        forvalues i = 1/3 {
            gen b`i' = _b[x`i']
        }
        exit
    end
    
    runby one_country, by(country) status
    The key thing is that you cannot go on to do the next regression until you have first saved the results from the last one.

    Comment


    • #3
      Thank you very much Clyde for your message and apologise for the late reply, but I've been away.

      The code works fine, but I'm unable to include the coefficient for the constant. In addition, how would the code change if I have different varnames, e.g. abc def and ghi instead of x1 x2 and x3?

      Thank you very much

      S

      Comment


      • #4
        Code:
        capture program drop one_country
        program define one_country
            regress y abc def ghi
            foreach v of varlist abc def ghi {
                gen b`v' = _b[`v']
            }
            gen b_cons = _b[_cons]
            exit
        end
        
        runby one_country, by(country) status

        Comment


        • #5
          Thank you very much Clyde. The code works, but compared to the x1,x2,x3 version the programme discards existing data in memory and I get no saved estimates since the report indicates by-group with errors equal to the total number of groups. This is the output:
          Code:
           program define one_country
            1. regress cagdp reer gapgdp dry2 dro2 realgdppc tot
            2. forvalues v of varlist reer gapgdp dry2 dro2 realgdppc tot {
            3. gen b`v' = _b[`v']
            4. }
            5. gen gen b0 = _b[_cons]
            6. exit
            7. end
          
          . runby one_country, by (country) status
          
            elapsed ----------- by-groups ----------    ------- observations ------       time
               time      count     errors    no-data        processed         saved  remaining
          ------------------------------------------------------------------------------------
           00:00:00         28         28          0            2,044             0   00:00:00
          
          --------------------------------------
          Number of by-groups    =            28
          by-groups with errors  =            28
          by-groups with no data =             0
          Observations processed =         2,044
          Observations saved     =             0
          --------------------------------------
          I have also tried, following the runby help:
          Code:
          capture program drop one_country
          program define one_country
          capture regress cagdp reer gapgdp dry2 dro2 realgdppc tot
           if _rc exit
          forvalues v of varlist reer gapgdp dry2 dro2 realgdppc tot {
          capture gen b`v' = _b[`v']
          }
          capture gen gen b0 = _b[_cons]
          exit
          end
          runby one_country, by (country) status
          but it is not much help.
          Any idea?

          Thanks

          S

          Comment


          • #6
            The way to tell what is going wrong here is to add the -verbose- option to -runby-. That way you will see regression output and error messages as you go along. That way you will be able to see what's happening. Try that. If you can't find a fix that way, then post back, using the -dataex- command to show example data.

            The problem is likely to be something with your data not being suitable for the regressions. The code works fine with the following example crafted from the built-in auto.dta set:

            Code:
            sysuse auto, clear
            rename rep78 country
            rename price y
            rename mpg abc
            rename headroom def
            rename turn ghi
            
            capture program drop one_country
            program define one_country
                regress y abc def ghi
                foreach v of varlist abc def ghi {
                    gen b`v' = _b[`v']
                }
                gen b_cons = _b[_cons]
                exit
            end
            
            runby one_country, by(country)
            Without seeing your data, the most likely problem is that you just don't have enough non-missing data to actually do any of your desired regressions. Remember that a regression will only include those observations which have non-missing values in all of the regression variables. If any variable has a missing value, that observation is dropped. It does not take very much missing data scattered among the variables and observations to end up with a country having no observations that have complete non-missing data on all of y abc def and ghi. (Or only one observation, which also makes a regression impossible. Or, if there are only two or three observations, then abc, def, and ghi are colinear, so one or more of them will be omitted from the analyses and the code will then fail when it tries to generate a variable containing the corresponding coefficient.)

            Comment

            Working...
            X