Announcement

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

  • time series: perform the same regression with different variables

    Hello everyone

    I have timeseries data containing the returns of an index and the returns of different shares. My goal is in a first step to regress each share return separately on the index return and then to save the estimated coefficients and standard deviations in order to display them in a table like in the regression outputs.

    What I am struggling to find out is a code with which to do this repeating computation in Stata. I guess a for loop would be the answer but I am not yet familiar with it in Stata. So I would be very grateful if anyone could give me a hint.

    My data has the following structure:

    index return stock1 stock2 stock3
    return1,1 return2,1 return3,1 return_i,1
    return1,2
    return2,2
    return3,2
    return_i,2
    return1,3
    return2,3
    return3,3
    return_i,3
    return1,t return2,t
    return3,t
    return_i,t

  • #2
    Here is an example that may start you in a useful direction.
    Code:
    sysuse auto, clear
    postutil clear
    postfile bse str20 var float (b se) using results, replace
    foreach s of varlist weight-turn {
        quietly regress `s' price
        matrix r = r(table)
        post bse ("`s'") (r[1,1]) (r[2,1])
    }
    postclose bse
    use results, clear
    list
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . postutil clear
    
    . postfile bse str20 var float (b se) using results, replace
    
    . foreach s of varlist weight-turn {
      2.     quietly regress `s' price
      3.     matrix r = r(table)
      4.     post bse ("`s'") (r[1,1]) (r[2,1])
      5. }
    
    . postclose bse
    
    . use results, clear
    
    . list
    
         +------------------------------+
         |    var          b         se |
         |------------------------------|
      1. | weight   .1419244   .0261645 |
      2. | length     .00326   .0008025 |
      3. |   turn   .0004618   .0001671 |
         +------------------------------+

    Comment


    • #3
      Thank you very much! This was really helpful!

      If I may ask another question related to it: The structure of my data is still the same. However, instead of the index' and shares' returns, I have the absolute index and share prices. Is there a code with which it is possible to automatically generate for each share and time period its respective return using the formula:

      return_it = log(share price_{i,t}/ share price_{i,t-1})-1

      so that I eventually end up with the data structure from the previous post? Up to now I used Microsoft Excel to do this computation and then added the results directly in the data editor, but since I am handling a rather large sample, the program is very slow. Hence, it would be great if I could implement this directly in Stata.

      Comment


      • #4
        The equivalent to the Excel formula you show would be - I think, because this is untested code -
        Code:
        foreach s of varlist share_price* {
            generate r_`s' = log10(`s'/`s'[_n-1]) - 1
            rename (r_*#) (return#[2])
        }
        since log() in Excel is the base 10 logarithm whereas in Stata log() is the natural logarithm.

        Or, suppose your share_price variables are numbered 1 to 42
        Code:
        forvalues v = 1/42 {
            generate return`v' = log10(share_price`v'/share_price`v'[_n-1]) - 1
        }

        Now let me add some advice on learning Stata. I'm sympathetic to you as a new user of Stata - it's a lot to absorb. And even worse if perhaps you are under pressure to produce some output quickly. Nevertheless, I'd like to encourage you to take a step back from your immediate tasks.

        When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. There are a lot of examples to copy and paste into Stata's do-file editor to run yourself, and better yet, to experiment with changing the options to see how the results change.

        All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu. The objective in doing the reading was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and PDF manuals.

        Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

        And now some advice on using Statalist effectively. Take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

        The more you help others understand your problem, the more likely others are to be able to help you solve your problem.
        Last edited by William Lisowski; 28 Mar 2019, 11:16.

        Comment


        • #5
          Thank you very much for your support and the valuable advices!

          Comment

          Working...
          X