【问题标题】:Why is gamm returning the error "unused argument (offset= ...)"?为什么 gamm 返回错误“未使用的参数(偏移量 = ...)”?
【发布时间】:2020-05-13 22:06:03
【问题描述】:

全部-

第一次发帖,如果我违反了一些提问的惯例(例如,提供可复制的示例),请见谅。

我正在尝试使用带有此代码的“gamm”函数来估计广义加法混合模型:

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)

由于某种原因,“offset”参数似乎没有传递给 gammPQL。我收到此错误:

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

这里是回溯消息:

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"), ...

我尝试在模型中使用偏移量作为术语(请参阅注释掉的代码),但得到了类似的错误。

只是检查代码,有人知道我做错了什么吗?

谢谢, 大卫

【问题讨论】:

    标签: r


    【解决方案1】:

    tl;dr; 在gamm 函数之外创建偏移量,然后使用...+offset() 将其传递给公式。 然后在您的示例中使用:

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

    gams 添加偏移量的一般语法是将其包含在公式中,例如y ~ ... + x + offset(offset_variable)。但是,如下例所示,gamm 似乎在努力解析 offset 函数中的函数(即 log 或除法)。

    一些例子:

    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)
    

    尝试 1:找到 off1,但错误可能是由于 off1 中的大值(我们真的希望转换日志,或使用任何链接函数)

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

    最大 PQL 迭代次数:20
    迭代 1
    迭代 2
    显示回溯
    使用调试重新运行
    na.fail.default(list(Xr.1 = c(-0.00679246534326368, -0.0381904761033802,
    : 对象中的缺失值

    尝试2:logoffset 函数内转换后似乎找不到off1

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

    最大 PQL 迭代次数:20
    迭代 1
    显示回溯
    使用调试重新运行
    eval 中的错误(predvars、data、env):找不到对象“off1”

    尝试3:在offset函数之外定义偏移项

    # 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))
    

    所以在外部创建偏移变量,然后将其传递给gamm 公式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      相关资源
      最近更新 更多