David Crow
David Crow

Reputation: 163

Why is gamm returning the error "unused argument (offset= ...)"?

all-

First-time poster, here, so please be forbearing if I've violated some of the conventions for asking questions (like, for example, providing a replicable example).

I'm trying to estimate a Generalized Additive Mixed Model using the "gamm" function with this code:

fit1.1 = gamm(opioidNonFatalOD ~ s(mandatoryReg.l2, k = 3, fx = TRUE, 
                                         bs = "cr") +
                  s(coalitionActive.l2, k = 3, fx = TRUE, bs = "cr") +
                  monthsSinceJan2011 +
                  everFunded +
                  ICD10 +
                  spoke5 +
                  hub +
                  s(monthly2, bs = "cc", fx = FALSE, k = 4) +
                  s(county2, bs = "re"),
                  #+ offset(log(population / 100000)),
           correlation = corAR1(form = ~ monthsSinceJan2011 | county2),
           data = tsData,
           family = quasipoisson, offset = log(population / 100000),
           niterPQL = 20,
           verbosePQL = TRUE)

For some reason, it looks like the "offset" argument isn't getting passed to gammPQL. I get this error:

iteration 1
Quitting from lines 201-220 (pfs_model_experiments_041520.Rmd) 
Error in lme(fixed = fixed, random = random, data = data, correlation = correlation,  : 
  unused argument (offset = log(population/1e+05))
Calls: <Anonymous> ... withVisible -> eval -> eval -> gamm -> eval -> eval -> gammPQL
Execution halted

Here're the traceback messages:

Error in lme(fixed = fixed, random = random, data = data, correlation = correlation, : unused argument (offset = log(population/1e+05))
4.
gammPQL(y ~ X - 1, random = rand, data = strip.offset(mf), family = family, correlation = correlation, control = control, weights = weights, niter = niterPQL, verbose = verbosePQL, mustart = mustart, etastart = etastart, ...) at <text>#1
3.
eval(parse(text = paste("ret$lme<-gammPQL(", deparse(fixed.formula), ",random=rand,data=strip.offset(mf),family=family,", "correlation=correlation,control=control,", "weights=weights,niter=niterPQL,verbose=verbosePQL,mustart=mustart,etastart=etastart,...)", sep = "")))
2.
eval(parse(text = paste("ret$lme<-gammPQL(", deparse(fixed.formula), ",random=rand,data=strip.offset(mf),family=family,", "correlation=correlation,control=control,", "weights=weights,niter=niterPQL,verbose=verbosePQL,mustart=mustart,etastart=etastart,...)", sep = "")))
1.
gamm(opioidNonFatalOD ~ s(mandatoryReg.l2, k = 3, fx = TRUE, bs = "cr") + s(coalitionActive.l2, k = 3, fx = TRUE, bs = "cr") + monthsSinceJan2011 + everFunded + ICD10 + spoke5 + hub + s(monthly2, bs = "cc", fx = FALSE, k = 4) + s(county2, bs = "re"), ...

I've tried using the offset as a term in the model (see commented-out code), but get a similar error.

Just be inspecting the code, does anyone have an idea of what I'm doing wrong?

Thanks, David

Upvotes: 2

Views: 843

Answers (1)

user20650
user20650

Reputation: 25854

tl;dr; Create the offset outside the gamm function and then pass it to the formula using ...+offset(). In your example then use:

tsData$off = log(tsData$population/100000)
gamm(opioidNonFatalOD ~ <other variables> + s(county2, bs = "re") + offset(off), 
                                                                        <other stuffs>)

The general syntax for gams to add an offset is to include it in the formula, like y ~ ... + x + offset(offset_variable). However, as seen in the examples below it seems as if gammis struggling to parse functions (i.e. the log or division) within the offset function.

Some examples:

library(mgcv)
# create some data
set.seed(1)
dat <- gamSim(6,n=200,scale=.2,dist="poisson")
# create an offset
dat$off1 = (dat$y+1)*sample(2:10, 200, TRUE)

Attempt 1: finds off1 but errors likely due to the large values in off1 (and we really would like the log transfromed, or whichever link function was used)

m1 <- gamm(y~s(x0)+s(x1)+s(x2) + offset(off1), 
           family=poisson,data=dat,random=list(fac=~1))

Maximum number of PQL iterations: 20
iteration 1
iteration 2
Show Traceback
Rerun with Debug
Error in na.fail.default(list(Xr.1 = c(-0.00679246534326368, -0.0381904761033802,
:missing values in object

Attempt 2: can't seem to find off1 after log transform within offset function

m2 <- gamm(y~s(x0)+s(x1)+s(x2) + offset(log(off1)), 
           family=poisson, data=dat,random=list(fac=~1))

Maximum number of PQL iterations: 20
iteration 1
Show Traceback
Rerun with Debug
Error in eval(predvars, data, env) : object 'off1' not found

Attempt 3: define offset term outside offset function

# Success  
dat$off2 = log(dat$off1)
m3 <- gamm(y~s(x0)+s(x1)+s(x2) + offset(off2), 
           family=poisson, data=dat, random=list(fac=~1))

So create the offset variable outside then pass it to the gamm formula.

Upvotes: 3

Related Questions