You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've realized when using predict.fixest() on models fit with femlm, the function basically ignores the offset values and behaves as if offset=1 when the offset is supplied inside the fmla argument in the femlm() function.
library(MASS)
library(fixest)
data(Seatbelts)
# fit models using base/standard functionsfit_glm.pois<- glm(DriversKilled~law+PetrolPrice+ offset(log(kms)), data=Seatbelts, family="poisson")
fit_glm.nb<- glm.nb(DriversKilled~law+PetrolPrice+ offset(log(kms)), data=Seatbelts)
# fit models using fixest functions (with the offest in the formula)fit_fixest.pois.1<- femlm(DriversKilled~law+PetrolPrice+ offset(log(kms)), data=Seatbelts, family="poisson")
fit_fixest.nb.1<- femlm(DriversKilled~law+PetrolPrice+ offset(log(kms)), data=Seatbelts, family="negbin")
# model predictions do not match between base and fixest calls
predict(fit_glm.pois, newdata= as.data.frame(head(Seatbelts, 5)), type="response")
1234578.0359866.5479486.4976696.08875103.57159
predict(fit_fixest.pois.1, newdata= as.data.frame(head(Seatbelts, 5)), type="response")
[1] 0.0086141930.0086594590.0086818890.0087712230.008760178
predict(fit_glm.nb, newdata= as.data.frame(head(Seatbelts, 5)), type="response")
1234580.6890268.8115589.4405899.36119107.09844
predict(fit_fixest.nb.1, newdata= as.data.frame(head(Seatbelts, 5)), type="response")
[1] 0.0089070560.0089540080.0089772740.0090699400.009058483# predict.fixest() behaves as if the offset was always 1seatbelts_offset1<- as.data.frame(head(Seatbelts, 5))
seatbelts_offset1$kms<-1# only showing poisson here, but same behaviour is observed with negative binomial
predict(fit_glm.pois, newdata=seatbelts_offset1, type="response")
123450.0086141930.0086594590.0086818890.0087712230.008760178
predict(fit_fixest.pois.1, newdata=seatbelts_offset1, type="response")
[1] 0.0086141930.0086594590.0086818890.0087712230.008760178
However, this issue doesn't happen if I use the offset argument when the model is fitted in femlm(), and the predict.fixest() calls return the same values as the base/MASS models.
# now fit models with the offset given as a separate argumentfit_fixest.pois.2<- femlm(DriversKilled~law+PetrolPrice, data=Seatbelts,
family="poisson", offset=~log(kms))
fit_fixest.nb.2<- femlm(DriversKilled~law+PetrolPrice, data=Seatbelts,
family="negbin", offset=~log(kms))
# model predictions match now
predict(fit_glm.pois, newdata= as.data.frame(head(Seatbelts, 5)), type="response")
1234578.0359866.5479486.4976696.08875103.57159
predict(fit_fixest.pois.2, newdata= as.data.frame(head(Seatbelts, 5)), type="response")
[1] 78.0359866.5479486.4976696.08875103.57159
predict(fit_glm.nb, newdata= as.data.frame(head(Seatbelts, 5)), type="response")
1234580.6890268.8115589.4405899.36119107.09844
predict(fit_fixest.nb.2, newdata= as.data.frame(head(Seatbelts, 5)), type="response")
[1] 80.6890268.8115589.4405899.36119107.09844
Maybe this is simply the intended behaviour (since offsets do work when supplied in the offset = argument)? However, I do believe this is a bug, since the femlm does seem to recognize the offset term inside of fmla, given that all the coefficients are the same.
Hello again!
I've realized when using
predict.fixest()
on models fit withfemlm
, the function basically ignores the offset values and behaves as ifoffset=1
when the offset is supplied inside thefmla
argument in thefemlm()
function.However, this issue doesn't happen if I use the
offset
argument when the model is fitted infemlm()
, and thepredict.fixest()
calls return the same values as the base/MASS models.Maybe this is simply the intended behaviour (since offsets do work when supplied in the
offset =
argument)? However, I do believe this is a bug, since thefemlm
does seem to recognize the offset term inside offmla
, given that all the coefficients are the same.I think this could be related to issue #270, I am using version 0.10.4. Thanks in advance for any attention into this issue.
PS: thanks a lot for publishing this package! It has been very helpful in speeding up some model fitting in my work :)
The text was updated successfully, but these errors were encountered: