I'm trying to fit a generalized additive model with a binary response, using the code:
library(mgcv)
m = gam(y~s(x1)+s(x2), family=multinom(K=2), data=mydata)
Below is a part of my data (total sample size is 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
But I receive this error:
Error in offset[[i]] : attempt to select less than one element
What is wrong with my code?
First of all, for binary response, why not use family = binomial()
?
Secondly, if you want to test multinom
, set K = 1
, because categories are coded from 0 to K
. See ?multinom
. However, you need to pass in a list of model formulae, for multinom
family. Even if K = 1
, you would need a length-1 list. Use list(y ~ s(x1) + s(x2))
.