seananderson / glmmfields

R package for Bayesian spatial and spatiotemporal GLMMs with possible extremes
50 stars 10 forks source link

Is there a way to include an offset in a Poisson/NB model? #14

Open robert-fitzsimmons opened 2 years ago

robert-fitzsimmons commented 2 years ago

My spatial unit is US counties, and I would like to include population of the county as an offset in my NB model. Is there a way to do that with your package? I did not see it mentioned in the documentation.

seananderson commented 2 years ago

It wouldn't be hard to add, but no, there currently isn't an offset term included. While Stan gets a bit faster for these types of models, we've been mainly working on a related package https://github.com/pbs-assess/sdmTMB that can fit similar models (and more) but with TMB instead of rstan. The two main limitations are (1) it's not Bayesian and (2) it doesn't do MVT random fields. sdmTMB can do offsets, although it currently does them via the reserved word + offset in the formula (where offset is a column named offset in your data, likely representing the log of population by county in your example) rather than offset(log(x)) as is done in R's glm().

ericward-noaa commented 2 years ago

@robert-fitzsimmons I just added this, per Sean's implementation in sdmTMB https://github.com/seananderson/glmmfields/commit/c639c3f15abce53ff084c5b0d038c6b33be33ec5

To implement the offset, include a column in the dataframe named 'offset', and then add '+ offset' to the formula. Let us know if there's any problems!

seananderson commented 2 years ago

I think I've fixed it here https://github.com/seananderson/glmmfields/commit/37e8c02368f08cecfa01ead7bd8b8a8b35ebbbd8 model.offset() expects offset(x) syntax, and we'd have to remove it from the model matrix. I just added offset as a separate argument now (as glm() allows too) and then carried it through to the predict function. I haven't extensively tested it, but that looks right to me. Of course, offset should likely be log(population) here.