Sam
Sam

Reputation: 13

Delta method confidence interval from 2SLS regression (with FE) using fixest and marginaleffects packages - Get NAs

I'm trying to find the confidence interval with the delta method using avg_comparisons() with the fixest feols() function for a 2SLS/IV + FE regression.

My specification has only one independent variable and one instrument. When I do my specification and I use avg_comparisons(), I get NAs.

Is there anything I can do to get the correct confidence interval?

One independent variable and one instrument example:

library(fixest)
library(marginaleffects)

TwoSLS <- feols(y ~ 0 | i + t | x1 ~ z, df)

avg_comparisons(TwoSLS)

Term Contrast Estimate Std. Error  z Pr(>|z|)  S 2.5 % 97.5 %
   x1 mean(+1)        0         NA NA       NA NA    NA     NA

predict(TwoSLS, newdata = head(df))

[1]  0.3466693 -0.7935570  2.3911069 -0.1111627 -1.1041513 -2.2443776

But it works when I add another independent variable, which is not what I really want:

TwoSLS_with_X2 <- feols(y ~ x2 | i + t | x1 ~ z, df)

avg_comparisons(TwoSLS_with_X2)

 Term Contrast Estimate Std. Error      z Pr(>|z|)    S 2.5 % 97.5 %
   x1 mean(+1)    0.382     0.0876  4.361   <0.001 16.2  0.21  0.554
   x2 mean(+1)   -0.137     0.5007 -0.275    0.784  0.4 -1.12  0.844

predict(TwoSLS_with_X2, newdata = head(df))

[1]  0.4739496  0.2461527  2.3167039 -0.2782309 -1.1859796 -1.9779422

If anyone wants reproducible data:

i <- c("a","a","a","a","b","b","b","b","c","c","c","c","d","d","d","d")
t <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
y <- rnorm(16)
x1 <- rnorm(16)
x2 <- rnorm(16)
z <- rnorm(16)
n <- c(2,2,2,2,1,1,1,1,6,6,6,6,8,8,8,8)
df <- data.frame(i,t,y,x1,x2,z,n)

Upvotes: 0

Views: 90

Answers (1)

Vincent
Vincent

Reputation: 17823

You are getting this result because changing the x1 or x2 variable has no effect on the output of predict(). Consider what avg_comparisons() is doing under the hood (read tutorial here):

i <- c("a","a","a","a","b","b","b","b","c","c","c","c","d","d","d","d")
t <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
y <- rnorm(16)
x1 <- rnorm(16)
x2 <- rnorm(16)
z <- rnorm(16)
n <- c(2,2,2,2,1,1,1,1,6,6,6,6,8,8,8,8)
df <- data.frame(i,t,y,x1,x2,z,n)

library(fixest)
library(marginaleffects)

TwoSLS <- feols(y ~ 0 | i + t | x1 ~ z, df)

df_plus_one <- transform(df, x1 = x1 + 1)

pred <- predict(TwoSLS, newdata = df)
pred_plus_one <- predict(TwoSLS, newdata = df_plus_one)

all(pred == pred_plus_one)

    [1] TRUE

plot(pred, pred_plus_one)

Therefore, avg_comparisons() returns the correct estimate: a precise zero.

Upvotes: 1

Related Questions