Announcement

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

  • formula stata uses to calculate stdp and stdf

    Hi

    I'm trying to explain to someone the difference between the se of a prediction and the se of a forecast, for example illustrated by the stata commands

    Code:
    twoway (lfitci lny x, stdp)
    twoway (lfitci lny x, stdpf)
    Could anyone tell me the formula that is used by the stdp and stdf options in stata for twoway, preduction etc please?

    thanks
    Rob

  • #2
    Rob may be interested in taking a look at -regression postestimation- entry in Stata 13.1 .pdf manual, especially pages 1898-1899.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Carlo

      Thanks. I've found the formula reference you cited. However, when I calculate it I don't get the same results as Stata.

      I have 11 observations in my data - the independent x is just a number 1..11. the RMSE of the regression is 0.049. So s2 = 0.049^2

      Code:
      (X'X)-1
      in excel is

      Code:
      =MINVERSE(MMULT(TRANSPOSE($A$2:$A$12),$A$2:$A$12))
      The other parts of h, x' and x is the same as x2 as I only have one independent variable

      For my first observation, x=1, gives a value of 0.0022 from sqrt(s2 * h) but stata gives 0.028

      Can anyone point to what I'm doing wrong please?

      Thanks
      Rob

      Comment


      • #4

        I can't interpret your Excel code (Stata matrix code would have been better) , but I see two possible errors:

        1) For simple linear regression, the formula for \(h_i\) is not \(x_i^2\) or a simple function of it. Rather it is:

        \[
        h_i = \frac{(1 + x_i)(X'X)^{-1}(1+x_i)'}{n\sum_i(x_i - \overline{x})^2}
        \]

        where the design matrix \(X\) is

        \[
        \begin{aligned}
        X = &
        \begin{pmatrix}
        1 & x_1 \\
        \vdots & \vdots \\
        1 & x_n
        \end{pmatrix}
        \end{aligned}
        \]

        2) The formula for stdf is not \( \sqrt{s^2 h_i} = s\sqrt{h_i}\), but rather \(s\sqrt(1+ h_i)\).
        Last edited by Steve Samuels; 01 May 2015, 16:39.
        Steve Samuels
        Statistical Consulting
        [email protected]

        Stata 14.2

        Comment


        • #5
          Correction:


          \[
          h_i = (1 + x_i)'(X'X)^{-1}(1+x_i)
          \]
          Steve Samuels
          Statistical Consulting
          [email protected]

          Stata 14.2

          Comment


          • #6
            Here's a "hand" calculation that uses Stata's matrix language.

            Code:
            sysuse auto, clear
            keep in 1/20
            
            gen one= 1
            mkmat one turn, matrix(X) // X matrix
            
            mkmat gear, matrix(y)  // y
            matrix H =  X*invsym(X'*X)*X'  //Hat (projection) matrix
            local hat1 =  el("H",1,1)  // first diagonal element
            
            
            matrix sse =(y-H*y)'*(y-H*y)
            
            local rsd = sqrt((el("sse",1,1))/18) //residual SD
            
            /* Compute forecast from regress */
            regress gear turn
            predict stdf, stdf
            
            list stdf in 1
                +----------+
                 |     stdf |
                 |----------|
              1. | .3457681 |
                 +----------+
            
            . di `rsd'*sqrt(1+`hat1')  // hand calculation
            
            .3457681
            Steve Samuels
            Statistical Consulting
            [email protected]

            Stata 14.2

            Comment


            • #7
              Dear Steve Samuels,

              How do I calculate stdf in the context of mixed models for the fitted predictions?

              Comment

              Working...
              X