Questions tagged [gam]

GAM (Generalized Additive Model) is a statistical model used to combine Generalized Linear Models with Additive Models.

GAM (Generalized Additive Model) is a statistical model used to combine Generalized Linear models with Additive Models. It is found in both the CRAN packages gam and mgcv for the R language.

683 questions
28
votes
5 answers

Error in model.frame.default: variable lengths differ

On running a gam model using the mgcv package, I encountered a strange error message which I am unable to understand: “Error in model.frame.default(formula = death ~ pm10 + Lag(resid1, 1) + : variable lengths differ (found for 'Lag(resid1,…
Meso
  • 1,375
  • 5
  • 17
  • 36
27
votes
4 answers

Is it possible to plot the smooth components of a gam fit with ggplot2?

I am fitting a model using gam from the mgcv package and store the result in model and so far I have been looking at the smooth components using plot(model). I have recently started using ggplot2 and like its output. So I am wondering, is it…
unique2
  • 2,162
  • 2
  • 18
  • 23
19
votes
1 answer

mgcv: How to set number and / or locations of knots for splines

I want to use function gam in mgcv packages: x <- seq(0,60, len =600) y <- seq(0,1, len=600) prova <- gam(y ~ s(x, bs='cr') can I set the number of knots in s()? and then can I know where are the knots that the spline used? Thanks!
memy
  • 229
  • 1
  • 2
  • 8
18
votes
1 answer

How to extract fitted splines from a GAM (`mgcv::gam`)

I am using GAM to model time trends in a logistic regression. Yet I would like to extract the the fitted spline from it to add it to another model, that cannot be fitted in GAM or GAMM. Thus I have 2 questions: How can I fit a smoother over time so…
Tom
  • 305
  • 2
  • 4
  • 15
11
votes
0 answers

GAM error: Fitting terminated with step failure - check results carefully

I’m writing a GAM using the mgcv package that predicts burrow abundance and distribution of two different species on an island using data obtained during a field trip and images taken from the Sentinel satellite. 101 plots were surveyed. 922 burrows…
Bong112
  • 161
  • 10
11
votes
1 answer

gam plots with ggplot

I need to create some gam plots in ggplot. I can do them with the general plot function, but am unsure how to do with ggplot. Here is my code and plots with the regular plot function. I'm using the College data set from the ISLR package. train.2 <-…
Marvin Bahr
  • 135
  • 1
  • 2
  • 11
11
votes
1 answer

Caret package - cross-validating GAM with both smooth and linear predictors

I would like to cross validate a GAM model using caret. My GAM model has a binary outcome variable, an isotropic smooth of latitude and longitude coordinate pairs, and then linear predictors. Typical syntax when using mgcv is: gam1 <- gam( y ~ s(lat…
Paul Lantos
  • 113
  • 1
  • 6
11
votes
1 answer

How to identify the function used by geom_smooth()

I would like to display a plot created by geom_smooth() but it is important for me to be able to describe how the plot was created. I can see from the documentation when n >= 1000, gam is used as the smoothing function, but I cannot see how many…
A Toll
  • 647
  • 7
  • 15
10
votes
2 answers

Force GAM model fit to be monotonic and go through a fixed point (x0, y0) with R mgcv

I am trying to fit a GAM model to data under two constraints simultatenously: (1) the fit is monotonic (increasing), (2) the fit goes through a fixed point, say, (x0,y0). So far, I managed to have these two constraints work separately: For (1),…
Marta Karas
  • 4,967
  • 10
  • 47
  • 77
10
votes
1 answer

Error in gam function in names(x) <- value: 'names' attribute must be the same length as the vector

I am using the mgcv package to model the ozone pollution concentration according to some environmental covariates. The model takes the form : model1 <- gam(O3 ~ s(X, Y, bs = "tp", k = 10) + wd + s(date, bs = "cc", k = 100) + district, …
王嘉炜
  • 101
  • 1
  • 1
  • 3
10
votes
1 answer

GAM model error

My data frame looks like: head(bush_status) distance status count 0 endemic 844 1 exotic 8 5 native 3 10 endemic 5 15 endemic 4 20 endemic 3 The count data is non-normally distributed. I'm…
Fbj9506
  • 231
  • 3
  • 11
9
votes
1 answer

How to use sample weights in GAM (mgcv) on survey data for Logit regression?

I'm interesting in performing a GAM regression on data from a national wide survey which presents sample weights. I read with interest this post. I selected my vars of interest generating a DF: nhanesAnalysis <- nhanesDemo %>% …
Borexino
  • 802
  • 8
  • 26
9
votes
1 answer

Why is bam from mgcv slow for some data?

I am fitting the same Generalized Additive Model on multiple data sets using the bam function from mgcv. While for most of my data sets the fit completes within a reasonable time between 10 and 20 minutes. For a few data sets the run take more than…
unique2
  • 2,162
  • 2
  • 18
  • 23
8
votes
1 answer

stat_smooth gam not the same as gam {mgcv}

I was using the stat_smooth function in ggplot2, decided I wanted the "goodness of fit", and used a mgcv GAM for that. It occurred to me that I should check to make sure that they were the same model (stat_smooth vs mgcv's gam), so I used the code…
CJ9
  • 91
  • 1
  • 6
8
votes
3 answers

Is it possible to include the product of two smooth terms in a mgcv gam model

I have had great success using gam to model seasonality for time series data. My latest model clearly shows a weekly pattern in addition to seasonal changes. While the weekly pattern itself is very stable over the year, its amplitude also varies…
unique2
  • 2,162
  • 2
  • 18
  • 23
1
2 3
45 46