I have been having an issue when trying to fit a binomial GAM to data. There are two ways these models can be coded, (i) providing a proportion as the response variable, and the number of trials as weights; and (ii) providing two columns, with successes and failures. I have reason to want to weight my data points (independently of the number of samples). However, I have noticed that if I use approach (ii) and add weights (using the weights
argument), I get very odd results indeed. Furthermore, if I supply the same weights in relative terms (but different absolute magnitudes), I get very different output. This does not happen when using an equivalent GLM model (or, indeed, when using the gam
package). How can I provide a set of weights for the data points?
Here is a MRE:
library('mgcv')
# Random data.
x = 1:100
y_binom = cbind(rpois(100, 5 + x/2), rpois(100, 100))
w = sample(seq_len(100), 100, replace = TRUE)
# GAM models.
m1 = gam(y_binom ~ s(x), family = 'binomial')
m2 = gam(y_binom ~ s(x), weights = w / mean(w), family = 'binomial')
m3 = gam(y_binom ~ s(x), weights = w / sum(w), family = 'binomial')
m4 = gam(y_binom ~ s(x), weights = w * 100, family = 'binomial')
ms = list(m1, m2, m3, m4)
# Different RMSEs.
lapply(X = ms, FUN = function(x) return(sqrt(mean(x$residuals^2))))
# Different predictions, e.g.
plot(predict(m2), predict(m3))
# This does not happen with GLMs.
m1 = glm(y_binom ~ x, family = 'binomial')
m2 = glm(y_binom ~ x, weights = w / mean(w), family = 'binomial')
m3 = glm(y_binom ~ x, weights = w / sum(w), family = 'binomial')
m4 = glm(y_binom ~ x, weights = w * 100, family = 'binomial')
ms = list(m1, m2, m3, m4)
# Same RMSEs (for m2-m4).
lapply(X = ms, FUN = function(x) return(sqrt(mean(x$residuals^2))))
# Same predictions, e.g.
plot(predict(m2), predict(m3))