Skip to main content
47 votes
Accepted

Adaptive GAM smooths in mgcv

Most of the extra smooths in the mgcv toolbox are really there for specialist applications — you can largely ignore them for general GAMs, especially univariate smooths (you don't need a random ...
Gavin Simpson's user avatar
25 votes
Accepted

ANOVA to compare models

The output from anova() is a series of likelihood ratio tests. The lines in the output are: The first line in the output corresponds to the simplest model with ...
Gavin Simpson's user avatar
21 votes

Smoothing methods for gam in mgcv package?

mgcv uses a thin plate spline basis as the default basis for it's smooth terms. To be honest it likely makes little difference in many applications which of these you choose, though in some situations ...
Gavin Simpson's user avatar
21 votes
Accepted

R/mgcv: Why do te() and ti() tensor products produce different surfaces?

These are superficially the same model but in practice when fitting there are some subtle differences. One important difference is that the model with ti() terms is ...
Gavin Simpson's user avatar
20 votes
Accepted

Model selection for GAM in R

If you want to select from among a group of covariates, then a principled way of doing this is to put some additional shrinkage on each of the smoothers in the model so that they can be penalised out ...
Gavin Simpson's user avatar
19 votes
Accepted

Generalized additive models (GAMs), interactions, and covariates

Q1 What's the difference between models 3 and 4? Model 3 is a purely additive model $$y = \alpha + f_1(x) + f_2(w) + \varepsilon$$ so we have a constant $\alpha$ plus the smooth effect of $x$ plus ...
Gavin Simpson's user avatar
16 votes
Accepted

Different ways of modelling interactions between continuous and categorical predictors in GAM

gam1 and gam2 are fine; they are different models, although they are trying to do the same thing, which is model group-specific ...
Gavin Simpson's user avatar
15 votes
Accepted

Time series analysis via generalized additive models: model assumptions and stationarity

The idea here is that by estimating the trend as a smooth function, the residuals then are a stationary process and the ARMA model is being estimated in the residuals. In other words, the estimated ...
Gavin Simpson's user avatar
14 votes
Accepted

Choosing k in mgcv's gam()

There is some confusion here and in the answer by @Ira S in that linked post. The default value of the argument k is ...
Gavin Simpson's user avatar
14 votes
Accepted

Random effects in Generalised Additive Model - mgcv package

The random effects in mgcv are proper random effects; there is a way to view random effects, penalized smooths, Gaussian processes, & Gaussian Markov Random Fields all as a Gaussian random field. ...
Gavin Simpson's user avatar
13 votes

Generalized Additive Model interpretation with ordered categorical family in R

In these models, the linear predictor is a latent variable, with estimated thresholds $t_i$ that mark the transitions between levels of the ordered categorical response. The plots you show in the ...
Gavin Simpson's user avatar
13 votes

How to choose the type of GAM-parameters

I'm assuming this is better explained in the 2nd edition of Simon's book (which should be out in a couple of days) as he and his students only worked out some of the theory for this years after Simon ...
Gavin Simpson's user avatar
13 votes
Accepted

Correcting for multiple pairwise comparisons with GAM objects {mgcv} in R

The glht() function for generalized linear hypotheses from the multcomp package can be used to carry out various kinds of ...
Achim Zeileis's user avatar
13 votes
Accepted

GAM factor smooth interaction--include main effect smooth?

You need to be careful with ordered factors here in mgcv as they aren't doing what I think you want to be fitting. If you pass an ordered factor to by, then ...
Gavin Simpson's user avatar
10 votes
Accepted

Gaussian Process smooths in mgcv: choosing between spherical and exponential covariance functions

The gp smooth type is only discussed in the second edition of Simon's book as it was added to the mgcv long after the first edition went to press. The main ...
Gavin Simpson's user avatar
10 votes
Accepted

Why are generalized additive models (GAMs) so popular in ecology?

TL;DR: GAMs are useful models when specific functional relationships are not hypothesized. Ecology as a science (and like many other sciences, particularly population sciences) often has hypotheses ...
Alexis's user avatar
  • 30.3k
9 votes

GAM model selection

If you are using an extra penalty on each term, you can just fit the model and you are done (from the point of view of selection). The point of these penalties is allow for shrinkage of the perfectly ...
Gavin Simpson's user avatar
9 votes

GAM with categorical variables - interpretation

In a factor by variable smooth, like other simple smooths, the bases for the smooths are subject to identifiability constraints. If you just naively computed the ...
Gavin Simpson's user avatar
9 votes
Accepted

Why is my design matrix rank deficient? (modelling seasonal data with a cyclic spline)

cyclicSpline already contains the constant vector in its span so if you additionally add an intercept it'll be rank deficient. ...
jld's user avatar
  • 20.5k
9 votes
Accepted

by group random effect GAM

Your base model is incorrectly specified; factor by smooths must have the by factor included as a parametric categorical term in ...
Gavin Simpson's user avatar
9 votes
Accepted

Why order of tensors in equation changes results of GAM?

The order shouldn't matter, but your model is not well specified. I suspect it's because the main effects of year are in both smooths and hence you have the same ...
Gavin Simpson's user avatar
8 votes
Accepted

Are GAM models linear in the parameters?

Yes, GAMs are linear in the parameters. If we ignore the estimation of smoothness parameters, once we have created the bases for all the covariates we want to fit a smooth effects of, a GAM is just ...
Gavin Simpson's user avatar
8 votes
Accepted

visreg visualization of mgcv results (GAM)

The difference arises because you are ignoring the intercept (& the coef for the non-reference levels of the factor; see first Note) when you go via the ...
Gavin Simpson's user avatar
8 votes
Accepted

Plotting GAMs on Response Scale with Multiple Smooth and Linear Terms

If the model contains z then the effect of x estimated by the model is that given z is in ...
Gavin Simpson's user avatar
8 votes
Accepted

y-axis values in plot(gam)

The model is a generalization of the generalized linear model – it's not a true GLM as we have the extra parameter the defines the extra dispersion that the NB has over the Poisson – and the ...
Gavin Simpson's user avatar
8 votes
Accepted

MGCV summary function extremely slow

The test for random effects is extremely costly computation-wise, especially so if there are lots of levels in the random effect factor. Use ...
Gavin Simpson's user avatar
8 votes
Accepted

Random wiggly smooths for 100s of factor levels

A couple of things you can do: Turn on a form of multithreaded fitting: use the nthreads argument or a multithreaded BLAS - see the Details section of ...
Gavin Simpson's user avatar
7 votes

mgcv::gam overfitting

The problem is what you mean by 'smooth' here. If you really want a curve that is smooth w.r.t. time and passes through the spike in the data at time 1 then it will have to vary enormously on the y ...
Simon Wood's user avatar
7 votes
Accepted

Simulating Responses from fitted Generalized Additive Model

I'll illustrate with the classic 4 term data set oft used to illustrate GAMs, but will only simulate data from the strongly nonlinear term $f(x_2)$ as it is easy to visualise the process with a single ...
Gavin Simpson's user avatar
7 votes
Accepted

GAM optimization methods in mgcv R package - which to choose?

This question is quite old now so perhaps some of the detail of {mgcv} has changed but I don't believe that is the case. That given, there are some inconsistencies in the stated facts. {mgcv} selects ...
Gavin Simpson's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible