Search code examples
rgammgcv

Extracting model reliability from multiple GAMs applied across a dataframe


I have been able to apply a General Additive Model iteratively across a dataframe, so where sp_a is the response variable...

sp_a  <- rnorm (100, mean = 3, sd = 0.9)
var_env_1 <- rnorm (100, mean = 1, sd = 0.3)
var_env_2 <- rnorm (100, mean = 5, sd = 1.6)
var_env_3 <- rnorm (100, mean = 10, sd = 1.2)
data <- data.frame (sp_a, var_env_1, var_env_2,var_env_3)
library(mgcv)
Gam <- lapply(data[,-1], function(x) summary(gam(data$sp_a ~ s(x))))

This creates a GAM between the response variable and each explanatory variable iteratively. However, how I would then extract p values or the s.pv from each model. Does anybody know how to do this? Also, it would be great to rank them by their AIC score like this...

Gam1 <- gam(sp_a ~ s(var_env_1))
Gam2 <- gam(sp_a ~ s(var_env_2))
Gam3 <- gam(sp_a ~ s(var_env_3))
AIC(Gam1,Gam2,Gam3)

But selecting this from the original 'Gam' output instead. Thank you for any help in advance.


Solution

  • In the end, it was evident I had to remove the summary option, that then allowed me to calculate AIC score for all models. Other interesting ways of formatting can be found here Using lapply on a list of models, as these functions work for different kinds of models (e.g. lm, glm).

    Gam <- lapply(data[,-1], function(x) gam(data$sp_a ~ s(x)))
    sapply(X = Gam, FUN = AIC)