I've run a for-loop in R that generates models for a binomial GAM for 200 different random data combinations (200 different set.seed values).
The for-loop and GAMs run just fine, and they store the models in the appropriate list, model[[i]]
, with each list element representing a model for a certain set.seed iteration.
I can run summary()
on an individual list element (model[[5]]
, for example) and get something like this:
Approximate significance of smooth terms:
edf Ref.df Chi.sq p-value
s(Petal.Width) 1.133e+00 9 5.414 0.008701 **
s(Sepal.Length) 8.643e-01 9 2.338 0.067509 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
R-sq.(adj) = 0.361 Deviance explained = 32.7%
-REML = 83.084 Scale est. = 1 n = 160
Since I've got 200 of these elements in my model
list, I was wondering if there's a quick way to go through and count how many times (out of the total 200) that Chi.sq value is equal to 0 for the Petal.Width variable. Basically, since I have the GAMs set up with bs = "cs"
, the number of times that Chi.sq is equal to 0 represents how often the variable-selection process removed that variable from the model.
Here's a cleaned-up version of the code I used for the for-loop to build the model:
a <- c(1:200)
model <- list()
for (i in a) {
#In my version there's some extra code here that subsets iris based on i
model[[i]] <- gam(Species~ s(Petal.Width, bs="cs") +
s(Sepal.Length, bs="cs"),
data = iris,
family = binomial,
method = "REML")
}