Announcement

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

  • replace or overwrite existing predicted values

    Hello everyone. I am doing some exploratory analyses which requires estimating different model specifications. Please, how do I replace predicted values from an old regression with predicted values from a new regression without having to generate another variable. Here's what I mean:

    Code:
    sysuse auto.dta
    I estimate the following regression

    Code:
    reg price mpg mpg weight
    And compute the predicted values

    Code:
    predict phat, xb
    Then I run another regression

    Code:
    reg price mpg weight foreign turn displacement
    Attempting to generate phat again results in the error

    Code:
    predict phat, xb
    phat already defined
    r(110)
    Please, how can I generate phat that overwrites the values of the existing phat each time I estimate a different regression?

  • #2
    You cannot do this in Stata, -predict- does not have an option of acting like replace.

    Simply -drop- the predicted value when you dont need it no more. Or if you still need it when the new prediction is made, choose a different name for the new prediction.

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . reg price mpg
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(1, 72)        =     20.26
           Model |   139449474         1   139449474   Prob > F        =    0.0000
        Residual |   495615923        72  6883554.48   R-squared       =    0.2196
    -------------+----------------------------------   Adj R-squared   =    0.2087
           Total |   635065396        73  8699525.97   Root MSE        =    2623.7
    
    ------------------------------------------------------------------------------
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             mpg |  -238.8943   53.07669    -4.50   0.000    -344.7008   -133.0879
           _cons |   11253.06   1170.813     9.61   0.000     8919.088    13587.03
    ------------------------------------------------------------------------------
    
    . predict pricehat
    (option xb assumed; fitted values)
    
    . reg price mpg head
    
          Source |       SS           df       MS      Number of obs   =        74
    -------------+----------------------------------   F(2, 71)        =     10.44
           Model |   144280501         2  72140250.4   Prob > F        =    0.0001
        Residual |   490784895        71  6912463.32   R-squared       =    0.2272
    -------------+----------------------------------   Adj R-squared   =    0.2054
           Total |   635065396        73  8699525.97   Root MSE        =    2629.2
    
    ------------------------------------------------------------------------------
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             mpg |  -259.1057   58.42485    -4.43   0.000    -375.6015   -142.6098
        headroom |  -334.0215   399.5499    -0.84   0.406    -1130.701    462.6585
           _cons |   12683.31   2074.497     6.11   0.000     8546.885    16819.74
    ------------------------------------------------------------------------------
    
    . drop pricehat
    
    . predict pricehat
    (option xb assumed; fitted values)
    
    .

    Comment


    • #3
      Thanks, Joro, for the clarification.

      Comment


      • #4
        Thanks Joro. I also did not know about that.

        To add one more tip to Chibo, you can use "capture" command.
        This command ignores the error message if you try to drop pricehat when it does not exist.
        It is useful when making a program.


        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . reg price mpg
        
        Source | SS df MS Number of obs = 74
        -------------+---------------------------------- F(1, 72) = 20.26
        Model | 139449474 1 139449474 Prob > F = 0.0000
        Residual | 495615923 72 6883554.48 R-squared = 0.2196
        -------------+---------------------------------- Adj R-squared = 0.2087
        Total | 635065396 73 8699525.97 Root MSE = 2623.7
        
        ------------------------------------------------------------------------------
        price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
        -------------+----------------------------------------------------------------
        mpg | -238.8943 53.07669 -4.50 0.000 -344.7008 -133.0879
        _cons | 11253.06 1170.813 9.61 0.000 8919.088 13587.03
        ------------------------------------------------------------------------------
        
        . predict pricehat
        (option xb assumed; fitted values)
        
        .
        . reg price mpg head
        
        Source | SS df MS Number of obs = 74
        -------------+---------------------------------- F(2, 71) = 10.44
        Model | 144280501 2 72140250.4 Prob > F = 0.0001
        Residual | 490784895 71 6912463.32 R-squared = 0.2272
        -------------+---------------------------------- Adj R-squared = 0.2054
        Total | 635065396 73 8699525.97 Root MSE = 2629.2
        
        ------------------------------------------------------------------------------
        price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
        -------------+----------------------------------------------------------------
        mpg | -259.1057 58.42485 -4.43 0.000 -375.6015 -142.6098
        headroom | -334.0215 399.5499 -0.84 0.406 -1130.701 462.6585
        _cons | 12683.31 2074.497 6.11 0.000 8546.885 16819.74
        ------------------------------------------------------------------------------
        
        . capture drop pricehat
        
        . predict pricehat
        (option xb assumed; fitted values)
        
        .
        . capture drop pricehat
        
        .
        . capture drop pricehat
        
        .
        end of do-file

        Comment

        Working...
        X