---
title: "Example Chapter Becker Aloe Cheung"
author: "Becker Aloe Cheung"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Example Chapter Becker Aloe Cheung}
  \usepackage[utf8]{inputenc}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---
  
```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

We load the necessary packages as well as the data set for the example. Because for this example we are using only complete data we remove the the two studies with `NA` (i.e., Study 6 and Study 17).

```{r readindata, message = FALSE}
library(mars)

becker09 <- na.omit(becker09) # ommiting studies with NA
```

Next, we create a list, which will be imputed in our other functions.
```{r convert_data}
becker09_list <- df_to_corr(becker09, 
                            variables = c('Cognitive_Performance',
                                          'Somatic_Performance',
                                          'Selfconfidence_Performance', 
                                          'Somatic_Cognitive',
                                          'Selfconfidence_Cognitive',
                                          'Selfconfidence_Somatic'),
                            ID = 'ID')
```

## Calculate OS
There are currently three options for the variance-covariance matrix of the correlation matrix (i.e, simple, average, and weighted) for this example we selected the weighted option. 
```{r os}
#olkin_siotani(becker09_list, becker09$N, type = 'simple')
#olkin_siotani(becker09_list, becker09$N, type = 'average')
olkin_siotani(becker09_list, becker09$N, type = 'weighted')
```

# Master Prep Data Function
The function below creates and organize elements that are then fitted internally into the `metafor` package. Below we fitted fixed and random-effects models and extracted some more detail information from objects that are not directly output by the functions.
First, we see results under fixed-effect models.
```{r prep_data, eval = FALSE}
mars(data = becker09, studyID = 'ID',
     effectID = 'numID', sample_size = 'N',
     effectsize_type = 'cor',
     estimation_method = 'FE',
     varcov_type = 'weighted',
     variable_names = c('Cognitive_Performance', 'Somatic_Performance',
                        'Selfconfidence_Performance',
                        'Somatic_Cognitive',
                        'Selfconfidence_Cognitive',
                        'Selfconfidence_Somatic')) |>
  summary()
```

Now we fit a random-effects model and extract some objects from this output.
```{r random-effect}
model_out_random <- mars(data = becker09, studyID = 'ID',
     effectID = 'numID', sample_size = 'N',
     effectsize_type = 'cor',
     varcov_type = 'weighted',
     variable_names = c('Cognitive_Performance', 'Somatic_Performance',
                        'Selfconfidence_Performance',
                        'Somatic_Cognitive',
                        'Selfconfidence_Cognitive',
                        'Selfconfidence_Somatic'))
summary(model_out_random)
```


# Fit path model
Now, we are ready to input the average correlation matrix and its variance covariance matrix and our own function to appropriate estimate SE via the multivariate delta method. 
```{r model_path}
model <- "## Regression paths
Performance ~ Cognitive + Somatic + Selfconfidence
Selfconfidence ~ Cognitive + Somatic
"


mars(data = becker09, studyID = 'ID',
     effectID = 'numID', sample_size = 'N',
     effectsize_type = 'cor',
     varcov_type = 'weighted',
     variable_names = c('Cognitive_Performance', 'Somatic_Performance',
                        'Selfconfidence_Performance',
                        'Somatic_Cognitive',
                        'Selfconfidence_Cognitive',
                        'Selfconfidence_Somatic')) |>
  path_model(model = model) |>
  summary()
```

# Team 
We now subset the data to obtain results only for the studies that reported on Team sports. 
```{r team}
becker09_T <- subset(becker09, becker09$Team == "T")

```


```{r prep_data2}
mars(data = becker09_T, studyID = 'ID',
     effectID = 'numID', sample_size = 'N',
     effectsize_type = 'cor',
     varcov_type = 'weighted',
     variable_names = c('Cognitive_Performance', 'Somatic_Performance',
                        'Selfconfidence_Performance',
                        'Somatic_Cognitive',
                        'Selfconfidence_Cognitive',
                        'Selfconfidence_Somatic')) |>
  summary()
# random_model2 <- fit_model(data = input_metafor2, effect_size = 'yi', 
#                            var_cor = 'V', moderators = ~ -1 + factor(outcome), 
#                            random_params = ~ factor(outcome) | factor(study))

```

# Fit path model
```{r model_path2}
model <- "## Regression paths
Performance ~ Cognitive + Somatic + Selfconfidence
Selfconfidence ~ Cognitive + Somatic
"


mars(data = becker09_T, studyID = 'ID',
     effectID = 'numID', sample_size = 'N',
     effectsize_type = 'cor',
     varcov_type = 'weighted',
     variable_names = c('Cognitive_Performance', 'Somatic_Performance',
                        'Selfconfidence_Performance',
                        'Somatic_Cognitive',
                        'Selfconfidence_Cognitive',
                        'Selfconfidence_Somatic')) |>
  path_model(model = model) |>
  summary()
```

# Individual
Similarly, we now subset the data to obtain results only for the studies that reported on Individual sports. 
```{r Indiv}
becker09_I <- subset(becker09, becker09$Team == "I")

```

```{r prep_data3}
mars(data = becker09_I, studyID = 'ID',
     effectID = 'numID', sample_size = 'N',
     effectsize_type = 'cor',
     varcov_type = 'weighted',
     variable_names = c('Cognitive_Performance', 'Somatic_Performance',
                        'Selfconfidence_Performance',
                        'Somatic_Cognitive',
                        'Selfconfidence_Cognitive',
                        'Selfconfidence_Somatic')) |>
  summary()

# random_model3 <- fit_model(data = input_metafor3, effect_size = 'yi', 
#                            var_cor = 'V', moderators = ~ -1 + factor(outcome), 
#                            random_params = ~ factor(outcome) | factor(study))

```

# Fit path model
```{r model_path3}
model <- "## Regression paths
Performance ~ Cognitive + Somatic + Selfconfidence
Selfconfidence ~ Cognitive + Somatic
"


mars(data = becker09_I, studyID = 'ID',
     effectID = 'numID', sample_size = 'N',
     effectsize_type = 'cor',
     varcov_type = 'weighted',
     variable_names = c('Cognitive_Performance', 'Somatic_Performance',
                        'Selfconfidence_Performance',
                        'Somatic_Cognitive',
                        'Selfconfidence_Cognitive',
                        'Selfconfidence_Somatic')) |>
  path_model(model = model) |>
  summary()
```

# Partial correlation matrix
Here we compute the synthetic partial correlation from the average correlation matrix.
```{r pipe, eval = FALSE}
# partial corr matrix
part_cor <- round(corpcor::vec2sm(model_out_random$beta_r),3)
diag(part_cor) <- 1
part_cor

# var-cov matrix of partials
Psy <- model_out_random$varcov_beta
round(var_path(model_out_random$beta_r, Psy, type = 'pcor'), 4)
```

# Partial from uni approach
Here we work with partial correlation for each study and then synthesize that information. 
```{r partial, eval = FALSE}
#---------------------------------------------------------------------
# Create a data set with 8 complete studies
#---------------------------------------------------------------------
R <-  becker09_list
R$"6" <- NULL
R$"17" <- NULL
n <- becker09$N[c(-3, -5)]
#------------------------------------------------------------------
# first replace NA by zeros
RR <- R                           # redifine list

PR <- lapply(RR, cor2pcor)
pr <- unlist(lapply(PR, '[[', 4))
var_pr <- (1-pr^2)^2 / (n - 3 -1)

rma.uni(pr, var_pr)
```
