lrberge / fixest

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

fepois with offset #383

Closed tlcaputi closed 1 year ago

tlcaputi commented 1 year ago

For some reason, I can't get fepois() to run with an offset variable. I get this error:

# Error in fepois(Y ~ X, offset = ~pop, data = df) : 
#   Algorithm failed at first iteration. Step-halving could not find a valid set of parameters.

I am likely doing something wrong, but I tried following along with these instructions: https://github.com/lrberge/fixest/issues/223.

Here is a reproducible example:

library(fixest)
sessionInfo()
# R version 4.1.1 (2021-08-10)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 20.04.1 LTS

# Matrix products: default
# BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
# LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

# locale:
#  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
# [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     

# other attached packages:
# [1] fixest_0.11.1

# loaded via a namespace (and not attached):
#  [1] zoo_1.8-9           compiler_4.1.1      cli_3.4.0          
#  [4] sandwich_3.0-1      dreamerr_1.2.3      Rcpp_1.0.7         
#  [7] nlme_3.1-153        grid_4.1.1          Formula_1.2-4      
# [10] jsonlite_1.7.2      numDeriv_2016.8-1.1 rlang_1.0.6        
# [13] lattice_0.20-44    

# Create dataset
set.seed(1234)
N = 10000
df = data.frame(Y = sample(0:10, size=N, replace = T), X = runif(n=N))
df$pop = floor(runif(N, min = -50, max = 50)) + 10000

# Preview
print(head(df))
#   Y          X   pop
# 1 9 0.71332213 10016
# 2 5 0.11496687  9980
# 3 4 0.86469866  9950
# 4 8 0.45169763  9965
# 5 4 0.99691363  9974
# 6 5 0.06564753  9981

# This works
mod1 = fepois(Y ~ X, data = df)
etable(mod1)

# This also works
mod2 <- glm(Y ~ X + offset(pop), data = df, family = "poisson", maxit = 100)
summary(mod2A)

# This doesn't work
mod3 = fepois(Y ~ X, offset = ~pop, data = df, glm.iter = 100)
# Error in fepois(Y ~ X, offset = ~pop, data = df) : 
#   Algorithm failed at first iteration. Step-halving could not find a valid set of parameters.

Thank you for all of your help and for an excellent package!

tlcaputi commented 1 year ago

I now see that the offset is not automatically logged. This now works. Please feel free to delete this thread. I apologize for the inconvenience.

mod3 = fepois(Y ~ X, offset = ~log(pop), data = df, glm.iter = 100)
lrberge commented 1 year ago

Hi! Indeed, the offset is added in the RHS so should have the appropriate form. Good that you found out! :-)