Question

This

x <- rnorm(100)
y <- rnorm(100) 
gam(y ~ s(x))
## Family: gaussian 
## Link function: identity 

## Formula:
## y ~ s(x)

## Estimated degrees of freedom:
## 1  total = 2 

## GCV score: 0.8116283

breaks down, when VGAM package is loaded:

library(VGAM)
gam(y ~ s(x))
##Error: $ operator is invalid for atomic vectors

Both implement s() function, but this shouldn't happen right? Is this an error in mgcv or VGAM package?

Was it helpful?

Solution

mgcv:gam calls mgcv:interpret.gam which is where the fail is.

interpret.gam seems to parse the formula for special functions, including 's', and then evaluates s(x) in the environment of the formula. Which means it will find whatever the current 's' is from the caller. Which could be something that returns something that gam doesn't like.

You can't fix it like this:

> gam(y ~ mgcv::s(x))
Error in model.frame.default(formula = y ~ mgcv::s(x), drop.unused.levels = TRUE) : 
  invalid type (list) for variable 'mgcv::s(x)'
> gam(y ~ mgcv:::s(x))
Error in model.frame.default(formula = y ~ mgcv:::s(x), drop.unused.levels = TRUE) : 
  invalid type (list) for variable 'mgcv:::s(x)'

But you can like this:

> s=mgcv:::s
> gam(y ~ s(x))

Family: gaussian 
Link function: identity 

Formula:
y ~ s(x)

Estimated degrees of freedom:
1  total = 2 

GCV score: 0.9486058

So its not an error in either package. You asked for a gam with s(x), and happened to have currently defined s(x) to be incompatible with that gam needs. You can't just plug any old function in there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top