将 broom::glance 的结果附加在一起

appending results together from broom::glance

假设我有这个数据:

df <- structure(list(a_bracket = structure(c(9L, 8L, 9L, 
9L, 9L, 9L), .Label = c("0-15", "16-20", "21-60", "61-100", "101-500", 
"501-1000", "1001-3500", "3501-5000", "5001+"), class = "factor"), b_bracket = structure(c(3L, 
2L, 3L, 4L, 1L, 4L), .Label = c("18-25", "26-35", "36-40", "41-45", 
"46-48", "49-70", "71+"), class = "factor"), gender = structure(c(2L, 
2L, 2L, 2L, 1L, 2L), .Label = c("Female", "Male"), class = "factor"), 
    q1 = structure(c(2L, 2L, 4L, 3L, 1L, 4L
    ), .Label = c("I don't\nlike a thing", 
    "I don't\na thing at all", "I like a\nthing", 
    "Ambivalent about\nthe thing"), class = "factor"), q2 = structure(c(3L, 
    2L, 1L, 1L, 4L, 1L), .Label = c("Neither like\nnor dislike", 
    "Somewhat\ndislike", "Somewhat\nlike", "Strongly\ndislike", 
    "Strongly\nlike"), class = "factor"), q3 = structure(c(2L, 
    2L, 2L, 3L, 2L, 1L), .Label = c("Moderately", "Not at\nall", 
    "Quite", "Slightly", "Very"
    ), class = "factor")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

df

# A tibble: 6 x 6
  a_bracket b_bracket gender q1                            q2                          q3           
  <fct>     <fct>     <fct>  <fct>                         <fct>                       <fct>        
1 5001+     36-40     Male   "I don't\na thing at all"     "Somewhat\nlike"            "Not at\nall"
2 3501-5000 26-35     Male   "I don't\na thing at all"     "Somewhat\ndislike"         "Not at\nall"
3 5001+     36-40     Male   "Ambivalent about\nthe thing" "Neither like\nnor dislike" "Not at\nall"
4 5001+     41-45     Male   "I like a\nthing"             "Neither like\nnor dislike" "Quite"      
5 5001+     18-25     Female "I don't\nlike a thing"       "Strongly\ndislike"         "Not at\nall"
6 5001+     41-45     Male   "Ambivalent about\nthe thing" "Neither like\nnor dislike" "Moderately"

我正在尝试 运行 一系列模型,提取 r 平方和 AIC 并将它们附加到一个新的 df 中,并将因变量的名称作为第三行。

这是我的尝试:

model_stats <- function(data){
  
  mod <- glance(
    lm(as.numeric(data) ~ 
         a_bracket + 
         b_bracket + 
         gender, 
       data = df))

  tibble(
    r_squared = mod %>% select(r.squared),
    AIC = mod %>% select(AIC)
  )
}

map_dfr(
  df %>% 
    select(starts_with("q")), 
    model_stats, 
    .id = "question"
) %>% unnest()

但出于某种原因,我不明白这会针对我 运行ning 的模型数量重复输出 N 次。

有人知道我做错了什么吗?

试试这个 -

library(tidyverse)
library(broom)

model_stats <- function(data){
  
  mod <- glance(
    lm(as.numeric(data) ~ 
         a_bracket + 
         b_bracket + 
         gender, 
       data = df))
  
  tibble(
    r_squared = mod %>% pull(r.squared),
    AIC = mod %>% pull(AIC)
  )

df %>%
  select(starts_with('q')) %>%
  map_df(model_stats, .id = 'question')

# question r_squared   AIC
#  <chr>        <dbl> <dbl>
#1 q1        6.59e- 1  21.8
#2 q2        7.5 e- 1  20.4
#3 q3        2.22e-31  20.4