-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofficer_notes.Rmd
171 lines (140 loc) · 3.49 KB
/
officer_notes.Rmd
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
title: "Officer Learning"
date: '`r format(Sys.Date(), "%Y-%m-%d")`'
output:
html_document:
code_folding: hide
theme: flat
toc: yes
toc_depth: 5
toc_float:
collapsed: no
pdf_document:
toc: yes
toc_depth: '5'
editor_options:
chunk_output_type: console
---
# Library
```{r}
library(tidyverse)
library(flextable)
library(officer)
```
# Overview of purrr
```{r}
library(tidyverse)
```
map is just for loop
When to use purrr?
```{r}
iris |>
mutate(sqrt_sepal_length = sqrt(Sepal.Length),
map_sqrt = map_dbl(Sepal.Length, sqrt),
map_log = map_dbl(Sepal.Length, log, base = 3),
base = 3,
map_log_base = map2_dbl(Sepal.Length, base, log))
```
```{r}
library(broom)
iris |>
nest(data = -Species) |>
mutate(model = map(data, ~ lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = .x)),
tidy_model = map(model, tidy)) |>
unnest(cols = c(Species, tidy_model)) |>
ggplot(aes(x = term, y = estimate, color = Species)) +
geom_point()
```
# train a random forest model for each species
```{r}
library(tidymodels)
install.packages("randomForest")
library(randomForest)
# train a random forest model for each species
set.seed(54)
model_data <- iris |>
nest(data = -Species) |>
mutate(data_splits = map(data, initial_split, prop = 0.8),
train_data = map(data_splits, training),
test_data = map(data_splits, testing))
# create model we want to tune
rf_model <- rand_forest(trees = tune()) |>
set_mode("regression") |>
set_engine("randomForest")
rf_grid <- grid_regular(parameters(rf_model))
model_data <- model_data |>
mutate(recipe = map(train_data, .f = function(x){
recipe(Sepal.Length ~ ., data = x)
}),
k_fold_data = map(train_data, vfold_cv))
train_models <- function(recipe, k_fold_data){
tune_grid(
rf_model,recipe,
grid = rf_grid,
resamples = k_fold_data
)
}
model_data <- model_data |>
mutate(tune_results = map2(recipe, k_fold_data, train_models))
model_data <- model_data |>
mutate(parameters = map(tune_results, ~show_best(.x, "rmse", n = 1)),
final_model = map2(parameters, recipe, .f = function(x, y){
workflow() |>
add_model(rf_model) |>
add_recipe(y) |>
finalize_workflow(x)
}),
eval = map2(final_model, data_splits, last_fit))
model_data |>
select(Species, eval) |>
mutate(metrics = map(eval, collect_metrics)) |>
select(Species, metrics) |>
unnest()
```
# create charts
```{r}
iris |>
nest(data = -Species) |>
mutate(chart = map(data, .f = function(x){
ggplot(data = x, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
})) |>
pull(chart)
```
# web scrape
https://github.com/andrew-couch/UFC_Data/blob/main/Scraper.R
```{r}
library(tidyselect)
library(rvest)
"http://ufcstats.com/statistics/events/completed?page=all" |>
read_html() |>
html_elements(".b-link_style_black") |>
html_attr("href") |>
tibble()
```
# Use progress package to track progress
```{r}
library(progress)
sim <- tibble(x = seq.int(1, 1000))
pb <- progress_bar$new(total = nrow(sim))
generic_function <- function(x){
pb$tick()
Sys.sleep(1/100)
x^2
}
sim |>
mutate(x = map_dbl(x, generic_function))
```
```{r}
library(furrr)
plan(multisession, workers = 4)
sim <- tibble(x = seq.int(1, 1000))
parallel_function <- function(sim){
sqrt(sim)
}
begin <- Sys.time()
sim |>
mutate(sim = future_map(sim, parallel_function))
end <- begin - Sys.time()
end
```