假设我有这样的数据:
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()但由于某些原因,我不明白,这会将输出重复N次,以满足我正在运行的模型数量。
有人知道我哪里做错了吗?
发布于 2021-07-29 23:20:18
试试这个-
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.4https://stackoverflow.com/questions/68578471
复制相似问题