setwd("~/Google Drive/PhD/Projects/Personal projects and well-being/data")

library(car)
library(psych)
library(tidyverse)
library(readr)
library(dplyr)
library(corrplot)
library(ggplot2)
library(GGally)
library(ggpubr)
library(Hmisc)
library(PerformanceAnalytics)
library(jmv)
library(stargazer)
library(plyr)

 #read data in
pp <- read.csv("personal.projects.cleaned.v3.csv")
#descriptives
descriptives(pp, vars = vars(bpns, autonomy, competence, relatedness), missing = TRUE)
## 
##  DESCRIPTIVES
## 
##  Descriptives                                                     
##  ──────────────────────────────────────────────────────────────── 
##               bpns        autonomy    competence    relatedness   
##  ──────────────────────────────────────────────────────────────── 
##    N               191         191           191            191   
##    Missing           0           0             0              0   
##    Mean       5.042757    5.450262      5.078534       4.599476   
##    Median     5.166667    5.500000      5.500000       5.000000   
##    Minimum    1.500000    2.000000      1.000000       1.000000   
##    Maximum    7.000000    7.000000      7.000000       7.000000   
##  ────────────────────────────────────────────────────────────────
descriptives(pp, vars = vars(passion, vlq, zest, engagement, mhc), missing = TRUE)
## 
##  DESCRIPTIVES
## 
##  Descriptives                                                              
##  ───────────────────────────────────────────────────────────────────────── 
##               passion     vlq         zest        engagement    mhc        
##  ───────────────────────────────────────────────────────────────────────── 
##    N               191         190         191           191         191   
##    Missing           0           1           0             0           0   
##    Mean       4.193063    52.68466    4.387625      3.195201    3.888859   
##    Median     4.500000    51.13889    4.416667      3.166667    4.071429   
##    Minimum    1.375000    12.90000    1.916667      1.000000    1.384615   
##    Maximum    5.000000    98.00000    6.416667      5.000000    6.000000   
##  ─────────────────────────────────────────────────────────────────────────
descriptives(pp, vars = vars(as.5f, as.6f, aps.standards), missing = TRUE)
## 
##  DESCRIPTIVES
## 
##  Descriptives                                         
##  ──────────────────────────────────────────────────── 
##               as.5f       as.6f       aps.standards   
##  ──────────────────────────────────────────────────── 
##    N               191         191              191   
##    Missing           0           0                0   
##    Mean       4.755516    4.405448         41.71204   
##    Median     4.777778    4.375000               42   
##    Minimum    3.111111    2.625000               23   
##    Maximum    6.111111    5.875000               49   
##  ────────────────────────────────────────────────────
#tabulated counts of mental health category and perfectionism style
xtabs(~ mh+perf, data=pp, na.action=na.exclude)
##              perf
## mh            adaptive maladaptive non-perfectionist
##   flourishing        2          19                18
##   languishing        0           2                 0
##   moderate          24          63                63
# well-being variables for correlations
pp.wb<- pp[,c("as.5f", "as.6f", "autonomy", "competence", "relatedness", "mhc", "aps.standards", "passion", "vlq", "zest", "engagement")]

