I have this mixed effects GAM regression in R (mgcv):
library(mgcv) gam_beta <- gam( y ~ te(time, x1) + te(time, x2) + s(time, by = city) + s(city, bs = "re"), data = my_data, method = "REML", family = betar(link = "logit") ) I tried to write the equation for this model (based on my understanding of tensor product functions te in `mgcv https://www.rdocumentation.org/packages/mgcv/versions/1.9-1/topics/te):
$$ y_{ij} \sim \text{Beta}(\mu_{ij}\phi, (1-\mu_{ij})\phi) $$ $$\mu_{ij} = \frac{1}{1+e^{-\eta_{ij}}}$$ $$ \text{logit}(\mu_{ij}) = \eta_{ij}= \beta_0 + f_{12}(time_{ij}, x1_{ij}) + f_{34}(time_{ij}, x2_{ij}) + h_i(time_{ij}) + b_i $$
I tried to expand this a bit more:
$$ \eta_{ij} = \underbrace{\sum_{k=1}^{K_1} \hat{\gamma}_{1k}f_{1k}(time_{ij}) + \sum_{l=1}^{L_1} \hat{\gamma}_{2l}g_{1l}(x1_{ij}) + \sum_{k=1}^{K_1}\sum_{l=1}^{L_1} \hat{\gamma}_{3kl}f_{1k}(time_{ij})g_{1l}(x1_{ij})}_{\text{first te(): time and x1 terms}} + $$
$$ \underbrace{\sum_{m=1}^{M_1} \hat{\gamma}_{4m}f_{2m}(time_{ij}) + \sum_{n=1}^{N_1} \hat{\gamma}_{5n}g_{2n}(x2_{ij}) + \sum_{m=1}^{M_2}\sum_{n=1}^{N_2} \hat{\gamma}_{6mn}f_{2m}(time_{ij})g_{2n}(x2_{ij})}_{\text{second te(): time and x2 terms}} + $$
$$ \underbrace{\sum_{p=1}^P \hat{\alpha}_{ip}h_p(time_{ij})}_{\text{city-specific smooth}} + \underbrace{\hat{b}_i}_{\text{random effect}} $$
Here is my question: I know that the te function in mgcv` includes both the main effects and the interaction effects (GAM Regression: Interactions vs Main Effects?). But won't this mean that there is a risk of a certain term being double counted and appearing twice in the GAM equation?
For example, I have te(time,x1) and te(time,x2). This is because I wanted to include time interactions with covariates x1 and x2 in my model. But will this result in the main effects of time being included twice? Or does mgcv recognize this and only include this once?
te()includes the main effects and interaction $\endgroup$