# Course: EDF 6403 - Quantitative Foundations of Educational Research
# Stats Analysis 6: correlations
# Author: Nguyet Hoang | hoangt@ufl.edu

# Set up R
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.0     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(conflicted)
conflict_prefer("filter", "dplyr")
## [conflicted] Will prefer dplyr::filter over any other package.
library(psych)
library(scales)
conflicts_prefer(psych::alpha)
## [conflicted] Will prefer psych::alpha over any other package.
library(haven)

# Read data
data <- read_sav("SA6_correlations.sav")
view(data)

# Compute the correlation matrix
results <- corr.test(data, use = "pairwise", adjust = "none")
r_matrix <- results$r
lowerMat(r_matrix, digits = 2)
##                   Essnt Bhvrs Prgrs Exsnt Prnnl Rcnst
## Essentialism       1.00                              
## Behaviorism       -0.31  1.00                        
## Progressivism      0.07  0.12  1.00                  
## Exisentialism      0.00  0.56 -0.06  1.00            
## Perennialism       0.03 -0.18  0.10 -0.62  1.00      
## Reconstructionism  0.18  0.11 -0.11 -0.06 -0.04  1.00
# Compute the p-value matrix
p_matrix <- results$p
lowerMat(p_matrix, digits = 3)
##                   Essnt Bhvrs Prgrs Exsnt Prnnl Rcnst
## Essentialism      0.000                              
## Behaviorism       0.177 0.000                        
## Progressivism     0.767 0.613 0.000                  
## Exisentialism     0.988 0.011 0.814 0.000            
## Perennialism      0.909 0.435 0.676 0.004 0.000      
## Reconstructionism 0.444 0.648 0.653 0.802 0.855 0.000
# Find pairs of variables that have significant correlations
p_list <- as.data.frame(as.table(p_matrix))
colnames(p_list) <- c("Variable_A", "Variable_B", "p_value")
p_list <- p_list %>% filter(Variable_A != Variable_B) %>% 
  distinct(p_value, .keep_all = TRUE) %>%
  rowwise() %>%
  mutate(r_value = r_matrix[Variable_A, Variable_B],
    effect_size = number(r_value^2, accuracy = 0.0001)) %>%
  ungroup()

sig_05  <- p_list %>% filter(p_value < 0.05 & p_value >= 0.01)
sig_05
## # A tibble: 1 × 5
##   Variable_A    Variable_B  p_value r_value effect_size
##   <fct>         <fct>         <dbl>   <dbl> <chr>      
## 1 Exisentialism Behaviorism  0.0110   0.556 0.3086
sig_01  <- p_list %>% filter(p_value < 0.01 & p_value >= 0.001)
sig_01
## # A tibble: 1 × 5
##   Variable_A   Variable_B    p_value r_value effect_size
##   <fct>        <fct>           <dbl>   <dbl> <chr>      
## 1 Perennialism Exisentialism 0.00383  -0.616 0.3794
sig_001 <- p_list %>% filter(p_value < 0.001)
sig_001
## # A tibble: 0 × 5
## # ℹ 5 variables: Variable_A <fct>, Variable_B <fct>, p_value <dbl>,
## #   r_value <dbl>, effect_size <chr>