Announcement

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

  • Missing values when calculate standard deviations

    My data is an unbalanced panel data.
    id: wficn (to uniquely identify each mutual fund)
    yrqt: the time variable, such as 1998q1,1998q2,1998q3,...etc.
    and key variable:
    x: fund style characteristics, x has many missings, the values of x range from 1 to 5.

    What I want to do is to calculate the standard deviation of variable x for past 12 quarters, call the new variable std_x


    in order to make the standard deviation not noisy, I need to require that for each fund, quarter, it has non-missing x for the past 12 quarters,
    for those that have missings, I will not calculate the vales for std_x, I just let it be missing too.

    say, for example, for fund i, for quarter 2000q1, for past 12 quarters, it has only 2 (<12) non-missing x, I will not use that two non-missing values to calculate the standard deviation for x, I will let std_x be missing too.

    wficn yrqt x std_x
    0001 1998q1 1 .
    0001 1998q2 2 .
    0001 1998q3 3 .
    0001 1998q4 2 .
    0001 1999q1 1 .
    0001 1999q2 2 .
    0001 1999q3 1 .
    0001 1999q4 2 .
    0001 2000q1 2 .
    0001 2000q2 1 .
    0001 2000q3 2 .
    0001 2000q4 2 .
    0001 2001q1 2 some number
    0001 2001q2 . .
    0001 2001q3 1 .
    0001 2001q4 2 .
    0001 2002q1 . .
    0001 2002q2 3 .
    0001 2002q3 4 .
    My question is how to calculate this standard deviation?

  • #2
    The new program -tsegen- by Robert Picard and Nick Cox should make short work of this. You can get it with -ssc install tsegen-, and then read the help.

    Comment


    • #3
      Thank you so much , professor Schechter!
      I tried the following code,
      bysort wficn (yrqt): tsegen std_x = rowsd(L(1/12).x,12) //To require a 12 quarter minimum non missing

      but I got the following error msg:
      tsegen may not be combined with by

      Should I just use the code below?
      tsegen std_x = rowsd(L(1/12).x,12) //To require a 12 quarter minimum non missing

      Comment


      • #4
        Yes, the help file for -tsegen- clearly states that you cannot use the -by- prefix, and it does not support a -by- option. All of the -by-like work it does is controlled by the -tsset-. So your proposed code looks right to me. Just make sure you have -tsset wficn yrqt- first.

        Comment


        • #5
          Hi, Professor Schechter, THANK YOU SO MUCH!

          Comment

          Working...
          X