I am running a gam (mgcv in R) to model a non-linear effect of time on a binomial reponse (positive or negative sample). This is a minimal example of such a model:
gam<- gam(pcr ~ s(rel_time, k=7), family = binomial(link="logit"), data=df)
For one of the species there are exclusively positive samples the last quarter of the predictor range (rel_time), and therefore the predicted real values of p is extremely close to one, as expected. However, then I also get abnormally large predicted confidence intervals (see figure of predicted values, with jittered observations as well, below).
This is how i have calculated predicted values from the model:
new_data <- data.frame(rel_time= seq(from=-33, to=710, by=1))
gam_pred <- bind_cols(new_data, as.data.frame(predict(gam, newdata = new_data, se.fit = TRUE)))
test3_pred$fit_r<- plogis(test3_pred$fit)
test3_pred$up<- plogis(test3_pred$fit+2*test3_pred$se.fit)
test3_pred$down<- plogis(test3_pred$fit-2*test3_pred$se.fit)
It seems that Wilson confidence intervals should be the way to go (Confidence interval around binomial estimate of 0 or 1, but I have no clue how to calculate this when predicting from a gam.
I have experimented with different link functions, but with no major effect. If I run a simple glm, the problem disappears, but clearly this is no option. By the way: If I do not specify k to be smaller than 8, the estimates of p also becomes a strange, and drops down to around 0.2 dispite only postive values:
As should be visible from the figure, the sampling interval in longer towards the second half of the sampling period. I am not convinced that this change, when k is different, is due to the same issue.
I can share the data if needed.
I would appreciate if someone can provide advice on how to deal with this.
gam
help says, as far as I can see, thats
has parametersx
,df
,spar
, but notk
. So I wonder what thek
is doing here. In any case I suspect the problem is with the smoother. Have you triedlo
instead ofs
? Also parameters ofs
may make a difference, but as I wrote, I don't seek
there. $\endgroup$mgcv:gam
. Herek
is the required basis dimension. For a univariate smooth the actual basis used will bek-1
with the default penalty as a sum-to-zero constraint is applied to the smooth. $\endgroup$