使用 mgcv GAM 进行二元逻辑回归时出现 offset[[i]] 错误

offset[[i]] error when doing binary logistic regression with mgcv GAM

我正在尝试用二进制响应拟合广义加性模型,使用代码:

library(mgcv)
m = gam(y~s(x1)+s(x2), family=multinom(K=2), data=mydata)

以下是我的部分数据(总样本量为 443):

mydata[1:3,]
   y       x1        x2
1  1 12.55127 0.2553079
2  1 12.52029 0.2264185
3  0 12.53868 0.2183521

但是我收到这个错误:

Error in offset[[i]] : attempt to select less than one element

我的代码有什么问题?

首先,对于二进制响应,为什么不用family = binomial()

其次,如果你想测试multinom,设置K = 1,因为类别被编码从0K。参见 ?multinom但是,对于multinom,您需要传入一个模型公式列表。即使 K = 1,您也需要一个长度为 1 的列表。使用 list(y ~ s(x1) + s(x2)).