Announcement

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

  • margins of Expected outcome vs Predicted outcome

    Dear all,
    I wonder if anyone knows of a simple way to obtain predicted means standard errors using -margins- (or other approaches)
    for example, say i run the following regression:
    Code:
    sysuse auto
    regress price mpg foreign
    ** I can get STD ERR for expected outcomes easily:
    margins, at(foreign=1 mpg=25 ) at(foreign=0 mpg= 20 )
    ** but how do I get Std for the predicted outcomes?
    ** I know i can get something like that using predict
    predict price_hat_sep, stdp
    ** but not sure how to do that using specific values as in the example above
    Thank you
    Fernando

  • #2
    Use the -nose- option
    Code:
    margins, at(foreign=1 mpg=25 ) at(foreign=0 mpg= 20 ) predict(stdp) nose

    Comment


    • #3
      Thank you Scott
      I realized i stated my question incorrectly.
      What i want to being able to produce CI for two scenarios.
      - for expected outcome
      - for predicted outcome
      The difference between the first and second is that the later should include an estimation of the variance of the error as well as variance of Betas

      Below an example using mata, and making the predictions at the mean:

      Code:
      sysuse auto, clear
      ** Mata
      mata: 
          y = st_data(.,"price")
          x = st_data(.,"mpg trunk foreign"),J(rows(y),1,1)
          b = invsym(cross(x,x))*cross(x,y)
          // vcv
          e = y:-x*b
          sig2  = cross(e,e)/(rows(y)-cols(x))
          vcovb = sig2 * invsym(cross(x,x))
          // CI at means
          x_mean = mean(x)
          xb = x_mean*b
          varxb = x_mean*vcovb*x_mean'
          // CI of expected Y
          t_crit=invt(70,0.975)
          (xb:-t_crit*sqrt(varxb),xb:+t_crit*sqrt(varxb))
          // CI of predicted Y 
          (xb:-t_crit*sqrt(varxb+sig2),xb:+t_crit*sqrt(varxb+sig2))
      end
      // This only produces CI for expected Y
      qui:regress price mpg trunk foreign
      margins
      As you can see i can produce Both CI manually, but would like to see if I can produce the second one using margins (or similar).
      Thank you
      F

      Comment


      • #4
        FernandoRios

        The deprecated command -adjust- will produce intervals for the forecast. Since -adjust- does not save any results, I saved a personal version as -adjust2-, made the DoTable subcommand an rclass program (line 636) and then added following after the -tabdisp- command (around lines 806)

        Code:
            ret local lb =  `lbs'
            ret local ub =  `ubs'
            ret local xb = `xbvar'
        Results:

        Code:
        :     // CI of expected Y
        :     t_crit=invt(70,0.975)
        
        :     (xb:-t_crit*sqrt(varxb),xb:+t_crit*sqrt(varxb))
                         1             2
            +-----------------------------+
          1 |  5578.192149   6752.321365  |
            +-----------------------------+
        
        :     // CI of predicted Y 
        :     (xb:-t_crit*sqrt(varxb+sig2),xb:+t_crit*sqrt(varxb+sig2))
                         1             2
            +-----------------------------+
          1 |  1081.128116    11249.3854  |
            +-----------------------------+
        
        : end
        -------------------------------------------------------------------------------------------------------------------------------------------------
        
        . // This only produces CI for expected Y
        . qui:regress price mpg trunk foreign
        
        . 
        . adjust2 , ci  noheader
        
        ----------------------------------------------
              All |         xb          lb          ub
        ----------+-----------------------------------
                  |    6165.26    [5578.19    6752.32]
        ----------------------------------------------
             Key:  xb         =  Linear Prediction
                   [lb , ub]  =  [95% Confidence Interval]
        
        . return list
        
        macros:
                         r(xb) : "6165.256964536324"
                         r(ub) : "6752.32"
                         r(lb) : "5578.19"
        
        . 
        . adjust2 , stdf ci  noheader
        
        ----------------------------------------------------------
              All |         xb        stdf          lb          ub
        ----------+-----------------------------------------------
                  |    6165.26   (2549.15)    [1081.13    11249.4]
        ----------------------------------------------------------
             Key:  xb         =  Linear Prediction
                   stdf       =  Standard Error (forecast)
                   [lb , ub]  =  [95% Prediction Interval]
        
        . return list
        
        macros:
                         r(xb) : "6165.256964536324"
                         r(ub) : "11249.4"
                         r(lb) : "1081.13"

        Comment


        • #5
          Didn’t know about that one
          thank you!

          Comment

          Working...
          X