Announcement

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

  • #16
    Originally posted by Jan Filips View Post


    Hi Robert,

    I was trying to edit your code which should give me exactly what I want.

    The original code you typed stands in the following topic, post #11: http://www.statalist.org/forums/foru...tas-for-stocks

    Code:
    
    * ------------ regressions over a window of 6 periods using -rangestat- -------- * define a linear regression in Mata using quadcross() - help mata cross(), example 2 mata: mata clear mata set matastrict on real rowvector myreg(real matrix Xall) { real colvector y, b, Xy real matrix X, XX y = Xall[.,1] // dependent var is first column of Xall X = Xall[.,2::cols(Xall)] // the remaining cols are the independent variables X = X,J(rows(X),1,1) // add a constant XX = quadcross(X, X) // linear regression, see help mata cross(), example 2 Xy = quadcross(X, y) b = invsym(XX) * Xy return(rows(X), b') } end * regressions with a constant over a rolling window of 6 periods by permno rangestat (myreg) stockexcessret mktrf, by(permno) interval(time -5 0) casewise * the Mata function returns first the number of observations and then as many * variables as there are independent variables (plus the constant) for the betas rename (myreg1 myreg2 myreg3) (nobs rs_mktrf rs_cons) * reject results if the window is less than 6 or if the number of obs < 4 isid permno year by permno: replace rs_mktrf = . if _n < 6 | nobs < 4 by permno: replace rs_cons = . if _n < 6 | nobs < 4 save "rangestat_results.dta", replace * ----------------- replicate using -rolling- ---------------------------------- use "test_data.dta", clear levelsof permno, local(permno) foreach s of local permno { rolling _b, window(6) saving(betas_`s', replace) reject(e(N) < 4): /// regress stockexcessret mktrf if permno == `s' } clear save "betas.dta", replace emptyok foreach s of local permno { append using "betas_`s'.dta" } rename end year merge 1:1 permno year using "rangestat_results.dta" isid permno year, sort gen diff_mktrf = abs(_b_mktrf - float(rs_mktrf)) gen diff_cons = abs(_b_cons - float(rs_cons)) summ diff*



    But what is the essence of the last part with ''replicate using -rolling-'' ?

    In addition, what type of dataset should I fill in for
    Code:
     use "test_data.dta", clear
    Because I do not have test_data.dta...
    I must say the whole part under: ''* ----------------- replicate using -rolling- ----------------------------------''

    Didn't work for me...

    Comment


    • #17
      It's still not clear what you're finding unclear in Robert Picard 's answer in #11. Assuming that your variable names are exactly as you told us then all you need is one line of code, namely

      Code:
       
       rangestat (reg) Re_Rf Rm_Rf, interval(fyear -4 0) by(gvkey)
      What's to edit? If your problem is really different from that stated, we can't help with the edit unless you tell us what's different.

      rolling is irrelevant to Robert's solution, as rangestat does the rolling for you. Did you try reading its help?

      Comment


      • #18
        Hi everyone, I am using Stata 16 to work on a similar problem for my research. In my case, I need to save the residual from each regression. After doing some web search, I found the following code online for one year of data:
        Code:
        qui levelsof co_code, local(co_code)
        capture drop resid
        ge resid = .
        foreach stk of local co_code{
           reg returns lag_mw mw lead_mw if co_code == "`stk'" 
           predict res, residuals
           replace resid = res if e(sample)
           drop res
           }
        Here, Co_code: Company Code; returns:stock returns; mw: market index returns
        The above code's author wrote this for one-year data and so they have run the regression by every stock code (co_code). Since I have multiple years of data (2008-2018), I tried to transform this loop to run multiple-year regression. When I run the code using ideas from this post, I get the following:

        Code:
         
        . qui levelsof co_code, local(co_code)
        
        . capture drop resid
        
        . ge resid = .
        (7,127,274 missing values generated)
        
        . forval yr = 2008/2018 {
          2. foreach stk of local co_code{
          3. reg wnreturns lag_mwret mwret lead_mwret if fyear==`yr' co_code =="`stk'" 
          4.  predict res, residuals
          5.    replace resid = res if e(sample)
          6.    drop res
          7.    }
          8.    }
        invalid 'co_code' 
        r(198);
        Could you please help me understand where my transformation is not working or if I transforming it correctly? If you could help transform the first code I have provided, I would be very grateful. Thank you!

        Comment


        • #19
          Code:
           
            if fyear==`yr' & co_code =="`stk'"
          is what you seek. But watch out. You are fitting a regression with 3 predictors which will be futile unless you have at least 5 observations for each (industry, year) combination and unlikely to be a good idea unless you have many more observations.

          Comment


          • #20
            Thanks Nick! I added the missing "&" to my command. I checked and there are definitely more than 5 observations for each firm, year combination. Your comment made me think of something, I have weekly data for each company, year combination. The variable "res" is supposed to be a residual return for every week. So, do you think I should be running this regression with weeks as my time variable? What do you think? For your reference, here's the link to the document which I am using to create this loop
            HTML Code:
            http://lwstatantu.blogspot.com/
            . If I need to run a regression with weekly data, how will I incorporate it into my code? I would be grateful for any ideas. Thanks!

            Comment


            • #21
              Hi Nick Cox, Your comment in #19 gave me the idea to run the regression setting week as my time variables. So, I tried the following and get this error:

              Code:
              . qui levelsof co_code, local(co_code)
              
              . qui levelsof week_id, local(week_id)
              
              . capture drop resid
              
              . ge resid = .
              (1,672,630 missing values generated)
              
              . foreach stk of local co_code{
                2. foreach w of local week_id{
                3.    reg wnreturns lag_mwret mwret lead_mwret if week_id=="`w'" & co_code =="`stk'" 
                4.    predict res, residuals
                5.    replace resid = res if e(sample)
                6.    drop res
                7.    }
                8.    }
              type mismatch
              r(109);
              I am not sure why I am getting this error. I have tried different combinations but to no avail. I would really appreciate any mistakes you could point in my code. Also, for your reference, this is what my data looks like:

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input long co_code float(year week_id) double(mwret lag_mwret lead_mwret) float resid
              11 2008 2535   -.06649971008300781   -.09247142169624567   -.03148415847681463 .
              11 2008 2536   -.03148415847681463   -.06649971008300781   -.11137222312390804 .
              11 2008 2537   -.11137222312390804   -.03148415847681463   -.21280589513480663 .
              11 2008 2538   -.21280589513480663   -.11137222312390804    .18719903379678726 .
              11 2008 2539    .18719903379678726   -.21280589513480663    .03372354805469513 .
              11 2008 2540    .03372354805469513    .18719903379678726   -.11738803843036294 .
              11 2008 2541   -.11738803843036294    .03372354805469513   -.03315603360533714 .
              11 2008 2542   -.03315603360533714   -.11738803843036294  -.010439421981573105 .
              11 2008 2543  -.010439421981573105   -.03315603360533714    .03699047735426575 .
              11 2008 2544    .03699047735426575  -.010439421981573105    .06843714625574648 .
              11 2008 2545    .06843714625574648    .03699047735426575   .019301355816423893 .
              11 2008 2546   .019301355816423893    .06843714625574648   -.02672520885244012 .
              11 2008 2547   -.02672520885244012   .019301355816423893  -.013181474292650819 .
              11 2009 2548  -.013181474292650819   -.02672520885244012  -.029572789557278156 .
              11 2009 2549  -.029572789557278156  -.013181474292650819   -.04662084439769387 .
              11 2009 2550   -.04662084439769387  -.029572789557278156    .05161661631427705 .
              11 2009 2551    .05161661631427705   -.04662084439769387  -.016435411293059587 .
              11 2009 2552  -.016435411293059587    .05161661631427705    .04282562481239438 .
              11 2009 2553    .04282562481239438  -.016435411293059587   -.05246870941482484 .
              11 2009 2554   -.05246870941482484    .04282562481239438  -.004928974318318069 .
              11 2009 2555  -.004928974318318069   -.05246870941482484  -.043389408849179745 .
              11 2009 2556  -.043389408849179745  -.004928974318318069   -.02761586755514145 .
              11 2009 2557   -.02761586755514145  -.043389408849179745    .08259395556524396 .
              11 2009 2558    .08259395556524396   -.02761586755514145    .06565725135806133 .
              11 2009 2559    .06565725135806133    .08259395556524396    .02514732163399458 .
              11 2009 2560    .02514732163399458    .06565725135806133    .08832431398332119 .
              11 2009 2561    .08832431398332119    .02514732163399458   .041370447957888246 .
              11 2009 2562   .041370447957888246    .08832431398332119   -.04516167985275388 .
              11 2009 2563   -.04516167985275388   .041370447957888246    .04222985589876771 .
              11 2009 2564    .04222985589876771   -.04516167985275388  -.007954363245517015 .
              11 2009 2565  -.007954363245517015    .04222985589876771   .002809791825711727 .
              11 2009 2566   .002809791825711727  -.007954363245517015    .16100615786854178 .
              11 2009 2567    .16100615786854178   .002809791825711727  .0013456032902467996 .
              11 2009 2568  .0013456032902467996    .16100615786854178    .05784685374237597 .
              
              
              end
              format %tw week_id
              Thanks!

              Comment


              • #22
                week_id is numeric, so it's an error to presents its values as strings, which is what the quotation marks imply

                You got that right earlier when you were looping over years.

                Comment


                • #23
                  Hi Nick Cox , thanks so much for your help as usual! I didn't realize that, so sorry. I have now removed the extra quotation and run the program, but it still gives the same error:

                  Code:
                  . qui levelsof week_id, local(week_id)
                  
                  . qui levelsof co_code, local(co_code)
                  
                  . capture drop resid
                  
                  . ge resid = .
                  (1,672,630 missing values generated)
                  
                  . foreach stk of local co_code{
                    2. foreach w of local week_id{
                    3.    reg wnreturns lag_mwret mwret lead_mwret if week_id==`w' & co_code =="`stk'"
                    4.    predict res, residuals
                    5.    replace resid = res if e(sample)
                    6.    drop res
                    7.    }
                    8.    }
                  type mismatch
                  r(109);
                  Also, in post #21, -dataex- shows my weekly data in terms of numbers, but in my actual data file, it is stored as 2008w1 2008w2 2008w3 and so on. Is this where the problem is?
                  I am so sorry about my silly mistakes missing in my code, I am trying to work on this code for almost a week, and I am still not there yet. I really appreciate any help you have!

                  Comment


                  • #24
                    The company code is numeric, so it's the same error all over again. The quotation marks are wrong.

                    Comment


                    • #25
                      Hi Nick Cox, thanks so much for pointing out my mistake. It was right next to my earlier error, and I didn't even see it. Thanks so much for your patience and help, I am able to run my code with some additional modifications!

                      Comment

                      Working...
                      X