![](/img/trans.png)
[英]Use purrr to run multiple regression models with changing outcomes and then extract residuals
[英]Extract residuals from models fit in purrr
我將我的數據分組並為每組擬合了一個模型,我希望得到每組的殘差。 我可以使用 RStudio 的查看器查看每個模型的殘差,但我不知道如何提取它們。 提取一組殘差可以像diamond_mods[[3]][[1]][["residuals"]]
,但是我如何使用 purrr 從每個組中提取一組殘差(與掃帚一起最終得到一個不錯的小費)?
以下是我已經走了多遠:
library(tidyverse)
library(purrr)
library(broom)
fit_mod <- function(df) {
lm(price ~ poly(carat, 2, raw = TRUE), data = df)
}
diamond_mods <- diamonds %>%
group_by(cut) %>%
nest() %>%
mutate(
model = map(data, fit_mod),
tidied = map(model, tidy)
#resid = map_dbl(model, "residuals") #this was my best try, it doesn't work
) %>%
unnest(tidied)
你很接近 - 但你應該使用map()
而不是map_dbl()
因為你需要返回一個列表而不是一個向量。
diamond_mods <- diamonds %>%
group_by(cut) %>%
nest() %>%
mutate(
model = map(data, fit_mod),
tidied = map(model, tidy),
resid = map(model, residuals)
)
使用dplyr
的devel
版本,我們可以在按 'cut' 分組后condense
執行此操作
library(dplyr)
library(ggplot2)
library(broom)
diamonds %>%
group_by(cut) %>%
condense(model = fit_mod(cur_data()),
tidied = tidy(model),
resid = model[["residuals"]])
# A tibble: 5 x 4
# Rowwise: cut
# cut model tidied resid
# <ord> <list> <list> <list>
#1 Fair <lm> <tibble [3 × 5]> <dbl [1,610]>
#2 Good <lm> <tibble [3 × 5]> <dbl [4,906]>
#3 Very Good <lm> <tibble [3 × 5]> <dbl [12,082]>
#4 Premium <lm> <tibble [3 × 5]> <dbl [13,791]>
#5 Ideal <lm> <tibble [3 × 5]> <dbl [21,551]>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.