Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand data with estimated fixed effects #144

Closed
pp2382 opened this issue May 14, 2021 · 2 comments
Closed

Expand data with estimated fixed effects #144

pp2382 opened this issue May 14, 2021 · 2 comments

Comments

@pp2382
Copy link

pp2382 commented May 14, 2021

Thanks so much for your work Laurent!

I am estimating a Poisson regression with several fixed effects and could not find a method to extract each fixed effects and merge them with their observation.
The function fixef extracts the FE but I could not find a way to merge them back to the initial dataset.

Am I missing something? Is there a simple way to assign the correct FE to each observation? It does not seem easy with the current format of the output of fixef.

Thanks very much

@pp2382 pp2382 closed this as completed May 17, 2021
@lrberge
Copy link
Owner

lrberge commented May 19, 2021

Hi and thanks for your kind words!

Indeed, this is not possible currently. That's because the use case is fairly limited so I didn't implement it.

But maybe you've found a solution by now?

@pp2382
Copy link
Author

pp2382 commented Jun 1, 2021

Yes, I did manage to find a way. Here is the code I use in case it can be useful for someone else (even though I am still learning my way around R)

##I first create my fixed effects (method doesn't work with combination of fe using ^)

dt[,var1_x_var2 := .GRP , by=c("var1","var2")]
dt[,var3_x_var4 := .GRP , by=c("var3","var4")]

##Then I run my regression

pois_reg <- fepois(Y ~ X1 |  var1_x_var2 + var3_x_var4 , data=dt)

##I use fixef to extract the fe and convert them in data.table
fe <- fixef(pois_reg)
fe1<-data.table(names = names(fe$var1_x_var2 ), fe1=fe$var1_x_var2 )
fe1[,names:=as.numeric(names)]

fe2<-data.table(names = names(fe$var3_x_var4), alpha=fe$var3_x_var4)
fe2[,names:=as.numeric(names)]

##Finally, I merged the two tables of fixed effects with the initial one

dt<-merge(dt,fe1,by.x=c("var1_x_var2"),by.y=c("names"),all.x=T)
dt<-merge(dt,fe2,by.x=c("var3_x_var4"),by.y=c("names"),all.x=T) 

It seemed to work for me, but I am not sure how general this solution is.

Thanks for following up and thanks again for a great package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants