Skip to content

Commit

Permalink
namer
Browse files Browse the repository at this point in the history
  • Loading branch information
perlatex committed Dec 25, 2020
1 parent ea3ddbe commit 7ac5558
Show file tree
Hide file tree
Showing 60 changed files with 1,875 additions and 1,864 deletions.
126 changes: 63 additions & 63 deletions adv_dplyr.Rmd

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions beauty_of_across.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dplyr 1.0版本增加了`across()`函数,这个函数集中体现了dplyr宏



```{r message=FALSE, warning=FALSE}
```{r beauty-of-across-1, message=FALSE, warning=FALSE}
library(tidyverse)
library(palmerpenguins)
penguins
Expand All @@ -14,7 +14,7 @@ penguins
��唳獢���憭撩憭勗€潘��€閬�霈⊥�銝€�撩憭勗€潛��圈�嚗��批虜閫���


```{r}
```{r beauty-of-across-2}
penguins %>%
summarise(
na_in_species = sum(is.na(species)),
Expand All @@ -29,7 +29,7 @@ penguins %>%
```
撟訾��唳獢��銝�憭��芣�8��憒��唳獢������停���暑鈭��隞��憭蝎斐銋捆��€�瑟�嚗�隞祈�嗆�啁`summarise_all()`嚗�

```{r}
```{r beauty-of-across-3}
penguins %>%
summarise_all(
~ sum(is.na(.))
Expand All @@ -38,7 +38,7 @@ penguins %>%

�箏末��€�Y揣嚗�隞祆��隡�蝐餃���嚗��霈∪��敺�桃��€潘�餈葵憟質秩嚗�亙�隞��

```{r}
```{r beauty-of-across-4}
penguins %>%
group_by(species) %>%
summarise(
Expand All @@ -52,7 +52,7 @@ penguins %>%
�€`summarise_if()`�瑟�


```{r}
```{r beauty-of-across-5}
d1 <- penguins %>%
group_by(species) %>%
summarise_if(is.numeric, mean, na.rm = TRUE)
Expand All @@ -63,7 +63,7 @@ d1
�寞�銝�嚗�霂凋�銝�蝞�憟賜�閫�€� 雿�鈭��year`, ��灼summarise_if()`銝剔 `is.numeric & !year`�餅�`year`嚗瘝⊥��€犖蝐餌�甈脫��舀�蝛瑞�嚗�隞祈��€閬�霈⊥�蝏�隡��葵�堆��嗅��僎�唬�韏瑯€�甇歹��賑���


```{r}
```{r beauty-of-across-6}
d2 <- penguins %>%
group_by(species) %>%
summarise(
Expand All @@ -73,7 +73,7 @@ d2
```

�€��撟�
```{r}
```{r beauty-of-across-7}
d1 %>% left_join(d2, by = "species")
```

Expand All @@ -85,12 +85,12 @@ d1 %>% left_join(d2, by = "species")

`across()`��堆�霈抵�銝€��敺���皜嚗��V�甇亙����其�嚗�甇交�摰�

```{r out.width = '75%', echo = FALSE}
```{r beauty-of-across-8, out.width = '75%', echo = FALSE}
knitr::include_graphics("images/across_cover.jpg")
```


```{r}
```{r beauty-of-across-9}
penguins %>%
group_by(species) %>%
summarise(
Expand All @@ -103,7 +103,7 @@ penguins %>%
## across()�賣敶W�

`across()`�賣嚗���銝芯蜓閬��嚗�
```{r, eval = FALSE}
```{r beauty-of-across-10, eval = FALSE}
across(.cols = , .fns = , .names = )
```

Expand Down Expand Up @@ -153,7 +153,7 @@ across(.cols = , .fns = , .names = )
### 瘙�銝€��蝻箏仃�潭��

撠望�祉�撘€憪��€瘙�
```{r, eval=FALSE}
```{r beauty-of-across-11, eval=FALSE}
penguins %>%
summarise(
na_in_species = sum(is.na(species)),
Expand All @@ -168,7 +168,7 @@ penguins %>%
```


```{r}
```{r beauty-of-across-12}
# using across()
penguins %>%
summarise(
Expand All @@ -189,7 +189,7 @@ penguins %>%

### 瘥葵蝐餃���銝�憭�蝏�

```{r}
```{r beauty-of-across-13}
penguins %>%
summarise(
distinct_species = n_distinct(species),
Expand All @@ -214,7 +214,7 @@ penguins %>%
### 憭�憭葵蝏恣�賣


```{r}
```{r beauty-of-across-14}
penguins %>%
group_by(species) %>%
summarise(
Expand Down Expand Up @@ -246,7 +246,7 @@ penguins %>%
鈭�銝�餈��畔across()`銝summarise()`�撩憭抒��絲��


```{r}
```{r beauty-of-across-15}
penguins %>%
group_by(species, island) %>%
summarise(
Expand Down Expand Up @@ -283,7 +283,7 @@ penguins %>%

### 銝���銝憭���霈�

```{r}
```{r beauty-of-across-16}
# using across()
penguins %>%
group_by(species) %>%
Expand All @@ -299,7 +299,7 @@ penguins %>%

### �唳������

```{r}
```{r beauty-of-across-17}
std <- function(x) {
(x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
}
Expand All @@ -326,7 +326,7 @@ penguins %>%

### �唳撖寞����

```{r}
```{r beauty-of-across-18}
# using across()
penguins %>%
drop_na() %>%
Expand All @@ -351,7 +351,7 @@ penguins %>%
### �典�蝏遣璅∩葉銝cur_data()`��雿輻


```{r}
```{r beauty-of-across-19}
penguins %>%
group_by(species) %>%
summarise(
Expand Down Expand Up @@ -388,7 +388,7 @@ penguins %>%
### 銝cur_column()`��雿輻


```{r}
```{r beauty-of-across-20}
# 瘥���隞亙��芰�蝟餅
df <- tibble(x = 1:3, y = 3:5, z = 5:7)
mult <- list(x = 1, y = 10, z = 100)
Expand Down Expand Up @@ -427,7 +427,7 @@ df %>% mutate(

### 銝c_across()`��銋暺�

```{r}
```{r beauty-of-across-21}
df <- tibble(x = 1:3, y = 3:5, z = 5:7)
df %>%
Expand All @@ -450,13 +450,13 @@ df %>%
- �唳獢葉���銵��雿�
- 銝��扯捶��雿���臭誑銝€韏瑕��綽�銝�left_join()`

```{r out.width = '90%', echo = FALSE, fig.cap = "across()函数总结图"}
```{r beauty-of-across-22, out.width = '90%', echo = FALSE, fig.cap = "across()�賣�餌��<U+3E65>"}
knitr::include_graphics("images/across.png")
```



```{r, echo = F}
```{r beauty-of-across-23, echo = F}
# remove the objects
# ls() %>% stringr::str_flatten(collapse = ", ")
Expand All @@ -465,7 +465,7 @@ rm(cutoffs, d1, d2, df, mult, std, weights)



```{r, echo = F, message = F, warning = F, results = "hide"}
```{r beauty-of-across-24, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```

42 changes: 21 additions & 21 deletions broom.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

还是用第 \@ref(ggplot2-geom) 章的`gapminder`案例

```{r}
```{r broom-1}
library(tidyverse)
library(gapminder)
gapminder
Expand All @@ -17,15 +17,15 @@ gapminder
### 可视化探索

画个简单的图
```{r}
```{r broom-2}
gapminder %>%
ggplot(aes(x = log(gdpPercap), y = lifeExp)) +
geom_point(alpha = 0.2)
```

我们想用**不同的模型**拟合`log(gdpPercap)``lifeExp`的关联

```{r}
```{r broom-3}
library(colorspace)
model_colors <- colorspace::qualitative_hcl(4, palette = "dark 2")
Expand Down Expand Up @@ -58,7 +58,7 @@ ggplot(
### 简单模型

还是回到我们今天的主题。我们建立一个简单的线性模型
```{r}
```{r broom-4}
out <- lm(
formula = lifeExp ~ gdpPercap + pop + continent,
data = gapminder
Expand All @@ -67,12 +67,12 @@ out
```


```{r, eval=FALSE}
```{r broom-5, eval=FALSE}
str(out)
```


```{r}
```{r broom-6}
summary(out)
```

Expand All @@ -83,7 +83,7 @@ knitr::include_graphics("images/lm-object-schematic.png")
```

我们发现`out`对象包含了很多元素,比如系数、残差、模型残差自由度等等,用读取列表的方法可以直接读取
```{r, eval=FALSE}
```{r broom-7, eval=FALSE}
out$coefficients
out$residuals
out$fitted.values
Expand All @@ -100,7 +100,7 @@ out$fitted.values
为规整模型结果,这里我们推荐用[David Robinson](http://varianceexplained.org/about/) 开发的`broom`宏包。


```{r message = FALSE, warning = FALSE}
```{r broom-8, message = FALSE, warning = FALSE}
library(broom)
```

Expand All @@ -118,13 +118,13 @@ library(broom)

### tidy

```{r}
```{r broom-9}
tidy(out)
```



```{r}
```{r broom-10}
out %>%
tidy() %>%
ggplot(mapping = aes(
Expand All @@ -137,14 +137,14 @@ out %>%


可以很方便的获取系数的置信区间
```{r}
```{r broom-11}
out %>%
tidy(conf.int = TRUE)
```



```{r}
```{r broom-12}
out %>%
tidy(conf.int = TRUE) %>%
filter(!term %in% c("(Intercept)")) %>%
Expand All @@ -166,12 +166,12 @@ out %>%
`augment()`会返回一个数据框,这个数据框是在原始数据框的基础上,增加了模型的拟合值(`.fitted`), 拟合值的标准误(`.se.fit`), 残差(`.resid`)等列。


```{r}
```{r broom-13}
augment(out)
```


```{r}
```{r broom-14}
out %>%
augment() %>%
ggplot(mapping = aes(x = lifeExp, y = .fitted)) +
Expand All @@ -185,7 +185,7 @@ out %>%
`glance()` 函数也会返回数据框,但这个数据框只有一行,内容实际上是`summary()`输出结果的最底下一行。


```{r}
```{r broom-15}
glance(out)
```

Expand All @@ -195,15 +195,15 @@ glance(out)

broom的三个主要函数在分组统计建模时,格外方便。

```{r}
```{r broom-16}
penguins <-
palmerpenguins::penguins %>%
drop_na()
```



```{r}
```{r broom-17}
penguins %>%
group_nest(species) %>%
mutate(model = purrr::map(data, ~ lm(bill_depth_mm ~ bill_length_mm, data = .))) %>%
Expand All @@ -213,7 +213,7 @@ penguins %>%



```{r}
```{r broom-18}
fit_ols <- function(df) {
lm(body_mass_g ~ bill_depth_mm + bill_length_mm, data = df)
}
Expand All @@ -231,7 +231,7 @@ out_tidy



```{r}
```{r broom-19}
out_tidy %>%
ggplot(aes(
x = species, y = estimate,
Expand All @@ -246,13 +246,13 @@ out_tidy %>%



```{r, echo = F}
```{r broom-20, echo = F}
# remove the objects
# rm(list=ls())
rm(out, out_tidy, penguins, model_colors, fit_ols)
```


```{r, echo = F, message = F, warning = F, results = "hide"}
```{r broom-21, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```
Loading

0 comments on commit 7ac5558

Please sign in to comment.