Skip to content

S3 Predict method for IV object #596

@zhizhongpu

Description

@zhizhongpu

Problem: predict() yields 0 for IV objects

> library(fixest)
> library(dplyr)
> iv = feols(mpg ~ 1 | hp ~ cyl, data = mtcars)
> iv |> summary(stage = 2)
TSLS estimation - Dep. Var.: mpg
                  Endo.    : hp
                  Instr.   : cyl
Second stage: Dep. Var.: mpg
Observations: 32
Standard-errors: IID 
             Estimate Std. Error  t value   Pr(>|t|)    
(Intercept) 33.290411   2.051284 16.22906  < 2.2e-16 ***
fit_hp      -0.089986   0.013059 -6.89063 1.1934e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 4.01816   Adj. R2: 0.52588
F-test (1st stage), hp: stat = 67.7, p = 3.478e-9, on 1 and 30 DoF.
            Wu-Hausman: stat = 15.5, p = 4.804e-4, on 1 and 29 DoF.
> dt_test = mtcars  |> 
+ select(mpg, hp, cyl) |> 
+ mutate(fit_hp = hp)
> dt_test$mpg_hat = predict(iv, newdata = dt_test)
> dt_test |> head(5)
                   mpg  hp cyl fit_hp mpg_hat
Mazda RX4         21.0 110   6    110       0
Mazda RX4 Wag     21.0 110   6    110       0
Datsun 710        22.8  93   4     93       0
Hornet 4 Drive    21.4 110   6    110       0
Hornet Sportabout 18.7 175   8    175       0

Expected behavior: either of

  1. Raise an error to alert the user that this is not supported
  2. support it (including the se.fit argument)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions