mhaddad - 1 year ago 68

R Question

I would like to apply a gam model on a dataset with specifying the types of functions to use.

It would be something like :

`y ~ cst1 * (s(var1)-s(var2)) * (1 - exp(var3*cst2))`

`s`

`var1`

`var2`

`s`

`cst1`

`cst2`

`s`

Is it possible? If not, is there any way (another type of models) i can use to do what i'm looking for?

Thanks in advance for replies.

Answer Source

This model could be fit with `nls`

, the non-linear least squares package. This will allow you to model the formula you want directly. The splines will need to be done manually, though. This question gets at what you would be trying to do.

As far as getting the splines to be the same for `var1`

and `var2`

, you can do this by subtracting the basis matrices. Basically you want to compute the coefficient vector `A`

where the term is `A * s(var1) + A * s(var2) = A * (s(var1) - s(var2))`

. You wouldn't want to just do `s(var1 - var2)`

; in general, `f(x) - f(y) != f(x - y)`

. To do this in R, you would

Compute the spline basis matrices with

`ns()`

for`var1`

and`var2`

,*giving them the same knots*. You need to specify both the`knots`

and the`Boundary.knots`

parameters so that the two splines will share the same basis.Subtract the two spline basis matrices (the output from the

`ns()`

function).Adapt the resulting subtracted spline basis matrix for the

`nls`

formula, as they do in the question I linked earlier.