Announcement

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

  • Generating a variance of a variable (panel data)

    Hi


    I have a question about generating a variable, which is a variance of another variable.
    Here is the context:
    - I have panel data of 2 firms.
    - Minimum data period is monthly.
    - I have a variable "return," a monthly abnormal return.
    - What I want to get is the variance (fluctuation) of the 'return' for each firm and for each 'timeid'.

    The code I tried:

    xtset id timeid
    egen variance = sd(return), by(id)
    replace variance = variance^2

    -> this gave me values that were the same for each id.
    -> But, as mentioned above, I want to get the fluctuation of the monthly return by company.

    So, I tried
    egen variance = sd(return), by(timeid)
    replace variance = variance^2
    Then, I got the different variances for each timeid, but I guess this value did not consider the "id."

    Can you help me how to resolve this issue?

    Thank you very much in advance.


    Below is the simple version of my dataset.

    id year month timeid return
    1 2010 1 1 3.46
    1 2010 2 2 5.22
    1 2010 3 3 2.71
    1 2010 4 4 4.90
    1 2010 5 5 4.33
    1 2010 6 6 5.13
    1 2010 7 7 4.03
    1 2010 8 8 3.63
    1 2010 9 9 2.98
    1 2010 10 10 3.96
    1 2010 11 11 4.90
    1 2010 12 12 2.56
    1 2011 1 13 5.97
    1 2011 2 14 4.82
    1 2011 3 15 3.14
    1 2011 4 16 2.51
    1 2011 5 17 5.92
    1 2011 6 18 5.28
    1 2011 7 19 5.71
    1 2011 8 20 2.59
    1 2011 9 21 2.15
    1 2011 10 22 5.72
    1 2011 11 23 4.54
    1 2011 12 24 5.51
    2 2010 1 1 4.24
    2 2010 2 2 2.53
    2 2010 3 3 3.49
    2 2010 4 4 5.72
    2 2010 5 5 4.54
    2 2010 6 6 4.19
    2 2010 7 7 4.89
    2 2010 8 8 3.96
    2 2010 9 9 5.31
    2 2010 10 10 3.04
    2 2010 11 11 2.40
    2 2010 12 12 2.68
    2 2011 1 13 3.24
    2 2011 2 14 4.33
    2 2011 3 15 3.76
    2 2011 4 16 3.12
    2 2011 5 17 4.83
    2 2011 6 18 3.47
    2 2011 7 19 5.90
    2 2011 8 20 5.45
    2 2011 9 21 2.72
    2 2011 10 22 2.77
    2 2011 11 23 5.01
    2 2011 12 24 3.34

  • #2
    What I want to get is the variance (fluctuation) of the 'return' for each firm and for each 'timeid'.

    I don't think that is what you want. Each firm has only one observation in each timeid (at least in your example data, and if that's not true in your real data you don't actually have panel data). So with only one observation, the variance is zero. You don't need any code at all for that. But I'm pretty sure that is not what you want.

    So you need to rethink what it is you really want. In other threads dealing with panel data of returns, I have seen people calculate the variance over a moving window. Perhaps that is what you need? If so, specify what the window parameters are and I can show you how it's done.

    Also, when posting back, please use the -dataex- command to show your example data. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X