Closed ericward-noaa closed 1 year ago
I think the fix of this is easy. glmmTMB
has copied glm
in adding this drop.unused.levels to model frames, e.g.
glmmTMB: https://github.com/glmmTMB/glmmTMB/blob/0dffb36351116e68328a0c31bf49ee99f0c92b6b/glmmTMB/R/glmmTMB.R#L1016 glm: https://github.com/SurajGupta/r-source/blob/a28e609e72ed7c47f6ddfbb86c85279a0750f0b7/src/library/stats/R/glm.R#L50
But we haven't added that. Needs to be done after the model frame is created, https://github.com/pbs-assess/sdmTMB/blob/f344e1fbb60497487cd53c9079482842f2ea4791/R/fit.R#L810
Actually - there's an easier solution than messing with the model.frame. Base R has the droplevels() function, which strips levels from a dataframe. Propose we just use this function on the inputted df, e.g.
droplevels(df)
Are we sure droplevels()
will behave the same in edge conditions like predicting with missing factor levels or new factor levels and that this will behave the same as glm? Maybe we should just use the same approach as glm? And perhaps we should add unit tests to make sure behaviour with prediction edge cases behaves the same as glm?
On the other hand, if we can show that prediction edge cases behave the same then I guess either approach is fine.
Yeah -- I had done some testing earlier, confirming that droplevels()
behaves similarly to glm. I added those to the tests to test-random-intercepts.R, and seems to be behaving as we expect
There's a funny situation where if data are filtered, but contain original factor names, the coefficients are estimated for the original names / levels instead of those in the data passed in. For example, here's a simple data frame with 3 levels, and can model them as characters or factors. In the third case I've added an extra level (e.g. perhaps data was filtered out in pre-processing)
Existing lm/glm/glmmTMB packages all fit these models fine, and don't try to estimate the coefficient for "c"
sdmTMB works fine with the first 2 examples using a factor or character. But for the case of the extra level, it tries to estimate the coefficient (and won't converge!)
I'll look into how lm/glmm/glmmTMB process these cases