lrberge / fixest

Fixed-effects estimations
https://lrberge.github.io/fixest/
361 stars 59 forks source link

Testing for equality of coefficient estimates? #437

Closed martinkoenen closed 7 months ago

martinkoenen commented 10 months ago

Hello, I'm using feols for running some pretty straightforward regressions of the below form. Is there a way to test for the equality of two coefficient estimates (e.g. exposure_0_12_first_eligible_rep_exp_dif and exposure_13_19_first_eligible_rep_exp_dif in the below example) and let the package report the t-statistic? I know of 'LinearHypothesis', but that only seems to give me Chi-square statistics. Any thoughts? Thank you!

feols(first_eligible_rep ~ exposure_0_12_first_eligible_rep_exp_dif + exposure_13_19_first_eligible_rep_exp_dif + ... | ... )

grantmcdermott commented 7 months ago

You can use the hypotheses() function from the (excellent) marginaleffects package.

library(fixest)
mod = feols(mpg ~ hp + wt | cyl, data = mtcars)

library(marginaleffects)
hypotheses(mod, "hp = wt")
#> 
#>     Term Estimate Std. Error    z Pr(>|z|)   S 2.5 % 97.5 %
#>  hp = wt     3.16       1.15 2.74  0.00616 7.3 0.898   5.42
#> 
#> Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high

Another option is the multcomp package.

library(multcomp)
#> Loading required package: mvtnorm
#> Loading required package: survival
#> Loading required package: TH.data
#> Loading required package: MASS
#> 
#> Attaching package: 'TH.data'
#> The following object is masked from 'package:MASS':
#> 
#>     geyser
summary(glht(mod, "hp - wt = 0"))
#> 
#>   Simultaneous Tests for General Linear Hypotheses
#> 
#> Fit: feols(fml = mpg ~ hp + wt | cyl, data = mtcars)
#> 
#> Linear Hypotheses:
#>              Estimate Std. Error t value Pr(>|t|)  
#> hp - wt == 0    3.158      1.153   2.739   0.0108 *
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> (Adjusted p values reported -- single-step method)

Created on 2023-11-30 with reprex v2.0.2

martinkoenen commented 7 months ago

Great, thanks so much!

StarryCatte commented 7 months ago

You can use the hypotheses() function from the (excellent) marginaleffects package.

library(fixest)
mod = feols(mpg ~ hp + wt | cyl, data = mtcars)

library(marginaleffects)
hypotheses(mod, "hp = wt")
#> 
#>     Term Estimate Std. Error    z Pr(>|z|)   S 2.5 % 97.5 %
#>  hp = wt     3.16       1.15 2.74  0.00616 7.3 0.898   5.42
#> 
#> Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high

Another option is the multcomp package.

library(multcomp)
#> Loading required package: mvtnorm
#> Loading required package: survival
#> Loading required package: TH.data
#> Loading required package: MASS
#> 
#> Attaching package: 'TH.data'
#> The following object is masked from 'package:MASS':
#> 
#>     geyser
summary(glht(mod, "hp - wt = 0"))
#> 
#>   Simultaneous Tests for General Linear Hypotheses
#> 
#> Fit: feols(fml = mpg ~ hp + wt | cyl, data = mtcars)
#> 
#> Linear Hypotheses:
#>              Estimate Std. Error t value Pr(>|t|)  
#> hp - wt == 0    3.158      1.153   2.739   0.0108 *
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> (Adjusted p values reported -- single-step method)

Created on 2023-11-30 with reprex v2.0.2

I'm using hypotheses() function in marginaleffects package to test whether there are significant difference between two coefficients in negative binomial regression (using fenegbin() function), but I recieved two warnings:

  1. Package fixest is installed, but package version 0.11.2 is required.
  2. In names(model[["coefficients"]]) == names(coefs) : longer object length is not a multiple of shorter object length What should I do to prevent such warnings?
grantmcdermott commented 7 months ago

You need to update your version of fixest. Install the latest release, restart your R session, and then try again.

StarryCatte commented 7 months ago

@grantmcdermott Hi! I updated my R version and install the latest version of fixestnow! Thanks a lot! But I still have the following warning when using hypotheses: In names(model[["coefficients"]]) == names(coefs) : longer object length is not a multiple of shorter object length Do you know why this happened?

I tried the package multcomp and recieved an error: Error in modelparm.default(model, coef. = coef., vcov. = vcov., df = df, : dimensions of coefficients and covariance matrix don't match

These warnings (errors) seem to occur only when fenegbin is used. When I'm using feols, there is no such warning.

grantmcdermott commented 7 months ago

Hmmm, hard to say without a reprex unfortunately. Might be worth putting a quick example together (ideally using a common dataset) so that others can try to debug on their local systems.

StarryCatte commented 7 months ago

I use dataset mtcars to run the following codes:

> data("mtcars")

> mod1 <- feols(mpg ~ hp + wt | cyl, data = mtcars)
> hypotheses(mod1, "hp = wt")

# Term Estimate Std. Error    z Pr(>|z|)   S 2.5 % 97.5 %
#  hp = wt     3.16       1.15 2.74  0.00616 7.3 0.898   5.42

# Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 

Using hypotheses after feols works well. But when using hypotheses after fenegbin, the warnings occured.

> mod2 <- fenegbin(hp ~ drat + wt, data = mtcars)
> hypotheses(mod2, "drat = wt")

# Term Estimate Std. Error     z Pr(>|z|)   S  2.5 % 97.5 %
# drat = wt    -0.37      0.125 -2.97  0.00298 8.4 -0.615 -0.126

# Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 

Warning messages:
1: In names(model[["coefficients"]]) == names(coefs) :
  longer object length is not a multiple of shorter object length
2: In names(model[["coefficients"]]) == names(coefs) :
  longer object length is not a multiple of shorter object length
3: In names(model[["coefficients"]]) == names(coefs) :
  longer object length is not a multiple of shorter object length
4: In names(model[["coefficients"]]) == names(coefs) :
  longer object length is not a multiple of shorter object length

Using fepois works well too.


> mod3 <- fepois(hp ~ drat + wt, data = mtcars)
> hypotheses(mod3, "drat = wt")

#  Term Estimate Std. Error     z Pr(>|z|)    S  2.5 % 97.5 %
#  drat = wt   -0.306     0.0311 -9.83   <0.001 73.3 -0.366 -0.245

# Columns: term, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high 
vincentarelbundock commented 7 months ago

Thanks @StarryCatte I believe that the fenegbin warning was superfluous and that the numerical results should not be affected. The new version of marginaleffects on Github and the next version on CRAN (out soon) should no longer generate this warning.