#calculating correlations and CIs
cor1 <- cor.mtest(pp.wb, use="pairwise.complete.obs", conf.level = 0.95)
cor1
## $p
##               [,1]        [,2]         [,3]         [,4]         [,5]
##  [1,] 0.000000e+00 0.565858341 1.343457e-05 2.590771e-04 9.653685e-02
##  [2,] 5.658583e-01 0.000000000 9.889010e-01 5.172522e-01 4.741342e-01
##  [3,] 1.343457e-05 0.988901048 0.000000e+00 1.018359e-22 4.622112e-08
##  [4,] 2.590771e-04 0.517252194 1.018359e-22 0.000000e+00 1.997229e-11
##  [5,] 9.653685e-02 0.474134233 4.622112e-08 1.997229e-11 0.000000e+00
##  [6,] 2.687097e-04 0.732980567 4.494337e-16 6.915654e-17 2.485119e-16
##  [7,] 4.140706e-02 0.916479428 5.165033e-03 2.632195e-07 5.061281e-02
##  [8,] 1.040594e-03 0.909560239 1.640097e-08 5.205198e-08 2.995544e-07
##  [9,] 4.440177e-04 0.723248449 6.275581e-07 1.408462e-08 8.760185e-12
## [10,] 2.265509e-02 0.007866747 1.338143e-02 1.244400e-02 2.629393e-02
## [11,] 8.949248e-01 0.006364637 9.118677e-02 5.760085e-02 7.285191e-02
##               [,6]         [,7]         [,8]         [,9]        [,10]
##  [1,] 2.687097e-04 4.140706e-02 1.040594e-03 4.440177e-04 2.265509e-02
##  [2,] 7.329806e-01 9.164794e-01 9.095602e-01 7.232484e-01 7.866747e-03
##  [3,] 4.494337e-16 5.165033e-03 1.640097e-08 6.275581e-07 1.338143e-02
##  [4,] 6.915654e-17 2.632195e-07 5.205198e-08 1.408462e-08 1.244400e-02
##  [5,] 2.485119e-16 5.061281e-02 2.995544e-07 8.760185e-12 2.629393e-02
##  [6,] 0.000000e+00 2.964637e-03 4.334679e-11 1.240068e-15 7.655631e-03
##  [7,] 2.964637e-03 0.000000e+00 5.155935e-06 4.554646e-02 1.165403e-01
##  [8,] 4.334679e-11 5.155935e-06 0.000000e+00 4.479783e-06 5.635796e-05
##  [9,] 1.240068e-15 4.554646e-02 4.479783e-06 0.000000e+00 4.926631e-03
## [10,] 7.655631e-03 1.165403e-01 5.635796e-05 4.926631e-03 0.000000e+00
## [11,] 6.372302e-01 1.470294e-01 1.135053e-02 2.075489e-01 7.246950e-03
##             [,11]
##  [1,] 0.894924797
##  [2,] 0.006364637
##  [3,] 0.091186767
##  [4,] 0.057600851
##  [5,] 0.072851906
##  [6,] 0.637230172
##  [7,] 0.147029425
##  [8,] 0.011350534
##  [9,] 0.207548917
## [10,] 0.007246950
## [11,] 0.000000000
## 
## $lowCI
##               [,1]        [,2]        [,3]        [,4]          [,5]
##  [1,]  1.000000000 -0.10077632  0.17494175  0.12405975 -0.0217500047
##  [2,] -0.100776324  1.00000000 -0.14098642 -0.09547725 -0.0905511361
##  [3,]  0.174941751 -0.14098642  1.00000000  0.53867588  0.2546603820
##  [4,]  0.124059751 -0.09547725  0.53867588  1.00000000  0.3410320836
##  [5,] -0.021750005 -0.09055114  0.25466038  0.34103208  1.0000000000
##  [6,]  0.123375066 -0.16623794  0.43511368  0.44943359  0.4397054355
##  [7,]  0.005870901 -0.14945538  0.06137056  0.23210928 -0.0003295592
##  [8,]  0.096737873 -0.15007654  0.26744748  0.25316476  0.2303741052
##  [9,]  0.114092815 -0.16759339  0.22086754  0.26999936  0.3500547892
## [10,]  0.023438590  0.05119212  0.03767948  0.03957441  0.0192347005
## [11,] -0.132540814  0.05637193 -0.26002182 -0.27425571 -0.2671386957
##              [,6]          [,7]        [,8]         [,9]       [,10]
##  [1,]  0.12337507  0.0058709010  0.09673787  0.114092815  0.02343859
##  [2,] -0.16623794 -0.1494553799 -0.15007654 -0.167593391  0.05119212
##  [3,]  0.43511368  0.0613705576  0.26744748  0.220867536  0.03767948
##  [4,]  0.44943359  0.2321092829  0.25316476  0.269999360  0.03957441
##  [5,]  0.43970544 -0.0003295592  0.23037411  0.350054789  0.01923470
##  [6,]  1.00000000  0.0741852333  0.33323338  0.428182717  0.05186347
##  [7,]  0.07418523  1.0000000000  0.18978890  0.002960371 -0.02850030
##  [8,]  0.33323338  0.1897889032  1.00000000  0.192426183  0.15133855
##  [9,]  0.42818272  0.0029603710  0.19242618  1.000000000  0.06265629
## [10,]  0.05186347 -0.0285003001  0.15133855  0.062656291  1.00000000
## [11,] -0.17546124 -0.2436608232 -0.31660237 -0.231177680  0.05321163
##             [,11]
##  [1,] -0.13254081
##  [2,]  0.05637193
##  [3,] -0.26002182
##  [4,] -0.27425571
##  [5,] -0.26713870
##  [6,] -0.17546124
##  [7,] -0.24366082
##  [8,] -0.31660237
##  [9,] -0.23117768
## [10,]  0.05321163
## [11,]  1.00000000
## 
## $uppCI
##            [,1]      [,2]       [,3]        [,4]       [,5]      [,6]
##  [1,] 1.0000000 0.1826962 0.43224162 0.388975466 0.25816063 0.3883852
##  [2,] 0.1826962 1.0000000 0.14297199 0.187863093 0.19265233 0.1175490
##  [3,] 0.4322416 0.1429720 1.00000000 0.710492864 0.49772714 0.6363868
##  [4,] 0.3889755 0.1878631 0.71049286 1.000000000 0.56568214 0.6468599
##  [5,] 0.2581606 0.1926523 0.49772714 0.565682141 1.00000000 0.6397531
##  [6,] 0.3883852 0.1175490 0.63638675 0.646859904 0.63975313 1.0000000
##  [7,] 0.2837549 0.1344871 0.33401256 0.479479243 0.27804366 0.3454007
##  [8,] 0.3652506 0.1338631 0.50797948 0.496523543 0.47806625 0.5596691
##  [9,] 0.3810140 0.1169268 0.47089637 0.510581983 0.57312208 0.6317498
## [10,] 0.2998302 0.3249101 0.31274711 0.314458233 0.29599767 0.3255121
## [11,] 0.1513921 0.3295487 0.01975575 0.004430199 0.01210945 0.1081694
##             [,7]        [,8]      [,9]     [,10]        [,11]
##  [1,] 0.28375492  0.36525057 0.3810140 0.2998302  0.151392129
##  [2,] 0.13448708  0.13386310 0.1169268 0.3249101  0.329548703
##  [3,] 0.33401256  0.50797948 0.4708964 0.3127471  0.019755746
##  [4,] 0.47947924  0.49652354 0.5105820 0.3144582  0.004430199
##  [5,] 0.27804366  0.47806625 0.5731221 0.2959977  0.012109451
##  [6,] 0.34540067  0.55966913 0.6317498 0.3255121  0.108169386
##  [7,] 1.00000000  0.44464711 0.2817794 0.2518453  0.037210609
##  [8,] 0.44464711  1.00000000 0.4474513 0.4123175 -0.041951667
##  [9,] 0.28177941  0.44745126 1.0000000 0.3358362  0.051175496
## [10,] 0.25184531  0.41231750 0.3358362 1.0000000  0.326720183
## [11,] 0.03721061 -0.04195167 0.0511755 0.3267202  1.000000000
#correlation Matrix
corrplot(cor(pp.wb, use="pairwise.complete.obs"), method="number", type="upper",
         addCoef.col = "orchid4", tl.col="hotpink3", tl.srt=45, p.mat = cor1$p, 
         sig.level = 0.05, insig = "blank", diag=FALSE)

