-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimulation.qmd
82 lines (55 loc) · 2.1 KB
/
simulation.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
# Simulation: the Monte Carlo Method {#sec-simulation}
## Getting Started {#sec-simulationGettingStarted}
### Load Packages {#sec-simulationLoadPackages}
```{r}
library("tidyverse")
```
### Load Data {#sec-simulationLoadData}
```{r}
load(file = "./data/players_projectedPoints_seasonal.RData")
```
## Overview {#sec-simulationOverview}
## Simulation of Projected Statistics and Points {#sec-simulationProjectedPoints}
```{r}
calculate_fantasyPoints <- function(df, scoring_rules){
#fantasyPoints <-
#return(fantasyPoints)
}
monte_carlo_simulation <- function(position_df, num_simulations = 10000){
player_ids <- unique(position_df$id)
results <- lapply(player_ids, function(player_id){ # use curly brackets when there are multiple expressions
player_data <- position_df %>% filter(id == player_id)
# Run the simulation for this player
simulations <- replicate(
num_simulations, {
sampled_data <- player_data %>%
summarise(across(
c(pass_yds, pass_tds, rush_yds, rush_tds, rec_yds, rec_tds), # add more statistical categories
~ if (all(is.na(.))) NA else sample(.[!is.na(.)], 1, replace = TRUE)))
sampled_data # return last expression
},
simplify = FALSE)
simulation_df <- dplyr::bind_rows(simulations)
#simulation_df$fantasyPoints <- calculate_fantasyPoints(simulation_df) # write function to calculate fantasy points
ci <- simulation_df %>%
summarise(across(where(is.numeric), list(
lower = \(x) quantile(x, 0.025, na.rm = TRUE),
upper = \(x) quantile(x, 0.975, na.rm = TRUE))))
ci <- ci %>%
mutate(id = player_id)
return(ci)
})
# Combine results for all players
final_ci <- bind_rows(results)
return(final_ci)
}
monte_carlo_simulation(players_projectedPoints_seasonal$QB, num_simulations = 10)
QB_ci <- monte_carlo_simulation(players_projectedPoints_seasonal$QB)
```
## Conclusion {#sec-simulationConclusion}
::: {.content-visible when-format="html"}
## Session Info {#sec-simulationSessionInfo}
```{r}
sessionInfo()
```
:::