shannimcg
shannimcg

Reputation: 71

Error in gam function of smoothing spline regression

I'm trying to run a smoothing spline regression using gam for y as a function of a and b, variables in the dataset. But when I run the following code, I get the following error.

> autogam_axb <- gam(data$y~s(data$a,data$b))
Error in eval(expr, envir, enclos) : object 'a' not found

Any idea what I am doing wrong?

Upvotes: 0

Views: 204

Answers (1)

Gavin Simpson
Gavin Simpson

Reputation: 174798

You need to separate the model specification from the locations of the data. The former is specified via the formula, whilst the data argument is used to tell gam about the latter:

autogam_axb <- gam(y ~ s(a, b), data = data)

This serves two purposes:

  1. The specification of the model is clear and not cluttered with data$, which makes it easier to see what it being fitted, and
  2. You are explicit about where the variables required to fit the model are to be found, but you do this in a single place in the function call.

Upvotes: 2

Related Questions