0

To ease reproducibility, I am using the goats data set from the ResourceSelection package which has spatial data for used (STATUS == 1) and 'available' (STATUS == 0) GPS locations of mountain goats. ID is for individual (n = 10) and ELEVATION, ... , TASP are attributes of the points.

library(tidyverse)
library(broom)
library(ResourceSelection)
head(goats)
  STATUS ID ELEVATION   SLOPE       ET   ASPECT       HLI      TASP
1      1  1       651 38.5216  35.3553 243.1131 0.9175926 0.9468804
2      1  1       660 39.6927  70.7107 270.0000 0.8840338 0.6986293
3      1  1       316 20.5477  50.0000 279.2110 0.7131423 0.5749115
4      1  1       334 34.0783  35.3553 266.1859 0.8643775 0.7447368
5      1  1       454 41.6187  25.0000 258.3106 0.9349181 0.8292587
6      1  1       343 28.4694 103.0776 237.0426 0.8254866 0.9756112

I am fitting multiple models to each individual and storing the output of each as a separate list-column as shown below.

#Function for model one
Mod1 <- function(df) {
  glm(STATUS ~ SLOPE + I(SLOPE^2) + ASPECT + ET, data = df)
}

#Function for model two without ET
Mod2 <- function(df) {
  glm(STATUS ~ SLOPE + I(SLOPE^2) + ASPECT, data = df)
  }


#Fit the models
ModelFits <- goats %>%
  group_by(ID) %>% 
  nest() %>% 
  mutate(fits1 = map(data, Mod1),
         fits2 = map(data, Mod2),
         glanced1 = map(fits1, glance),
            #Create a dummy column to index model one
            glanced1 = map(glanced1, ~ .x %>% mutate(Mod = "One")),
         glanced2 = map(fits2, glance),
            #Create a dummy column to index model two
            glanced2 = map(glanced2, ~ .x %>% mutate(Mod = "Two")))

For each individual I would like to do model selection and identify which model (Mod1 or Mod2) was ranked higher according to AIC. To this end, I am trying to unnest the two list-columns created with glance and bind them into a separate dataframe. I can do this manually for glanced1 and glanced2 as shown below which creates the desired output summarizing all individual models in a single dataframe.

Mod1DF <- ModelFits %>% 
  unnest(glanced1) %>% 
  #Remove other list-columns
  select(-c(data,  fits1, fits2, glanced2)) %>% 
  as.data.frame()

Mod2DF <- ModelFits %>% 
  unnest(glanced2) %>% 
  #Remove other list-columns
  select(-c(data,  fits1, fits2, glanced1)) %>% 
  as.data.frame()  


Dat <- bind_rows(Mod1DF, Mod2DF)
#There is one model for each model type and individual in `Dat`
table(Dat$Mod)
One Two 
 10  10

However, with many models, this approach is cumbersome. I have tried other approaches but the result binds the columns rather than rows (i.e. goes wide rather than long), for example:

Dat <- ModelFits %>% 
  select(-c(data, fits1, fits2)) %>% 
  unnest(glanced1, glanced2) %>% 
  bind_rows() %>% 
  as.data.frame()

How can I achieve the desired results with a less cumbersome approach?

B. Davis
  • 3,391
  • 5
  • 42
  • 78

1 Answers1

1

You can use gather to convert your wide dataframe to a long format:

ModelFits %>%
  gather("model", "fit", glanced1:glanced2) %>%
  unnest(fit) %>%
  select(ID, null.deviance:Mod)

But a more straightforward path might be to iterate through a list of models:

map_df(list("One" = Mod1, "Two" = Mod2), function(mod) {
  goats %>%
    group_by(ID) %>%
    nest() %>%
    mutate(fits = map(data, mod), glanced = map(fits, glance)) %>%
    select(ID, glanced) %>%
    unnest()
}, .id = "Mod")
Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48