ggpairs(pp.wb) + theme_classic()

#distributions of scale scores by level of mental health
boxplot(passion ~ mh, data = pp, xlab = "mental health", ylab = "passion", 
        plot = TRUE, outlier.tagging = TRUE)

boxplot(zest ~ mh, data = pp, xlab = "mental health", ylab = "zest", 
        plot = TRUE, outlier.tagging = TRUE)

boxplot(vlq ~ mh, data = pp, xlab = "mental health", ylab = "valued living", 
        plot = TRUE, outlier.tagging = TRUE)

boxplot(engagement ~ mh, data = pp, xlab = "mental health", ylab = "engagement", 
        plot = TRUE, outlier.tagging = TRUE)

boxplot(autonomy ~ mh, data = pp, xlab = "mental health", ylab = "autonomy satisfaction", 
        plot = TRUE, outlier.tagging = TRUE)

boxplot(competence ~ mh, data = pp, xlab = "mental health", ylab = "competence satisfaction", 
        plot = TRUE, outlier.tagging = TRUE)

boxplot(relatedness ~ mh, data = pp, xlab = "mental health", ylab = "relatedness satisfaction", 
        plot = TRUE, outlier.tagging = TRUE)

boxplot(bpns ~ mh, data = pp, xlab = "mental health", ylab = "overall psych need satisfaction", 
        plot = TRUE, outlier.tagging = TRUE)

