mfasiolo / qgam

Additive quantile regression R package
http://mfasiolo.github.io/qgam/
30 stars 7 forks source link

Error when fixing smoothing parameters #38

Closed apmonr closed 4 years ago

apmonr commented 4 years ago

I would like to fix smoothing parameters to calculate the proportion of deviance explained by different model terms (like here), but I get the following error:

set.seed(123) library(qgam) x1 <- runif(100) x2 <- runif(100) y <- x1 + x2 + rnorm(100)

m1 <- qgam(y ~ s(x1) + s(x2), data = data.frame(y = y, x1 = x1, x2 = x2), 0.1) m2 <- qgam(y ~ s(x2), data = data.frame(y = y, x2 = x2), 0.1, argGam = list(sp = m1$sp[2]))

Error in gam(formula = y ~ s(x2), family = elf(qu = NA, co = NA, theta = NA, : formal argument "sp" matched by multiple actual arguments

Is there another way to specify "sp" arguments for qgam?

Thank you for your time, Adrian

mfasiolo commented 4 years ago

Hi Adrian,

Sorry for the delay. Note that QGAMs have an extra tuning parameter (the so-called "learning rate"), relative to standard GAMs. Fixing the smoothing parameters while selecting the learning rate is not a good idea, hence it is not currently allowed by qgam.To do what you want to do, you should fix both the learning rate and the smoothing paramters:

set.seed(123)
library(qgam)
x1 <- runif(100)
x2 <- runif(100)
y <- x1 + x2 + rnorm(100)

m1 <- qgam(y ~ s(x1) + s(x2), data = data.frame(y = y, x1 = x1, x2 = x2), 0.1)

m2 <- qgam(y ~ s(x2), lsig = m1$calibr$lsig, # <- fixing learning rate 
                    data = data.frame(y = y, x2 = x2), 0.1, argGam = list(sp = m1$sp[2])) 

The reason is that the hierarchy of parameters is learning rate -> smoothing parameters -> regression coefficients so letting the learning rate change, while fixing the smoothing parameter is a strange thing to do. Maybe I'll add an explicit warning about this.

Best,

Matteo

apmonr commented 4 years ago

That worked, many thanks!

Adrian