dfClong=dfC %>% rename(C_pre_12_8=C_basal_temporada) %>% select(CODE,contains("_pre_")) %>% pivot_longer(-CODE) %>%
mutate(mesDia=str_replace(name,"C_pre_([0-9]+)_([0-9]+)","\\2-\\1")) %>%
separate(col = "mesDia",into = c("mes","dia"),sep = "-") %>%
mutate(mes=as.integer(mes),dia=as.integer(dia),ano=as.integer(ifelse(mes>=7,2022,2023))) %>%
mutate(Fecha=make_date(month = mes,day = dia, year=ano)) %>%
select(CODE,Fecha,Cortisol=value) %>% filter(complete.cases(.))%>%
group_by(CODE) %>% mutate(CortisolZ=scale(Cortisol)) %>% ungroup()
dfClong %>% head()
## # A tibble: 6 × 4
## CODE Fecha Cortisol CortisolZ[,1]
## <chr> <date> <dbl> <dbl>
## 1 COD1 2022-08-12 4.8 0.421
## 2 COD1 2022-08-18 3.99 -0.123
## 3 COD1 2022-08-26 3.23 -0.634
## 4 COD1 2022-08-30 4.3 0.0852
## 5 COD1 2022-09-07 5.6 0.958
## 6 COD1 2022-09-15 6.29 1.42
dfTlong=dfT %>% rename(T_pre_12_8=T_basal_temporada) %>% select(CODE,contains("_pre_")) %>% pivot_longer(-CODE) %>%
mutate(mesDia=str_replace(name,"T_pre_([0-9]+)_([0-9]+)","\\2-\\1")) %>%
separate(col = "mesDia",into = c("mes","dia"),sep = "-") %>%
mutate(mes=as.integer(mes),dia=as.integer(dia),ano=as.integer(ifelse(mes>=7,2022,2023))) %>%
mutate(Fecha=make_date(month = mes,day = dia, year=ano)) %>%
select(CODE,Fecha,Testosterona=value) %>% filter(complete.cases(.)) %>%
group_by(CODE) %>% mutate(TestosteronaZ=scale(Testosterona)) %>% ungroup()
dfTlong %>% head()
## # A tibble: 6 × 4
## CODE Fecha Testosterona TestosteronaZ[,1]
## <chr> <date> <dbl> <dbl>
## 1 COD1 2022-08-12 298 1.66
## 2 COD1 2022-08-18 254 0.832
## 3 COD1 2022-08-26 277 1.26
## 4 COD1 2022-08-30 287 1.45
## 5 COD1 2022-09-07 267 1.08
## 6 COD1 2022-09-15 250 0.757
Vamos a Unir datos de partidos con los datos de T y C del del mismo día o anteriores Hay muchos entrenamientos, antes de una toma de Cortisol o Testosterona, pero enlazamos con la más cercana en el tiempo posterior o en el mismo dia. Añadimos la restricción que la medición de hormona no puede hacerse más de 6 días antes del partido para considerarse válida. (No queremos que se usen las hormonas de dos partidos posteriores en caso de que alguna vez no se haya hecho la medida.)
comp_date <- function(x, y) x > y
comp_code <- function(x, y) x == y
dfPartidos_CAnterior <- dfPartidos %>% select(Fecha) %>%
fuzzy_left_join(dfClong , by = c("Fecha" = "Fecha"),
match_fun = list(comp_date)) %>%
group_by(Fecha.x) %>%
filter(Fecha.y == max(Fecha.y)) %>%
ungroup() %>%
rename(Fecha=Fecha.x,Fecha_C=Fecha.y) %>%
select(CODE,Fecha,Fecha_C,everything())
dfPartidos_TAnterior <- dfPartidos %>% select(Fecha) %>%
fuzzy_left_join(dfTlong , by = c("Fecha" = "Fecha"),
match_fun = list(comp_date)) %>%
group_by(Fecha.x) %>%
filter(Fecha.y == max(Fecha.y)) %>%
ungroup() %>%
rename(Fecha=Fecha.x,Fecha_T=Fecha.y) %>%
select(CODE,Fecha,Fecha_T,everything())
dfPartidos_CT=dfPartidos %>% inner_join(dfPartidos_CAnterior) %>%
inner_join(dfPartidos_TAnterior) %>%
filter(Fecha<Fecha_C+7,Fecha<Fecha_T+7)
## Joining with `by = join_by(Fecha)`
## Warning in inner_join(., dfPartidos_CAnterior): Each row in `x` is expected to match at most 1 row in `y`.
## ℹ Row 2 of `x` matches multiple rows.
## ℹ If multiple matches are expected, set `multiple = "all"` to silence this
## warning.
## Joining with `by = join_by(Fecha, CODE)`
dfPartidos_CT %>% head(10)
## # A tibble: 10 × 11
## Fecha Resul…¹ Puntos Acumu…² CODE Fecha_C Corti…³ Corti…⁴ Fecha_T
## <date> <fct> <dbl> <dbl> <chr> <date> <dbl> <dbl> <date>
## 1 2022-08-19 L 0 0 COD1 2022-08-18 3.99 -0.123 2022-08-18
## 2 2022-08-19 L 0 0 COD10 2022-08-18 3.61 -0.373 2022-08-18
## 3 2022-08-19 L 0 0 COD11 2022-08-18 4.32 -0.677 2022-08-18
## 4 2022-08-19 L 0 0 COD12 2022-08-18 6.37 -0.380 2022-08-18
## 5 2022-08-19 L 0 0 COD13 2022-08-18 3.21 -0.807 2022-08-18
## 6 2022-08-19 L 0 0 COD16 2022-08-18 3.92 -0.654 2022-08-18
## 7 2022-08-19 L 0 0 COD19 2022-08-18 3.94 -0.813 2022-08-18
## 8 2022-08-19 L 0 0 COD20 2022-08-18 2.34 -1.04 2022-08-18
## 9 2022-08-19 L 0 0 COD21 2022-08-18 3.2 -0.583 2022-08-18
## 10 2022-08-19 L 0 0 COD22 2022-08-18 2.45 -1.28 2022-08-18
## # … with 2 more variables: Testosterona <dbl>, TestosteronaZ <dbl[,1]>, and
## # abbreviated variable names ¹Resultado, ²Acumulado, ³Cortisol,
## # ⁴CortisolZ[,1]
En este punto no estoy seguro de que sea lo más conveniente, pero nos olvidamos de los jugadores y nos quedamos con las medias en cada fecha de partido y sus respectivas mediciones de T y C anteriores. Podríamos quedarnos con todos los datos, jugador a jugador para un análisis multinivel, pero esto es lo que tenemos escrito que vamos a hacer:
dfPartidos_CT_mean=dfPartidos_CT %>% select(-CODE) %>% group_by(Fecha,Resultado,Puntos,Acumulado) %>% summarise_all(mean,na.rm=T)
dfPartidos_CT_mean
## # A tibble: 27 × 10
## # Groups: Fecha, Resultado, Puntos [27]
## Fecha Resultado Puntos Acumulado Fecha_C Cortisol Cortis…¹ Fecha_T
## <date> <fct> <dbl> <dbl> <date> <dbl> <dbl> <date>
## 1 2022-08-19 L 0 0 2022-08-18 3.84 -0.650 2022-08-18
## 2 2022-09-02 L 0 3 2022-08-30 6.72 0.584 2022-08-30
## 3 2022-09-09 L 0 3 2022-09-07 6.96 0.667 2022-09-07
## 4 2022-09-16 L 0 3 2022-09-15 6.93 0.650 2022-09-15
## 5 2022-09-23 D 1 4 2022-09-18 9.42 1.84 2022-09-18
## 6 2022-10-07 D 1 6 2022-10-05 6.72 0.442 2022-10-05
## 7 2022-10-12 L 0 6 2022-10-08 6.74 0.505 2022-10-08
## 8 2022-10-21 L 0 9 2022-10-18 3.90 -0.856 2022-10-18
## 9 2022-10-28 L 0 9 2022-10-26 5.59 0.108 2022-10-26
## 10 2022-11-01 L 0 9 2022-10-26 5.59 0.108 2022-10-26
## # … with 17 more rows, 2 more variables: Testosterona <dbl>,
## # TestosteronaZ <dbl>, and abbreviated variable name ¹CortisolZ
dfPartidos_CT_mean %>% ggplot(aes(y=Cortisol,x=Fecha))+
geom_point(aes(color=Resultado),alpha=0.5,size=2)+
geom_smooth(se = FALSE)+
scale_color_manual(values=c("D"="Gray","L"="darkred","W"="darkgreen"))+
scale_x_date(date_breaks = "1 week", date_labels = "%Y-%m-%d") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
guides(color = guide_legend(ncol = 5))+
theme(legend.position = "top")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
dfPartidos_CT_mean %>% ggplot(aes(y=CortisolZ,x=Fecha))+
geom_point(aes(color=Resultado),alpha=0.5,size=2)+
geom_smooth(se = FALSE)+
scale_color_manual(values=c("D"="Gray","L"="darkred","W"="darkgreen"))+
scale_x_date(date_breaks = "1 week", date_labels = "%Y-%m-%d") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
guides(color = guide_legend(ncol = 5))+
theme(legend.position = "top")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
dfPartidos_CT_mean %>% ggplot(aes(y=Testosterona,x=Fecha))+
geom_point(aes(color=Resultado),alpha=0.5,size=2)+
geom_smooth(se = FALSE)+
scale_color_manual(values=c("D"="Gray","L"="darkred","W"="darkgreen"))+
scale_x_date(date_breaks = "1 week", date_labels = "%Y-%m-%d") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
guides(color = guide_legend(ncol = 5))+
theme(legend.position = "top")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
dfPartidos_CT_mean %>% ggplot(aes(y=TestosteronaZ,x=Fecha))+
geom_point(aes(color=Resultado),alpha=0.5,size=2)+
geom_smooth(se = FALSE)+
scale_color_manual(values=c("D"="Gray","L"="darkred","W"="darkgreen"))+
scale_x_date(date_breaks = "1 week", date_labels = "%Y-%m-%d") +
theme_minimal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
guides(color = guide_legend(ncol = 5))+
theme(legend.position = "top")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
dfPartidos_CT_mean %>% openxlsx::write.xlsx("partidos_CT.xlsx")