-
Notifications
You must be signed in to change notification settings - Fork 1
/
many_models.qmd
418 lines (283 loc) · 9.09 KB
/
many_models.qmd
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
---
title: "many_models"
---
source
(https://tim-tiefenbach.de/post/2023-dplyr-many-models/)
```{r}
install.packages("dplyover")
remotes::install_github("TimTeaFan/dplyover")
library(tidyverse) # <- necessary
library(tidyr) # <- necessary
library(broom) # <- necessary
library(rlang) # <- nice to have
library(modelsummary) # <- for output
library(purrr) # <- not really needed
library(dplyover) # <- only for the data
```
```{r}
lookup_vec <- set_names(names(csatraw), names(csat))
csat_named <- csatraw |>
rename(any_of(lookup_vec)) |>
select(cust_id, type, product, csat,
ends_with("rating"))
csat_named_nested <- csat_named %>%
nest_by(product) %>%
mutate(data=list(data))
# make forumla
lm_fomrula <- my_formula <- csat ~ postal_rating + phone_rating + email_rating +
website_rating + shop_rating
#use formula and gather results
csat_prod_nested_res <- csat_named_nested %>%
mutate(
mod = list(lm(my_formula, data = data))
,modstat = list(broom::glance(mod))
,res = list(broom::tidy(mod))
)
```
```{r}
csat_prod_nested_res |>
select(product, modstat) |>
unnest(modstat) |>
select(r.squared, p.value, nobs)
```
```{r}
csat_prod_nested_res |>
select(product, res) |>
unnest(res) |>
filter(term != "(Intercept)")
```
## Advanced features to super charge
### How to add a all to different subcomponents
1. For the dimension you are querying add a "All" component
2. pass this to `bind_rows()` to the original dataframe
```{r}
csat_all <- csat_named |>
mutate(product = "All") |>
bind_rows(csat_named)
```
```{r}
csat_named_nested <- csat_all %>%
nest_by(product) %>%
mutate(
data=list(data)
,mod=list(lm(my_formula,data=data))
,mod2=list(lm(csat~postal_rating,data=data))
,modstat2=list(broom::glance(mod2))
,res=list(broom::tidy(mod))
,modstat=list(broom::glance(mod))
)
csat_named_nested %>%
unnest(modstat2)
```
## how to dynamic filter datasets or augmented based on user input conditions
- Create a list of named arguments that would be passed on filter()
- The names are will be used as row entries later on
- Use `TRUE` to keep all rows (eg no filter)
- If you want to to filter rows / data then you need to use `expr()` to capture the expression that would go directly into filter of the nested dataset
- Write it as if you were directly filtering data in a regular filter, eg `eval(cut=="Fair")`
- Use `expand_grid()` to create pair of each nested table by the filter criteria
- In the framing table use, create a new column with the names (using `names()`) of the list containing the filter object
- Create a new column that filters the data set and evaluates the arguments `filter(data,eval(filter_ls))`
- Remember to use `expr()` for non logical arguments
- Alternative you can directly name the column names from the datamodel
- When doing additional modling on the data objects, it can be difficult to understand what has been done to each data object
- instead of using `list(lm(my_formula,data=data))` you can use `rlang::list2({product}_{type}:=lm(my_formula,data=data))` which makes it easier to know what is going on
- You can nest the formula arguments inside as well by creating a named list for the arguments and directly passing that through
- From there you can use `eval()` in `lm()` to execute the arguments
### Add section on how to do it more dynamically with glue::glue and expr()
- You can use create strings use glue::glue to construct customer arguments
- Then use `rlang::parse_expr()`to execute that argument
```{r}
filter_ls <- list(
All = TRUE,
no_reactivate = expr(type != "reactivate")
)
```
```{r}
cast_all_groups <- csat_all %>%
nest_by(product) %>%
mutate(data=list(data)) %>%
expand_grid(filter_ls) %>%
mutate(
type=names(filter_ls)
,.after = product
)
```
```{r}
named_formula <- list2(standard=my_formula <- csat ~ postal_rating + phone_rating + email_rating +
website_rating + shop_rating)
cast_all_groups %>%
rowwise() %>%
mutate(
data=list(
filter(data,eval(filter_ls))
)
,formula=named_formula
,formula_name=names(named_formula)
,.keep = "unused"
) %>%
mutate(
mod=list2("{product}_{type}":=lm(eval(formula),data=data))
,res=list2("{product}_{type}":=broom::glance(mod))
) %>% unnest(res)
```
## Dynamically create formulas with multiple conditions
- reformulate() is alternative way to create a model argument and can be directly used in a lm()
- reformulate(independent_vars,dependent_vars)
```{r}
lm(
reformulate(
termlabels = c("carat","cut","color","x")
,response = "price"
),data = diamonds
)
```
- Create vector of quoted column names and use expand grid to directly put them in the framing table
- From there you can directly reference the names
```{r}
indep_vars <- c("postal_rating",
"phone_rating",
"email_rating",
"website_rating",
"shop_rating")
csat_all %>%
nest_by(product) %>%
mutate(data=list(data)) %>%
expand_grid(filter_ls,indep_vars) %>%
mutate(
type=names(filter_ls)
,.after = product
) %>%
rowwise() %>%
mutate(
data=list(filter(data,eval(filter_ls)))
,mod=list(lm(reformulate(termlabels=indep_vars,response="csat"),data=data))
,modstat=list(broom::glance(mod))
) %>% unnest(modstat)
```
- Real strength is using this in combination with `update()` to dynamically update the arguments to include more and more arguments
- Create baseline argument
- Create named list of updated vars that you want to incremental add to argument
- Create column of named arguments
- use pattern update(base_formula,reformulate(c(".",update_vars)
- "." says to take all original variables and ad c() and update vars to them
- NULL is wont update anything and is the baseline model
```{r}
my_formula2 <- csat ~ postal_rating + phone_rating + shop_rating
update_vars <- list2(base = NULL,
email = "email_rating",
website = "website_rating+email_rating")
test <- csat_all %>%
nest_by(product) %>%
mutate(data=list(data)) %>%
expand_grid(filter_ls,update_vars) %>%
mutate(
type=names(filter_ls)
,args=names(update_vars)
,.after = type
) %>%
rowwise() %>%
mutate(
form=list(
update(my_formula2,reformulate(c(".",update_vars),intercept = 0))
)
,mod=list(
lm(form,data=data)
)
,modstat=list(
broom::glance(mod)
)
) %>%
unnest(modstat)
head(test$form)
```
# Alternative pattern, Data-less grids
- when you have a column shares a name in the grid and in the underlying datamodel, you can use the .env$col_name to clarify which context you are referring to things
```{r}
product <- c(
"All", unique(csat_named$product)
)
all_grps_grid <- expand_grid(product, filter_ls) |>
mutate(type = names(filter_ls),
.after = product)
```
## Practice
```{r}
library(tidyverse)
filter_args <- rlang::list2(All=TRUE,
low_price=expr(price<2000)
,med_price=expr(price>2000&price<12000)
,high_price=expr(price>12000)
)
diamonds %>%
mutate(color="All") %>%
bind_rows(diamonds) %>%
nest_by(color) %>%
mutate(data=list(data)) %>%
expand_grid(filter_args) %>%
mutate(
filter_names=names(filter_args)
) %>%
rowwise() %>%
mutate(
data=list(filter(data,eval(filter_args)))
,.keep = "unused"
) %>%
mutate(
cor=list(cor(data$carat,data$price))
) %>%
unnest(cor) %>%
select(color,filter_names,cor) %>%
# pivot_wider(names_from=filter_names,values_from=cor) %>%
echarts4r::e_charts(x=filter_names) %>%
echarts4r::e_scatter(serie =cor,legend = TRUE) %>%
# echarts4r::e_scatter(serie =low_price,symbol_size = 10) %>%
# echarts4r::e_scatter(serie =high_price,symbol_size = 10) %>%
echarts4r::e_visual_map(cor)
```
```{r}
library(tidyverse)
cum_sum_vec<- c("price","x","y","z","carat")
input <- "sum(1:10)"
str(expr(input))
eval(parse_expr(input))
args_lst <- rlang::list2(
rlang::call2('cumsum',expr(price))
)
diamonds %>%
nest_by(cut) %>%
mutate(data=list(data)) %>%
expand_grid(input_args=cum_sum_vec) %>%
mutate(args=glue::glue("cumsum({input_args})")) %>%
rowwise() %>%
mutate(
data_mod=list(
mutate(data
,"{input_args}_cumsum":=eval(parse_expr(args))
)
)
) %>%
pull(data_mod) %>% .[[1]]
```
## Reproduce Stackoverflow posts
```{r}
make_data_fixed <- function(df) {
df %>%
mutate(price_cumsum=cumsum(price),
max_price_cumsum=max(price_cumsum))
}
make_data_input <- function(df,x) {
df %>%
mutate("{{x}}_cumsum":=cumsum({{x}}),
"max_{{x}}_cumsum":=max("{{x}}_cumsum")
)
}
selected_cols <- c("clarity","depth")
```
```{r}
library(tidyverse)
diamonds %>%
nest_by(cut) %>%
mutate(data=list(data)) %>%
mutate(mod=list(lm(price~carat,data=data)))
```