第19章 使用purrr和broom处理多个模型

前一章写了单一模型如何构建,这一章主要介绍如何解决大量模型数据

本章将介绍 3 种功能强大的方法来帮助我们游刃有余地处理大量模型。
• 使用多个简单模型来更好地理解复杂数据集。
• 使用列表列在数据框中保存任意数据结构。例如,可以通过这种方法让数据列中包含线
性模型。
• 使用 David Robinson 开发的 broom 包将模型转换为整洁数据。这是一种非常强大的技术,
可以处理大量模型,因为一旦有了整洁数据,我们在本书前面学到的所有技术就有用武
之地了。

以下各节将详细介绍本章要使用的各种技术。
• 19.2 节将介绍关于列表列的数据结构知识,以及可以将列表放在数据框中的原因。
• 19.3 节将介绍创建列表列的 3 种主要方法。
• 19.4 节将介绍如何将列表列还原为常用的原子向量(或原子向量集合),以便更容易进
行处理。
• 19.5 节将介绍由 broom 包提供的一整套工具,以及它们在其他类型数据结构上的使用
方法。

本读书笔记也将围绕着书中的结构进行记录。

我的数学、统计学、R语言都不好..所以这章对我来说很难理解。但是就像书中所写的那样本章属于进阶内容,如果本书是你的第一本 R 语言书, 那么本章将是一个严峻挑战。它需要你对建模、数据结构和迭代都有深刻理解。因此,如果看不懂本章内容,也不要担心——先将它放在一边,如果几个月后想做做思维训练,就再来读读看。哈哈哈哈 加油

准备工作

加载用于数据分析的tidyverse包和用于构建模型的modelr包

library(modelr)
library(tidyverse)

gapminder

The gapminder data summarizes the progression of countries over time, looking at statistics like life expectancy and GDP. The data is easy to access in R, thanks to Jenny Bryan, who created the gapminder package

世界经济情况介绍

In this case study, we’re going to focus on just three variables to answer the question “How does life expectancy (lifeExp) change over time (year) for each country (country)?”
country: factor with 142 levels
continent: factor with 5 levels
year: ranges from 1952 to 2007 in increments of 5 years
lifeExp:life expectancy at birth, in years
pop:population
gdpPercap:GDP per capita (US$, inflation-adjusted)

gapminder %>%
ggplot(aes(year, lifeExp, group = country)) + # 设置线条分组
geom_line(alpha = 1/3) #设置线和透明图
==
ggplot (data = gapminder, mapping= aes (year, lifeExp, group = country))) +
geom_line(alpha = 1/3)
预期寿命随年份变化图

This is a small dataset: it only has ~1,700 observations and 3 variables. But it’s still hard to see what’s going on! 下面将重点关注一下线性关系

nz <- filter(gapminder, country == "New Zealand")
nz %>%
ggplot(aes(year, lifeExp)) +
geom_line() +
ggtitle("Full data = ")
nz_mod <- lm(lifeExp ~ year, data = nz)
nz %>%
add_predictions(nz_mod) %>%
ggplot(aes(year, pred)) +
geom_line() +
ggtitle("Linear trend + ")
nz %>%
add_residuals(nz_mod) %>%
ggplot(aes(year, resid)) +
geom_hline(yintercept = 0, color = "white", size = 3) +
geom_line() +
ggtitle("Remaining pattern")

全数据= 预期值 +残差

这是知道一个国家 可以筛选出来符合标准的 但是How can we easily fit that model to every country?

嵌套数据

https://campus.datacamp.com/courses/exploratory-data-analysis-in-r-case-study/tidy-modeling-with-broom?ex=10

可以跟着这个练习 也不错

Extract out the common code with a function and repeat using a map function from purrr.

by_country <- gapminder %>%
group_by(country, continent) %>%
nest()
嵌套数据

运用nest函数创建了一个按照contry和continent分组的多个list数据组成的data.frame

列表列

列表列是隐式定义在数据框中的:数据框是由相同长度的向量组成的命名列表。一个列表就是一个向量,因此将列表作为数据框的一列是完全合理的。


数据结构
country_model <- function(df) {
lm(lifeExp ~ year, data = df)
}
models <- map(by_country$data, country_model)

In other words, instead of creating a new object in the global envi‐ ronment, we’re going to create a new variable in the by_country data frame. That’s a job for dplyr::mutate():

by_country <- by_country %>%
mutate(model = map(data, country_model))
by_country
#> # A tibble: 142 × 4
#> country continent data model
#>    
#> 1 Afghanistan Asia  
#> 2 Albania Europe  
#> 3 Algeria Africa  

摘要函数

summarize() 函数的一个局限性是,只能使用返回单一值的摘要函数。这意味着我们不能使用像 quantile() 这样的函数,因为它会返回任意长度的向量

嵌套还原

还原前

还原后

你可能感兴趣的:(第19章 使用purrr和broom处理多个模型)