Announcement

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

  • generate beta variable for panel dataset

    Dear all,

    I hope I could get some help with the following problem. My dataset is a panel and contains asset returns from 45 companies, over a period from 1998-2016 and looks like the following:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Date str23 CompanyName str11 Country float Recessionlag1 double(Xsector X)
    13880 "AIR LIQUIDE" "France" .   74.99411992971802   -.5188337554968327
    13881 "AIR LIQUIDE" "France" 0  .40916783846729227 .0031839828055408082
    13884 "AIR LIQUIDE" "France" 0   .1968211549754861   .00846651915332929
    13885 "AIR LIQUIDE" "France" 0  -.1809183794515586 -.003148172018527824
    13886 "AIR LIQUIDE" "France" 0 -.29311647324801804  -.03263164551346719
    end
    format %tdnn/dd/CCYY Date
    Now I am trying to generate a new variable containing the beta coefficient of the regression in the code below (cbeta) but I get a variable that only contains the beta from the first company in the sector for all companies and then Stata gives an error code:

    Code:
    forvalues i = 1(1)45 {
       qreg Xsector X, quantile(0.05)
       gen cbeta `i' = _b[X]
       }
    Code:
    variable cbeta already defined
    r(110);
    How do I adjust my command to generate the beta coefficient variable for each company separately in the panel dataset?

    I am using the Stata version 14.2
    I hope I have specified my problem clearly enough,
    Kind regards,
    Charlotte
    Last edited by Charlotte Koelemij; 07 Dec 2017, 03:25.

  • #2
    I see two problems.

    First, the loop will run the same qreg each time through the loop - there is nothing in the qreg command to restrict it to the data for a single company.

    Second, if you are trying to create variables cbeta1, cbeta2, ..., you need to remove the space between cbeta and `i' in your generate command. As it stands, it tries to generate the variable cbeta (with no digit after it) each time through the loop. That is the meaning of the error message you received.

    Comment


    • #3
      Dear William,

      Thank you very much for your quick response,

      I have adjusted my command to solve for the same qreg;
      Code:
      forvalues i = 1(1)45 {
         qreg Xsector X, quantile(0.05), if companyid==`i'
         gen cbeta`i' = _b[X]
         }
      Then I get a beta for each different company. However, I want to generate one variable "cbeta" which differs per company like this:

      company A 1
      company A 1
      company A 1
      company B 2
      company B 2
      company B 2
      company C 3
      company C 3
      company C 3

      Could you tell me how to adjust the command to do so?

      Kind regards,
      Charlotte

      Comment


      • #4
        You can use runby (from SSC) to perform a regression per company. This would look something like:

        Code:
        * create a demonstration dataset
        clear all
        set seed 312
        set obs 45
        gen company = _n
        expand 1000
        bysort company: gen Date = mdy(12,31,1997) + _n
        format %tdnn/dd/CCYY Date
        gen double Xsector = runiform()
        gen double X = runiform()
        
        * perform a regression per company
        program my_qreg
          qreg Xsector X, quantile(0.05)
          gen cbeta = _b[X]
        end
        runby my_qreg, by(company)
        
        * spot check for company 2
        qreg Xsector X if company == 2, quantile(0.05)
        sum cbeta if company == 2
        and the spot check results:
        Code:
        . * spot check for company 2
        . qreg Xsector X if company == 2, quantile(0.05)
        Iteration  1:  WLS sum of weighted deviations =  102.88653
        
        Iteration  1: sum of abs. weighted deviations =  103.15077
        Iteration  2: sum of abs. weighted deviations =  48.853891
        Iteration  3: sum of abs. weighted deviations =  45.019146
        Iteration  4: sum of abs. weighted deviations =  25.264765
        Iteration  5: sum of abs. weighted deviations =  24.678962
        Iteration  6: sum of abs. weighted deviations =  23.451682
        Iteration  7: sum of abs. weighted deviations =  23.352759
        Iteration  8: sum of abs. weighted deviations =  23.345709
        
        .05 Quantile regression                             Number of obs =      1,000
          Raw sum of deviations  23.3467 (about .05011088)
          Min sum of deviations 23.34571                    Pseudo R2     =     0.0000
        
        ------------------------------------------------------------------------------
             Xsector |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
                   X |    .002263    .022248     0.10   0.919    -.0413952    .0459213
               _cons |   .0489107   .0128404     3.81   0.000     .0237134     .074108
        ------------------------------------------------------------------------------
        
        . sum cbeta if company == 2
        
            Variable |        Obs        Mean    Std. Dev.       Min        Max
        -------------+---------------------------------------------------------
               cbeta |      1,000     .002263           0    .002263    .002263

        Comment


        • #5
          Dear Robert,

          You are a hero! Thank you very much it works perfectly!

          Kind regards, and many thanks,
          Charlotte

          Comment


          • #6
            Dear Robert,

            I hope I can ask you an additional question to my previous one. I want to run the regression with the runby command per company yearly. How would I have to adjust the command?

            I tried a lot including the following,
            Code:
            program my_qreg  
            qreg Xsector X, quantile(0.05)  
            gen cbeta = _b[X]
            end
            runby my_qreg, by(company, year)
            but I get an error code which says: .

            Code:
             runby my_qreg, by(companyid, year)
            by() does not contain a valid varlist
            r(198);
            Thank you very much in advance,
            Kind regards,
            Charlotte

            Comment


            • #7
              You should not have a comma (,) within the -by()- option.

              Comment


              • #8
                thank you!

                Comment

                Working...
                X