Properly nesting these things is going to be difficult in general but potentially not necessary if the reason for nesting is just to compare the models.
Essentially what you are asking is if the effect of x
on y
is wiggly (non-linear) or will a linear effect suffice.
You can actually test this given the assumptions of the generalised likelihood ratio test for GAMs (see ?anova.gam
) and given the basis you used here, a thin plate spline, there should be correct nesting as the basis includes a linear basis function such that you could set all other parameters for basis functions to 0 and the models would be nested (except for the disappearance of the smoothness parameter in the linear version of the model).
I'm not 100% sure how this would work for the other bases in mgcv; there doesn't appear to be any basis function that is a linear function in the cubic regression spline basis (bs = 'cr'
) in the sense that you can zero all coefficients other than the one for the linear function.
You can also test this idea directly with a single model by modifying the thin plate spline basis when fitting only the second model.
gam(y ~ x + s(x, bs = "tp", m = c(2,0)), family = "gaussian", ...)
The m
bit sets up the thin plate regression spline basis (bs = "tp"
, the default so your models used this anyway) with a second order penalty (again the default penalty is on the integrated squared second derivative of the smooth evaluated over the range of $x$), but no null space (the functions that are perfectly smooth). This is shown below
The above image shows the bases for your m2
(left) and for my model (right). Notice that the pink linear basis function going from bottom left to top right in the left hand figure is missing from the right hand figure, and the basis functions themselves have been modified accordingly.
As such, you can use the no-null-space model parameterisation to test for a non-linear effect using summary()
.
If you are interested in this more generally, then nesting is not assured in models with say
y ~ s(x) + s(z)
vs
y ~ te(x, z)
again because there aren't terms in the te()
basis that map to terms in the individual s()
bases. In such cases, the ti()
basis can be used to get proper nesting:
y ~ s(x) + s(z) + ti(x, z)
where in the ti()
basis the "main" smooth effects of $x$ and $z$ have been removed from the usual tensor product basis.
I should also note that you can use AIC()
to compare the models without needing them to be strictly nested.