Оцените и выберите 5 моделей, которые показали лучший результат для каждой группы - PullRequest
1 голос
/ 24 мая 2019

Это пример данных.

ind1 <- rnorm(99)
ind2 <- rnorm(99)
ind3 <- rnorm(99)
ind4 <- rnorm(99)
ind5 <- rnorm(99)
dep <- rnorm(99, mean=ind1)
group <- rep(c("A", "B", "C"), each=33)
df <- data.frame(dep,group, ind1, ind2, ind3, ind4, ind5)

Здесь простая линейная регрессионная модель была установлена ​​для каждой комбинации переменных в df после группировки по категориальной переменной.Результатом доволен.Но мои исходные данные имеют более 5 переменных.Трудно увидеть и сравнить результаты в этом списке.Поэтому я хотел бы выбрать 5 лучших моделей для каждой группы из результирующего списка (tibble_list) на основе значения AIC.Буду очень признателен, если кто-нибудь поможет мне в этом.

indvar_list <- lapply(1:5, function(x) 
  combn(paste0("ind", 1:5), x, , simplify = FALSE))

formulas_list <- rapply(indvar_list, function(x)
  as.formula(paste("dep ~", paste(x, collapse="+"))))

run_model <- function(f) {    
  df %>% 
    nest(-group) %>% 
    mutate(fit = map(data, ~ lm(f, data = .)),
           results1 = map(fit, glance),
           results2 = map(fit, tidy)) %>% 
    unnest(results1) %>% 
    unnest(results2) %>% 
    select(group, term, estimate, r.squared, p.value, AIC) %>% 
    mutate(estimate = exp(estimate))
}

tibble_list <- lapply(formulas_list, run_model)
tibble_list

1 Ответ

1 голос
/ 24 мая 2019

Можно было бы связать строки в один набор данных со столбцом .id, затем arrange по 'group', 'AIC', сгруппированным по 'group', filter по строкам, имеющим первые пятьunique 'index'

library(tidyverse)
bind_rows(tibble_list, .id = 'index') %>% 
    arrange(group, AIC) %>% 
    group_by(group) %>% 
    filter(index %in% head(unique(index), 5)) 
# A tibble: 51 x 7
# Groups:   group [3]
#   index group term        estimate r.squared  p.value   AIC
#   <chr> <fct> <chr>          <dbl>     <dbl>    <dbl> <dbl>
# 1 1     A     (Intercept)    0.897     0.319 0.000620  79.5
# 2 1     A     ind1           2.07      0.319 0.000620  79.5
# 3 7     A     (Intercept)    0.883     0.358 0.00129   79.5
# 4 7     A     ind1           2.14      0.358 0.00129   79.5
# 5 7     A     ind3           0.849     0.358 0.00129   79.5
# 6 8     A     (Intercept)    0.890     0.351 0.00153   79.9
# 7 8     A     ind1           2.12      0.351 0.00153   79.9
# 8 8     A     ind4           0.860     0.351 0.00153   79.9
# 9 19    A     (Intercept)    0.877     0.387 0.00237   80.0
#10 19    A     ind1           2.18      0.387 0.00237   80.0
## … with 41 more rows
...