-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactor.qmd
717 lines (435 loc) · 14.5 KB
/
factor.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
---
title: "factors"
format: html
---
## factor documentation
time units:
- pp = prior period
- cp = current period
- diff = difference between pp and cp
aggregate dimensions
- sum = sum the the value_var
- mean = take the simple row level average of the the value_var
- row = keep the row level granularity without aggregating
- wtdavg = take the wtd. average of the value_var
## how will you use this?
```{r}
factor <- function(.data,formula,target){
return("success")
}
data |>
factor(
volume~(cp(quantity,row)*pp(quantity_prop,row)-pp(quantity-row))*cp(net_price,row)
) |>
## extract each argument one by one
## modify each to add data to it
## executive each to moditfy data an add column
## then store the new colum names and add another column with the name
## how to deal with grups? add in a grouped frame
factor(
price ~ (cp(quantity,row)*pp(quantity_prop,row)-pp(quantity-row))*cp(net_price,row)
) |>
factor(
mix ~ (cp(quantity,row)*pp(quantity_prop,row)-pp(quantity-row))*cp(net_price,row)
) |>
pvm(target=revenue,new_products=FALSE,discontinued_products=TRUE)
data |>
mutate(
volume=(lag_quantity*lag_quantity_prop)-(lag_quantity*net_price)
)
vol(vol ~ pp(quantity,sum) * d(price_realization) )+
price(price ~ d(quantity) * cp(price_realization) )+
mix1(mix1 ~ cp(quantity,sum)) * pp(mix)-cp(quantity))+
mix2(mix2 ~ pp(price_realization) - pp(price_realization,mean))+
mix(mix ~ mix1 * mix2)
```
# lets create some sample data
```{r}
alternative_result <- 10
if (!is.numeric(alternative_result)) {
}
assertthat::assert_that(
is.numeric("alternative_result")
,msg = eval(msg)
)
msg <- cli::cli_alert(text = "tihs is a test")
example_tbl <- function(class) {
vctrs::new_data_frame(
list(
a = letters[1:3],
b = data.frame(c = 1:3, d = 4:6 + 0.5)
),
class = c(class, "tbl")
)
}
library(tibble)
example_tbl("default")
tbl_sum.default_header_extend <- function(x, ...) {
default_header <- NextMethod()
c(default_header, "PVM" = "Summary of PVM mix with inputs how long can this go and util it stops doing this")
}
pillar:::print_tbl(example_tbl("default_header_extend"))
```
```{r}
library(tidyverse)
devtools::load_all()
db <- fpaR::create_contonso_duckdb()
sales_db <- db$sales
sales_tbl <-
fpaR::sales |>
mutate(
month=month(order_date)
) |>
group_by(
month
,product_key
) |>
summarize(
quantity=sum(quantity)
,net_price=sum(net_price)
,.groups="drop"
) |>
group_by(month) |>
arrange(product_key,.by_group = TRUE) |>
ungroup() |>
mutate(
price_realization=net_price/quantity
,mix=net_price/sum(net_price)
,revenue=net_price*quantity
) |>
filter(
month %in% c(1,2)
) |>
select(
month,product_key,quantity,net_price,revenue
)
sales_tbl |>
pivot_wider(
names_from = month
,values_from=quantity:revenue
)
sales_slim_tbl <- sales_tbl |>
group_by(month) |>
mutate(
total_revenue=sum(revenue)
,total_quantity=sum(quantity)
) |>
group_by(product_key) |>
mutate(
lag_quantity=lag(quantity,1,NA)
,lag_revenue=lag(revenue,1,NA)
,lag_net_price=lag(net_price,1,NA)
,lag_total_revenue=lag(total_revenue,1,NA)
,lag_total_quantity=lag(total_quantity,1,NA)
,delta_quantity=quantity-lag_quantity
,delta_net_price=net_price-lag_net_price
,delta_revenue=revenue-lag_revenue
,mix=revenue/total_revenue
,lag_max=lag_revenue/lag_total_revenue
,quantity_prop=quantity/total_quantity
,lag_quantity_prop=lag_quantity/lag_total_quantity
) |>
arrange(product_key) |>
drop_na()
## factor analysis
sales_slim_tbl |>
group_by(month) |>
mutate(
volume_effect=((quantity*lag_quantity_prop)-lag_quantity)*net_price
) |>
ungroup() |>
summarise(
volume_effect=sum(volume_effect)
)
```
vol=(cp(quantity,row)*pp(quantity_prop,row)-pp(quantity-row))*cp(net_price,row)
cp(quantity,row)=quantity
pp(quantity_prop,row)=lag_quantity_prop
cp(net_price,row)=net_price
cp=column_as_is
pp=lag_column_as_is
row=nothing
### Aproach 1
- just have outside functions return column names
```{r}
pp <- function(.data,column,fn="row"){
#original column name
column_name <- rlang::as_name(rlang::enquo(column))
# Capture the column name and aggregation function
column_name_adj <- paste0("lag_",column_name)
if(fn=="row"){
column_expr_adj <- column_name_adj
}else{
column_expr_adj <- rlang::quo_name(
rlang::call2(fn, column_name_adj,na.rm=TRUE)
)
}
if(is.null(attr(.data,"column_name"))){
.data <- structure(.data,column_name=column_name)
}else{
attr(.data,"column_name") <- c(attr(.data,"column_name"),column_name)
}
if(is.null(attr(.data,"column_name_adj"))){
.data <- structure(.data,column_name_adj=column_name_adj)
}else{
attr(.data,"column_name_adj") <- c(attr(.data,"column_name_adj"),column_name_adj)
}
if(is.null(attr(.data,"column_expr_adj"))){
.data <- structure(.data,column_expr_adj=column_expr_adj)
}else{
attr(.data,"column_expr_adj") <- c(attr(.data,"column_expr_adj"),column_expr_adj)
}
return(.data)
}
```
```{r}
cp <- function(.data,column,fn="row"){
column_name <- rlang::as_name(rlang::enquo(column))
# Capture the column name and aggregation function
column_name_adj <- column_name
if(fn=="row"){
column_expr_adj <- column_name_adj
}else{
column_expr_adj <- rlang::quo_name(
rlang::call2(fn, column_name_adj,na.rm=TRUE)
)
}
if(is.null(attr(.data,"column_name"))){
.data <- structure(.data,column_name=column_name)
}else{
attr(.data,"column_name") <- c(attr(.data,"column_name"),column_name)
}
if(is.null(attr(.data,"column_name_adj"))){
.data <- structure(.data,column_name_adj=column_name_adj)
}else{
attr(.data,"column_name_adj") <- c(attr(.data,"column_name_adj"),column_name_adj)
}
if(is.null(attr(.data,"column_expr_adj"))){
.data <- structure(.data,column_expr_adj=column_expr_adj)
}else{
attr(.data,"column_expr_adj") <- c(attr(.data,"column_expr_adj"),column_expr_adj)
}
return(.data)
}
```
```{r}
d <- function(.data,column,fn="row") {
column_name <- rlang::as_name(rlang::enquo(column))
# Capture the column name and aggregation function
column_name_adj <- paste0("delta_",column_name)
if(fn=="row"){
column_expr_adj <- column_name_adj
}else{
column_expr_adj <- rlang::quo_name(
rlang::call2(fn, column_name_adj,na.rm=TRUE)
)
}
if(is.null(attr(.data,"column_name"))){
.data <- structure(.data,column_name=column_name)
}else{
attr(.data,"column_name") <- c(attr(.data,"column_name"),column_name)
}
if(is.null(attr(.data,"column_name_adj"))){
.data <- structure(.data,column_name_adj=column_name_adj)
}else{
attr(.data,"column_name_adj") <- c(attr(.data,"column_name_adj"),column_name_adj)
}
if(is.null(attr(.data,"column_expr_adj"))){
.data <- structure(.data,column_expr_adj=column_expr_adj)
}else{
attr(.data,"column_expr_adj") <- c(attr(.data,"column_expr_adj"),column_expr_adj)
}
return(.data)
}
```
```{r}
mtcars |>
pp(column=quantity,fn = "row") |>
cp(column=price,fn="mean") |>
d(column=volume,fn="row") |> attributes()
```
```{r}
factor <- function(.data,formula){
formula <- y~x+b+c
args <- attr(terms.formula(formula),"variables")
len <- length(call_lst)-1
args_lst <- map(
3:(3+len)
,.f = \(x) call_modify(args[[x]],.data=expr(.data))
) |>
map(\(x) eval_tidy(x))
new_formula_chr <- reduce2(
.x=3:(3+len-1)
,.y=1:len
,.f=\(prev,.x,.y){
prev |>
str_replace(
pattern =fixed(expr_name(args[[.x]]))
,replacement = paste0("args_lst[[",.y,"]]")
)
},.init = rlang::expr_name(rlang::f_rhs(formula))
)
formula_values_vec <- new_formula_chr |> rlang::parse_expr() |> eval_tidy()
data[expr_name(target)] <- formula_values_vec
formula_str <- rlang::expr_name(rlang::f_rhs(formula))
}
```
```{r}
library(tidyverse)
library(rlang)
pp <- function(.data,column,fn="row",time_unit) {
column <- expr(quantity)
fn <- "mean"
column_str <- rlang::as_name(rlang::enquo(column))
# Capture the column name and aggregation function
column_adj_str <- rlang::sym(paste0("lag_",column_str))
## augment table
augment_expr <- rlang::expr(
dplyr::mutate(
dplyr::across(dplyr::all_of(column_str),\(x) dplyr::lag(x,1),.names = "lag_{.col}")
)
)
# calculate a few tests
if(fn=="row"){
out <- list()
out$out <- column_adj_str
out$column_name <- column_str
out$column_name_adj <- rlang::as_name(column_adj_str)
out$augment_expr <- augment_expr
return(out)
}
# Create a dynamic call to wrap the aggregation function around the column
fn_column <- rlang::call2(fn, column_adj_str,na.rm=TRUE)
out <- list()
out$out <- fn_column
out$column_name <- column_str
out$column_name_adj <- rlang::as_name(column_adj_str)
out$augment_expr <- augment_expr
return(out)
}
cp <- function(.data,column,fn="row") {
column_str <- rlang::as_name(rlang::enquo(column))
column_adj_str <- rlang::sym(column_str)
if(fn=="row"){
out <- list()
out$out <- column_adj_str
out$column_name <- column_str
out$column_name_adj <- rlang::as_name(column_adj_str)
return(out)
}
# Create a dynamic call to wrap the aggregation function around the column
fn_column <- rlang::call2(fn, column_adj_str,na.rm=TRUE)
out <- list()
out$out <- fn_column
out$column_name <- column_str
out$column_name_adj <- rlang::as_name(column_adj_str)
return(out)
}
## need to add pp and cp logic to d so that it checks of the columns exists andif not creats it and then creates the delta from it
d <- function(.data,column,fn="row",time_unit) {
pp_out <- pp(.data=.data,column=column,fn=fn,time_unit)
cp_out <- cp(.data=.data,column=column,fn=fn,time_unit)
column_str <- rlang::as_name(rlang::enquo(column))
# Capture the column name and aggregation function
column_adj_str <- rlang::sym(paste0("delta_",column_str))
if(fn=="row"){
out <- list()
out$out <- column_adj_str
out$column_name <- column_str
out$column_name_adj <- rlang::as_name(column_adj_str)
return(out)
}
fn_column <- rlang::call2(fn, column_adj_str,na.rm=TRUE)
out <- list()
out$out <- fn_column
out$column_name <- column_str
out$column_name_adj <- rlang::as_name(column_adj_str)
return(out)
}
d(quantity,"sum")
library(rlang)
factor <- function(formula,data,time_unit,by){
formula <- formula(revenue~pp(quantity,"sum")*cp(net_price,"row"))
formula_terms <- drop.terms(terms(formula),keep.response = FALSE)
formula_terms_call <- attr(formula_terms,"variables")
args_lst <- rlang::eval_tidy(formula_terms_call)
time_unit <- "month"
lhs <- rlang::f_lhs(formula)
#pattern to split by arthmetic operators
call_pattern <- "(?<!\\w)[\\*\\+/](?!\\w)\\s*|\\s+-\\s*"
operator_pattern <- "[-*\\+]+"
## split out the right hand list to seperate call objects as texts
# rhs_text_lst <- strsplit(rhs_text,call_pattern, perl = TRUE) |> purrr::pluck(1)
## excute the call objects to get a list of objects
# args_lst <- purrr::map(rhs_text_lst,rlang::parse_expr) |> purrr::map(rlang::eval_tidy)
## pull out each call objects result individually
# args_call <- map(seq_along(args_lst),\(.x) args_lst |> pluck(.x,"out") |> rlang::expr_text())
# args_column_name <- map(seq_along(args_lst),\(.x) args_lst |> pluck(.x,"column_name"))
# args_column_name_adj <- map(seq_along(args_lst),\(.x) args_lst |> pluck(.x,"column_name_adj"))
## transform tibble
sales_tbl |>
group_by(
dplyr::pick(dplyr::all_of(time_unit))
) |>
group_by(product_key) |>
arrange(product_key,month) |>
mutate(
across(unlist(c(lhs_text,args_column_name)),\(x) lag(x,1),.names = "lag_{.col}")
,across(unlist(c(lhs_text,args_column_name)),\(x) lag(x,1),.names = "delta_{.col}")
)
pak::pak("dplyrover")
all_column_names <- c(lhs_text,args_column_name,args_column_name_adj) |> unlist()
# replace each variable one by one
str_out <- purrr::reduce2(
.x=rhs_text_lst
,.y=args_call
,.f = \(prev,.x,.y){
prev |> gsub(.x,.y,x=_,fixed = TRUE)
}
,.init = rhs_text
)
# add to formula new column
out_obj <- data |>
dplyr::mutate(!!lhs_text:=!!parse_expr(str_out))
return(out_obj)
}
sales_slim_tbl <- sales_tbl |>
group_by(month) |>
mutate(
total_revenue=sum(revenue)
,total_quantity=sum(quantity)
) |>
group_by(product_key) |>
arrange(product_key,month)
mutate(
lag_quantity=lag(quantity,1,NA)
,lag_revenue=lag(revenue,1,NA)
,lag_net_price=lag(net_price,1,NA)
,lag_total_revenue=lag(total_revenue,1,NA)
,lag_total_quantity=lag(total_quantity,1,NA)
,delta_quantity=quantity-lag_quantity
,delta_net_price=net_price-lag_net_price
,delta_revenue=revenue-lag_revenue
,mix=revenue/total_revenue
,lag_max=lag_revenue/lag_total_revenue
,quantity_prop=quantity/total_quantity
,lag_quantity_prop=lag_quantity/lag_total_quantity
) |>
arrange(product_key) |>
drop_na()
assertthat::assert_that(
column_str %in% column_names
,msg = "Please ensure that the column exists in the dataset"
)
assertthat::assert_that(
.data |> dplyr::pull(column_str) |> is.numeric()
,msg = "Please ensure column is numeric"
)
```
total_x
delta_x
lag_x
I want a function in R that uses the below syntax
formula(net_price~cp(sales,quantity,'row') * pp(sales,quantity_prop,'row') - pp(sales,quantity,'row') *pp(sales,quantity,'row'))
where the final result will be a mutate function where net_price (to the left of ~) is the name of the column and each of
cp(sales,quantity,'row'), pp(sales,quantity_prop,'row'),pp(sales,quantity,'row') and pp(sales,quantity,'row') will be captured call objects and executed which will eventually return column names so that mutate will use the armethic indicators against the columns names.