rtensorgammgcv

r mgcv package : how to set location of knots when using tensor product


I want to set up manually some knots locations in the gam function that uses a tensor product.

For example :

set.seed(0)
n <- 500
x <- runif(n)
z <- runif(n)
y <- runif(n)
gam(y ~ te(x, z), knots = list(x = c(0.2, 0.5, 0.8), y=c(0.2, 0.5, 0.8)))

But I have this error :

Error in smooth.construct.cr.smooth.spec(object$margin[[i]], dat, knt) : 
  number of supplied knots != k for a cr smooth

I tried to add a k value in the tensor, but the error is still the same How can I solve this ? Thank you for your help.


Solution

  • The knot locations you specify via knots and the number of basis functions k must be compatible. In your example, the defaults were used for k which IIRC is to have k = c(5,5) for a 2d tensor product.

    If you want to use the knot locations you specified then you need to set k lower than this:

    knots <- list(x = c(0.2, 0.5, 0.8), z = c(0.2, 0.5, 0.8))
    gam(y ~ te(x, z, k = c(3,3)), method = "REML", knots = knots)