#demographic descriptives
pp$gender <- as.factor(pp$gender)
pp$ethnicity <- as.factor(pp$ethnicity)
pp$student <- as.factor(pp$student)

pp$gender <- factor(pp$gender, levels = c(1,2,4),
labels = c("female", "male", "other identity"))

pp$ethnicity <- factor(pp$ethnicity, levels = c(1,2,3,4,5,6),
labels = c("white", "black", "asian", "indigenous", "latin", "other"))

pp$student <- factor(pp$student, levels = c(1,2),
labels = c("full time", "part time"))


descriptives(pp, age)
## 
##  DESCRIPTIVES
## 
##  Descriptives            
##  ─────────────────────── 
##               age        
##  ─────────────────────── 
##    N               177   
##    Missing          14   
##    Mean       20.71186   
##    Median           20   
##    Minimum          17   
##    Maximum          33   
##  ───────────────────────
eth = table(pp$ethnicity)
eth
## 
##      white      black      asian indigenous      latin      other 
##        119          6         46          0          0          0
student = table(pp$student)
student
## 
## full time part time 
##       186         5
gender = table(pp$gender)
gender
## 
##         female           male other identity 
##            168             22              1
#distributions of scale scores by type of perfectionism
boxplot(passion ~ perf, data = pp, xlab = "perfectionism", ylab = "passion", 
        plot = TRUE)

boxplot(zest ~ perf, data = pp, xlab = "perfectionism", ylab = "zest", 
        plot = TRUE)

boxplot(vlq ~ perf, data = pp, xlab = "perfectionism", ylab = "valued living", 
        plot = TRUE)

boxplot(engagement ~ perf, data = pp, xlab = "perfectionism", ylab = "engagement", 
        plot = TRUE)

boxplot(autonomy ~ perf, data = pp, xlab = "perfectionism", ylab = "autonomy satisfaction", 
        plot = TRUE)

boxplot(competence ~ perf, data = pp, xlab = "perfectionism", ylab = "competence satisfaction", 
        plot = TRUE)

boxplot(relatedness ~ perf, data = pp, xlab = "perfectionism", ylab = "relatedness satisfaction", 
        plot = TRUE)

boxplot(bpns ~ perf, data = pp, xlab = "perfectionism", ylab = "overall psych need satisfaction", 
        plot = TRUE)

boxplot(mhc ~ perf, data = pp, xlab = "perfectionism", ylab = "mental health level", 
        plot = TRUE)