---
title: "Correlation Matrix using rmcorr_mat"
author: "Jonathan Bakdash and Laura Marusich"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
bibliography: references.bib 
vignette: >
  %\VignetteIndexEntry{Correlation Matrix using rmcorr_mat}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, tidy = FALSE)
options(width = 80)
library(knitr)
library(rmarkdown)
library(rmcorr) 
library(corrplot) 

```
#### Running Examples Requires corrplot [@corrplot2021]
```{r, eval = FALSE}
#Install corrplot
 install.packages("corrplot")
 require(corrplot)
```

## Plotting a Correlation Matrix 
The output from *rmcorr_mat* can be used be used to plot a correlation matrix.
```{r}
dist_rmc_mat <- rmcorr_mat(participant = Subject, 
                           variables = c("Blindwalk Away",
                                         "Blindwalk Toward",
                                         "Triangulated BW",
                                         "Verbal",
                                         "Visual matching"),
                           dataset = twedt_dist_measures,
                           CI.level = 0.95)

corrplot(dist_rmc_mat$matrix)
```

## Plotting Multiple Models
The output can also be used to plot multiple models side-by-side.
```{r}
#Number of models being plotted
n.models <- length(dist_rmc_mat$models)

#Change graphing parameters to plot side-by-side
#with narrower margins
par(mfrow = c(3,4), 
    mar = c(2.75, 2.4, 2.4, 1.4))

for (i in 1:n.models) {
    plot(dist_rmc_mat$models[[i]])
    }

#Reset graphing parameters
#dev.off()
```

## Adjusting for Multiple Comparisons
The third component of the output from *rmcorr_mat()* contains a summary of results. Using the summary component, we demonstrate adjusting for multiple comparisons using two methods: the Bonferroni correction and the False Discovery Rate (FDR). <br>
<br>
This example also compares the unadjusted *p*-values to both adjustment methods. Because most of the unadjusted *p*-values are quite small, many of the adjusted *p*-values tend to be similar to the unadjusted ones and the two adjustment methods also tend to produce similar *p*-values.   


```{r}
#Third component: Summary
dist_rmc_mat$summary

#p-values only
dist_rmc_mat$summary$p.vals

#Vector of original, unadjusted p-values for all 10 comparisons
p.vals <- dist_rmc_mat$summary$p.vals

p.vals.bonferroni <- p.adjust(p.vals, 
                              method = "bonferroni",
                              n = length(p.vals))

p.vals.fdr <- p.adjust(p.vals, 
                       method = "fdr",
                       n = length(p.vals))

#All p-values together
all.pvals <- cbind(p.vals, p.vals.bonferroni, p.vals.fdr)
colnames(all.pvals) <- c("Unadjusted", "Bonferroni", "fdr")
round(all.pvals, digits = 5)
```