Using AIC for Model Comparison
library(MuMIn)
library(AICcmodavg)
##
## Attaching package: 'AICcmodavg'
## The following objects are masked from 'package:MuMIn':
##
## AICc, DIC, importance
#load in the data set
banding_data <- read.csv("SD_banding_data.csv")
#omit NAs
banding_na <- na.omit(banding_data)
Make a Model
mass_model <- glm(mass ~ species+age+fat+temp+season, data = banding_na, family = gaussian, na.action = na.fail)
Dredge the Model
#construct all models with dredge, compare them based on AICc scores and include species in every model
AICc_models <- dredge(mass_model, rank = "AICc", fixed = "species")
## Fixed terms are "species" and "(Intercept)"
#make a list of models from the dredged data
model_list <- get.models(AICc_models, subset = TRUE)
#iew the first model
model_list[1]
## $`3`
##
## Call: glm(formula = mass ~ fat + 1 + species, family = gaussian, data = banding_na,
## na.action = na.fail)
##
## Coefficients:
## (Intercept) fat speciesAMRO speciesATSP speciesCOYE speciesDICK
## 12.2045 0.8434 66.2555 3.5195 -2.2424 16.6629
## speciesHASP speciesLCSP speciesLISP speciesSAVS speciesSOSP speciesSWSP
## 20.4866 -0.5007 3.1783 4.1756 7.6123 3.1393
## speciesYEWA
## -3.5309
##
## Degrees of Freedom: 609 Total (i.e. Null); 597 Residual
## Null Deviance: 32420
## Residual Deviance: 1052 AIC: 2092
Make a Pretty Table
model_name_list<-NULL #make an empty list
for (i in 1:16){
model_name_list = c(model_name_list, as.character(model_list[[i]][['formula']]))} #loop through model output to extract formula for each model
model_name_listb <- model_name_list[seq(3, length(model_name_list), 3)] #select every third element from list and put it in a new list
modavg_table<-aictab(model_list, modnames = model_name_listb, #label the models with models name list
second.ord = TRUE, #Use AICc instead of AIC
sort = TRUE) #Order based on model weight
#View table
modavg_table
##
## Model selection based on AICc:
##
## K AICc Delta_AICc AICcWt Cum.Wt
## fat + 1 + species 14 2092.53 0.00 0.33 0.33
## age + fat + 1 + species 16 2093.67 1.14 0.18 0.51
## fat + season + 1 + species 15 2094.49 1.96 0.12 0.63
## fat + temp + 1 + species 15 2094.63 2.10 0.11 0.74
## age + fat + season + 1 + species 17 2094.68 2.15 0.11 0.85
## age + fat + temp + 1 + species 17 2095.78 3.26 0.06 0.92
## fat + season + temp + 1 + species 16 2096.59 4.06 0.04 0.96
## age + fat + season + temp + 1 + species 18 2096.79 4.26 0.04 1.00
## age + 1 + species 15 2192.40 99.87 0.00 1.00
## age + temp + 1 + species 16 2193.65 101.13 0.00 1.00
## age + season + 1 + species 16 2194.50 101.97 0.00 1.00
## age + season + temp + 1 + species 17 2195.75 103.23 0.00 1.00
## 1 + species 13 2196.92 104.39 0.00 1.00
## season + 1 + species 14 2197.99 105.46 0.00 1.00
## temp + 1 + species 14 2198.33 105.81 0.00 1.00
## season + temp + 1 + species 15 2199.03 106.51 0.00 1.00
## LL
## fat + 1 + species -1031.91
## age + fat + 1 + species -1030.38
## fat + season + 1 + species -1031.84
## fat + temp + 1 + species -1031.91
## age + fat + season + 1 + species -1029.82
## age + fat + temp + 1 + species -1030.38
## fat + season + temp + 1 + species -1031.83
## age + fat + season + temp + 1 + species -1029.82
## age + 1 + species -1080.79
## age + temp + 1 + species -1080.37
## age + season + 1 + species -1080.79
## age + season + temp + 1 + species -1080.36
## 1 + species -1085.15
## season + 1 + species -1084.64
## temp + 1 + species -1084.81
## season + temp + 1 + species -1084.11