We are hoping for some guidance regarding a gam model using mgcv R package. We want to know if variables measured over time affect our outcome variable. In other words if variable "X" and/or variable "Y" changes after controlling for time.
We have the following variables:
- ID - subject identification
- year - continuous covariate, repeated measures
- Var X - continuous variable
- Var Y - continuous variable
- Outcome - continuous variable, dependent variable
We are not entirely sure if the following model answers our question:
m1 <- gam(Outcome ~ s(VarX) + s(VarY) + s(year) +
s(year, ID, bs = "fs"),
family = gaussian, data = dat, method = 'REML')
Or if the model below is the one taking into account that each variable is measured over time for each ID:
m2 <- gam(Outcome ~ s(VarX) + s(VarY) + s(year) +
s(year, ID, bs = "fs", by = VarX) +
s(year, ID, bs = "fs", by = VarY),
data = dat, method = 'REML')
"fs"smooths aren't defined correctly; you wants(VarX, ID, bs = 'fs'), i.e. the factor is supplied after the continuous covariate. What you are doing is essentially equivalent toID + s(VarX, by = ID). If you want separate smoothness parameters over the levels ofID, then drop the"fs"bit and just useID + s(VarX, by = ID). But you can't mix these with the data the OP has as there is but one level in the hierarchy. More generally one can havef1 + s(x1, f2, bs = "fs", by = f1)where f1 and f2 are factors, but that isn't the case here. – Gavin Simpson Aug 13 '21 at 07:32s(VarX, ID, bs = 'fs'), which is not equivalent tos(X, by = ID, bs = 'fs')? Is it uncouth to correct my answer to reflect the correction, or should I just leave it as it? – Sean Hardison Aug 13 '21 at 22:45