-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfpl_new.qmd
132 lines (104 loc) · 4.05 KB
/
fpl_new.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
---
title: "fplnew"
format: html
editor: visual
---
```{r}
library(tidyverse)
library(jsonlite)
```
```{r}
url <- "https://fantasy.premierleague.com/api/bootstrap-static"
fpl_ret <- fromJSON(url)
summary(fpl_ret)
```
```{r}
player <- fpl_ret$elements
team <- fpl_ret$teams
pos <- fpl_ret$element_types
events <- fpl_ret$events
fpl_ret$element_stats
fpl_ret$elements
```
```{r}
cleaned_players <- read_csv(file = "../Fantasy-Premier-League/data/2023-24/cleaned_players.csv")
```
```{r}
cleaned_players -> last_season
# Add current prices to last season's data
combined <- last_season |> left_join(player |> select(first_name, second_name, "now_cost_new" = now_cost , points_per_game), by= c("first_name", "second_name"))
combined2 <- combined|> mutate(ppm_per_cost = as.numeric(points_per_game) / as.numeric(now_cost_new / 100))
library(ggrepel)
combined2 |>
filter(minutes > (0.5 * max(minutes, na.rm = TRUE))) |>
ggplot(aes(x = minutes,
y = ppm_per_cost,
colour = element_type)) +
geom_smooth(method = "lm",
se = FALSE, alpha = 0.4) +
geom_label_repel(aes(label = second_name), alpha = 0.8) +
labs(title = "Points per match (2023/24) per £m (2024/25 prices)",
subtitle = "Players who played a majority of minutes",
y = "Points per match per £m",
x = "Minutes played",
colour = "Position") +
theme_bw() +
facet_wrap(~element_type)
#
# m1 <- lm(data = df, formula = total_points ~ creativity + influence + threat)
#
# m2 <- lm(data = df, formula = total_points ~ creativity * influence * threat)
#
# m3 <- mgcv::gam(formula = total_points ~ s(creativity, k = 5, bs = "cr") + s(influence, k = 5, bs = "cr") + s(threat, k = 5, bs = "cr"), data = df)
```
```{r}
combined3 <- combined2 |>
mutate(total_per_cost = as.numeric(total_points) / as.numeric(now_cost_new / 100))
combined3 |>
filter(minutes > (0.5 * max(minutes, na.rm = TRUE))) |>
ggplot(aes(x = minutes,
y = total_per_cost,
colour = element_type)) +
geom_smooth(method = "lm",
se = FALSE, alpha = 0.4) +
geom_point() +
geom_label_repel(aes(label = second_name), alpha = 0.8) +
labs(title = "Total points (2023/24) per £m (2024/25 prices)",
subtitle = "Players who played a majority of minutes",
y = "Total points per £m",
x = "Minutes played",
colour = "Position") +
theme_bw() +
facet_wrap(~element_type)
```
```{r}
gameweeks_csvs_paths <- list.files(path = "../Fantasy-Premier-League/data/2023-24/gws/", pattern = "^gw", full.names = TRUE)
gameweeks_csvs_list <- purrr::map(.x = gameweeks_csvs_paths, .f = rio::import)
combined_df <- purrr::imap_dfr(gameweeks_csvs_list, ~ mutate(.x, gw = .y))
```
```{r}
original_prices <- combined_df |>
filter(gw == 1) |>
select(name, "original_price" = value)
player2 <- player |> select(first_name, second_name,
"influence_new" = influence,
"creativity_new" = creativity,
"threat_new" = threat) |>
mutate(name = paste0(first_name, " ", second_name)) |>
left_join(original_prices)
combined_df2 <- combined_df |>
left_join(player2 |> select(name,
influence_new,
creativity_new,
threat_new,
original_price),
by = c("name"))
combined_df3 <- combined_df2 |>
mutate(total_points = if_else(total_points < 0, 0, total_points))
m1 <- lme4::lmer(data = combined_df2, formula = total_points ~ position + original_price + was_home +(1 | team) + (1 | opponent_team))
m2 <- lme4::lmer(data = combined_df2, formula = total_points ~ position * was_home + (1 | team) + (1 | name) + (1 | opponent_team))
m3 <- lme4::glmer(data = combined_df3,
family = poisson(link = "log"), formula = total_points ~ position * was_home + (1 | team) + (1 | name) + (1 | opponent_team))
modelsummary::modelsummary(models = list(m1, m2, m3))
```
Idea: Bayesian model based on previous 4 GWs but priors set based on 2023/24 data, allowing prediction in first GWs.