generated from tmarumt/r-devcontainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinytable_sample.R
109 lines (94 loc) · 2.62 KB
/
tinytable_sample.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
105
106
107
108
109
library(dplyr)
library(tidyr)
library(tinytable)
dir_post <- here::here()
data <- arrow::read_parquet(file.path(dir_post, "data", "cleaned.parquet")) |>
mutate(
is_died = injury8 == "Died within 24 hours",
is_hospitalized = injury8 %in% c(
"Hospitalization after 24 hours",
"Hospitalization within 24 hours",
"Died within 24 hours"
)
)
tab_count <- data |>
filter(!is.na(weather), !is.na(gender)) |>
summarize(n = n(), .by = c(year, gender, weather)) |>
pivot_wider(names_from = c(gender, year), values_from = n) |>
arrange(weather) |>
select(weather, starts_with("Men"), starts_with("Women"))
tt_count <- tab_count |>
`colnames<-`(c("", rep(2019:2023, 2))) |>
tt() |>
group_tt(
i = list("Good Weather" = 1, "Bad Weather" = 3),
j = list("Men" = 2:6, "Women" = 7:11)
) |>
style_tt(i = c(1, 4), bold = TRUE) |>
format_tt(replace = "-")
tt_count |>
theme_tt("tabular") |>
save_tt(file.path(dir_post, "tex", "table_count.tex"),
overwrite = TRUE
)
tt_count |>
save_tt(file.path(dir_post, "img", "table_count.pdf"),
overwrite = TRUE
)
tt_count |>
save_tt(file.path(dir_post, "doc", "table_count.docx"),
overwrite = TRUE
)
library(modelsummary)
library(fixest)
setFixest_fml(..ctrl = ~ type_person + positive_alcohol + positive_drug | age_c + gender)
models <- list(
"(1)" = feglm(xpd(is_hospitalized ~ ..ctrl),
family = binomial(logit), data = data
),
"(2)" = feglm(xpd(is_hospitalized ~ ..ctrl + type_vehicle),
family = binomial(logit), data = data
),
"(3)" = feglm(xpd(is_hospitalized ~ ..ctrl + type_vehicle + weather),
family = binomial(logit), data = data
),
"(4)" = feglm(xpd(is_died ~ ..ctrl),
family = binomial(logit), data = data
),
"(5)" = feglm(xpd(is_died ~ ..ctrl + type_vehicle),
family = binomial(logit), data = data
),
"(6)" = feglm(xpd(is_died ~ ..ctrl + type_vehicle + weather),
family = binomial(logit), data = data
)
)
modelsummary(models)
cm <- c(
"type_personPassenger" = "Passenger",
"type_personPedestrian" = "Pedestrian",
"positive_alcoholTRUE" = "Positive Alcohol"
)
gm <- tibble(
raw = c(
"nobs", "FE: age_c", "FE: gender",
"FE: type_vehicle", "FE: weather"
),
clean = c(
"Observations", "FE: Age Group", "FE: Gender",
"FE: Type of Vehicle", "FE: Weather"
),
fmt = c(0, 0, 0, 0, 0)
)
tt_reg <- modelsummary(models,
stars = c("+" = .1, "*" = .05, "**" = .01),
coef_map = cm,
gof_map = gm
) |>
group_tt(j = list(
"Hospitalization" = 2:4,
"Died within 24 hours" = 5:7
))
tt_reg |>
save_tt(file.path(dir_post, "img", "table_reg.pdf"),
overwrite = TRUE
)