-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecture11.R
105 lines (82 loc) · 1.83 KB
/
lecture11.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
library(usethis)
library(dplyr)
library(tidyr)
library(dbplyr)
library(magrittr)
library(ggplot2)
library(knitr)
library(devtools)
library(tidyverse)
library(pacman)
library(ggthemes)
library(gdata)
library(griffen)
library(hrbrthemes)
library(gt)
library(gtExtras)
library(webshot2)
library(broom)
p_load(gapminder,ggthemes,Hmisc,wesanderson,ggridges)
##########
gap_lifeExp <-
gapminder |>
group_by(continent) |>
summarise(lifeExp_mean = mean(lifeExp), lifeExp_sd = sd(lifeExp))
gap_lifeExp |>
gt()
growth <- function(a){
c <- lm(log(gdpPercap) ~ year, a)
return(c)
}
gapminder_nested <-
gapminder |>
group_by(continent) |>
nest()
gapminder_nested <-
gapminder_nested |>
mutate(model = map(data,growth))
gapminder_nested <-
gapminder_nested |>
mutate(tidy_model = map(model,tidy))
gapminder_model <-
gapminder_nested |>
select(continent, tidy_model)
gapminder_coef <-
gapminder_model |>
unnest(cols = c("tidy_model"))
gapminder_ycoef <-
gapminder_coef |>
filter(term == "year") |>
select(continent, estimate, std.error, p.value)
gapminder_ycoef <-
gapminder_ycoef |>
mutate(estimate = roundr(estimate))
gapminder_ycoef <-
gapminder_ycoef |>
mutate(estimate = if_else(p.value<0.05, paste0(estimate,"*"), estimate))
gapminder_ycoef <-
gapminder_ycoef |>
mutate(std.error = roundr(std.error))
gapminder_ycoef <-
gapminder_ycoef |>
mutate(std.error = paste0("(", std.error,")"))
gapminder_ycoef <-
gapminder_ycoef |>
rename("Growth rate" = estimate) |>
select(-p.value) |>
ungroup()
gap_tbl <-
gapminder_ycoef |>
gt()
gap_tbl <-
gap_tbl |>
tab_style(
style = cell_text(align = "left"),
locations = cells_stub()
)
gap_tbl <-
gap_tbl |>
cols_merge(columns = c("Growth rate", "std.error"), pattern = "{1}<br>{2}")
gap_tbl <-
gap_tbl |>
gt_theme_538()