Announcement

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

  • Loop with missing values

    Dear all,

    I want to estimate betas of the Fama French Model for every year. I have got weekly returns and I am using the following loop:


    foreach variable of varlist r_share* {
    sum `variable' if year == 1991
    if r(N) >= 52 {
    scalar lagged = floor(4*(r(N)/100)^(2/9))
    newey `variable' r_sp500 smb hml if year == 1991, lag(`= lagged')
    }
    else {
    display "Not enough observations"
    }
    matrix matrix_1991 = nullmat(matrix_1991) \ e(b)
    }


    Variables:
    r_share = return share
    smb = Fama French factor
    hml = Fama French factor
    r_sp500 = return s&p500

    The loop is working fine there is just one problem and I am not able to solve this. As I need all the estimated betas in a matrix afterwards (matrix_1991) and I have got missing observations (for the Y-values) how can I tell stata to save "." in the matrix when there are "Not enough observations"?

    So if I have got enought observations Stata should do the newey west regression and if not it should just store . for all coefficients.

    Hopefully somebody can help me out!

    BR

    Ikn

  • #2
    Well, instead of (or in addition to) -display "not enough observations"- run
    Code:
    matrix junk = (.)
    matrix matrix_1991 = nullmat(matrix_1991) \ junk

    Comment


    • #3
      Thank you Clyde for your quick response. At least my loop is calculating now but unfortunately I receive a comformability error.

      My code looks as follows:

      Code:
      foreach variable of varlist r_share* {
      sum `variable' if year == 1991
      if r(N) >= 52 {
      scalar lagged = floor(4*(r(N)/100)^(2/9))
      newey `variable' r_sp500 smb hml if year == 1991, lag(`= lagged')
      }
      else {
      matrix junk = (.)
      matrix matrix_1991 = nullmat(matrix_1991) \ junk
      }
      matrix matrix_1991 = nullmat(matrix_1991) \ e(b)
      }
      I guess Stata or at least me can't fill the new matrix (newey west matrix + junk matrix) with only newey west coefficients or is there a solution for this?

      Comment


      • #4
        Right, sorry. The matrix junk can't just be a single missing value because it won't conform to the shape of matrix_1991. So you have to make it have as many missing values in it as you have coefficients in the e(b) matrices when -newey- runs successfully. So, let's say that the e(b) matrices all have 4 colulmns, then -matrix junk = (., ., ., .)- is needed.

        Comment


        • #5
          Wow thank you Clyde! You really helped me out. Really appreciate that!

          Comment

          Working...
          X