`offset` from stats package insert an offset, but `stats::offset` insert a variable
Consider the following example, a negative binomial model with an offset fit by glmmTMB:
# 1. Load the required package
library(glmmTMB)
# 2. Prepare the built-in Salamanders dataset
data("Salamanders")
# For reproducibility, set a seed
set.seed(42)
# Create a mock "area_searched" variable representing plot size (e.g., between 10 and 100 sq meters)
Salamanders$area_searched <- runif(nrow(Salamanders), min = 10, max = 100)
# 3. Fit a zero-inflated negative binomial model with an offset
# We use log(area_searched) to evaluate the count as a rate (salamanders per square meter)
fit_offset <- glmmTMB(
count ~ mined + cover + offset(log(area_searched)) + (1 | site),
ziformula = ~ mined,
family = nbinom2,
data = Salamanders
)
# 4. View the model summary
summary(fit_offset)
This results in
Family: nbinom2 ( log )
Formula: count ~ mined + cover + offset(log(area_searched)) + (1 | site)
Zero inflation: ~mined
Data: Salamanders
AIC BIC logLik -2*log(L) df.resid
1820.0 1851.3 -903.0 1806.0 637
Random effects:
Conditional model:
Groups Name Variance Std.Dev.
site (Intercept) 0.3158 0.562
Number of obs: 644, groups: site, 23
Dispersion parameter for nbinom2 family (): 0.533
Conditional model:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -4.60591 0.39227 -11.742 < 2e-16 ***
minedno 1.67837 0.46724 3.592 0.000328 ***
cover -0.05222 0.18562 -0.281 0.778463
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Zero-inflation model:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.06702 0.44813 -0.150 0.881
minedno -18.22190 3484.00726 -0.005 0.996
so far so good.
But If I now replace offset in the formula with stats::offset, a real regressor (i.e. coefficient not fixed to 1) is inserted:
# 3. Fit a zero-inflated negative binomial model with an offset
# We use log(area_searched) to evaluate the count as a rate (salamanders per square meter)
fit_offset <- glmmTMB(
count ~ mined + cover + stats::offset(log(area_searched)) + (1 | site),
ziformula = ~ mined,
family = nbinom2,
data = Salamanders
)
# 4. View the model summary
summary(fit_offset)
which results in:
Family: nbinom2 ( log )
Formula: count ~ mined + cover + stats::offset(log(area_searched)) + (1 | site)
Zero inflation: ~mined
Data: Salamanders
AIC BIC logLik -2*log(L) df.resid
1743.3 1779.0 -863.6 1727.3 636
Random effects:
Conditional model:
Groups Name Variance Std.Dev.
site (Intercept) 0.03921 0.198
Number of obs: 644, groups: site, 23
Dispersion parameter for nbinom2 family (): 1.05
Conditional model:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.12946 0.62084 -0.209 0.834815
minedno 1.41575 0.38738 3.655 0.000257 ***
cover -0.18904 0.11078 -1.706 0.087928 .
stats::offset(log(area_searched)) -0.06322 0.11289 -0.560 0.575491
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Zero-inflation model:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.5627 0.5122 1.099 0.271930
minedno -2.3698 0.6099 -3.886 0.000102 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Why is that?