#### ON TIME-USE AND WELL-BEING: THE ROLE OF PERSONAL AND PROJECT CHARACTERISTICS ###
    #SPRING 2021

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

library(car)
library(psych)
library(tidyverse)
library(QuantPsyc)
library(readr)
library(dplyr)
library(rockchalk)
library(corrplot)
library(ggplot2)
library(GGally)
library(Hmisc)
library(PerformanceAnalytics)
library(jmv)
library(stargazer)
library(multilevel)
library(bda)
library(mediation)
library(lavaan)
library(knitr)
library(MBESS)
library(semPlot)

 #read data in
pp <- read.csv("personal.projects.cleaned.v2.csv")

#descriptives
descriptives(pp, vars = vars(as.5f, as.6f, bpns, autonomy, competence, relatedness, passion, 
                             vlq, zest, engagement), missing = TRUE)
## 
##  DESCRIPTIVES
## 
##  Descriptives                                                                                                                               
##  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
##               as.5f       as.6f       bpns        autonomy    competence    relatedness    passion     vlq         zest        engagement   
##  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
##    N               191         191         191         191           191            191         191         190         191           191   
##    Missing           0           0           0           0             0              0           0           1           0             0   
##    Mean       4.755516    4.405448    5.042757    5.450262      5.078534       4.599476    4.193063    52.68466    4.387625      3.195201   
##    Median     4.777778    4.375000    5.166667    5.500000      5.500000       5.000000    4.500000    51.13889    4.416667      3.166667   
##    Minimum    3.111111    2.625000    1.500000    2.000000      1.000000       1.000000    1.375000    12.90000    1.916667      1.000000   
##    Maximum    6.111111    5.875000    7.000000    7.000000      7.000000       7.000000    5.000000    98.00000    6.416667      5.000000   
##  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
descriptives(pp, vars = vars(as.5f, as.6f, pp1.14, pp.2.14, pp.3.14, pp.competence, aps.standards), missing = TRUE)
## 
##  DESCRIPTIVES
## 
##  Descriptives                                                                                              
##  ───────────────────────────────────────────────────────────────────────────────────────────────────────── 
##               as.5f       as.6f       pp1.14      pp.2.14     pp.3.14     pp.competence    aps.standards   
##  ───────────────────────────────────────────────────────────────────────────────────────────────────────── 
##    N               191         191         190         184         184              191              191   
##    Missing           0           0           1           7           7                0                0   
##    Mean       4.755516    4.405448    8.505263    8.592391    3.826087         20.42408         41.71204   
##    Median     4.777778    4.375000    9.000000    9.000000    4.000000               21               42   
##    Minimum    3.111111    2.625000           1           1           1                2               23   
##    Maximum    6.111111    5.875000          10          10           7               27               49   
##  ─────────────────────────────────────────────────────────────────────────────────────────────────────────
# well-being variables for correlations
pp.wb<- pp[,c("as.5f", "as.6f", "autonomy", "aps.standards", "competence", "pp.competence", "relatedness", "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 4.140706e-02 2.590771e-04
##  [2,] 5.658583e-01 0.000000000 9.889010e-01 9.164794e-01 5.172522e-01
##  [3,] 1.343457e-05 0.988901048 0.000000e+00 5.165033e-03 1.018359e-22
##  [4,] 4.140706e-02 0.916479428 5.165033e-03 0.000000e+00 2.632195e-07
##  [5,] 2.590771e-04 0.517252194 1.018359e-22 2.632195e-07 0.000000e+00
##  [6,] 6.171758e-01 0.272885439 1.990726e-04 2.368762e-05 2.552889e-03
##  [7,] 9.653685e-02 0.474134233 4.622112e-08 5.061281e-02 1.997229e-11
##  [8,] 1.040594e-03 0.909560239 1.640097e-08 5.155935e-06 5.205198e-08
##  [9,] 4.440177e-04 0.723248449 6.275581e-07 4.554646e-02 1.408462e-08
## [10,] 2.265509e-02 0.007866747 1.338143e-02 1.165403e-01 1.244400e-02
## [11,] 8.949248e-01 0.006364637 9.118677e-02 1.470294e-01 5.760085e-02
##               [,6]         [,7]         [,8]         [,9]        [,10]
##  [1,] 6.171758e-01 9.653685e-02 1.040594e-03 4.440177e-04 2.265509e-02
##  [2,] 2.728854e-01 4.741342e-01 9.095602e-01 7.232484e-01 7.866747e-03
##  [3,] 1.990726e-04 4.622112e-08 1.640097e-08 6.275581e-07 1.338143e-02
##  [4,] 2.368762e-05 5.061281e-02 5.155935e-06 4.554646e-02 1.165403e-01
##  [5,] 2.552889e-03 1.997229e-11 5.205198e-08 1.408462e-08 1.244400e-02
##  [6,] 0.000000e+00 1.521318e-04 5.894576e-06 6.083929e-05 1.631258e-04
##  [7,] 1.521318e-04 0.000000e+00 2.995544e-07 8.760185e-12 2.629393e-02
##  [8,] 5.894576e-06 2.995544e-07 0.000000e+00 4.479783e-06 5.635796e-05
##  [9,] 6.083929e-05 8.760185e-12 4.479783e-06 0.000000e+00 4.926631e-03
## [10,] 1.631258e-04 2.629393e-02 5.635796e-05 4.926631e-03 0.000000e+00
## [11,] 5.202758e-01 7.285191e-02 1.135053e-02 2.075489e-01 7.246950e-03
##             [,11]
##  [1,] 0.894924797
##  [2,] 0.006364637
##  [3,] 0.091186767
##  [4,] 0.147029425
##  [5,] 0.057600851
##  [6,] 0.520275762
##  [7,] 0.072851906
##  [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.0058709010  0.12405975
##  [2,] -0.100776324  1.00000000 -0.14098642 -0.1494553799 -0.09547725
##  [3,]  0.174941751 -0.14098642  1.00000000  0.0613705576  0.53867588
##  [4,]  0.005870901 -0.14945538  0.06137056  1.0000000000  0.23210928
##  [5,]  0.124059751 -0.09547725  0.53867588  0.2321092829  1.00000000
##  [6,] -0.106132467 -0.06295931  0.12895375  0.1658164545  0.07752818
##  [7,] -0.021750005 -0.09055114  0.25466038 -0.0003295592  0.34103208
##  [8,]  0.096737873 -0.15007654  0.26744748  0.1897889032  0.25316476
##  [9,]  0.114092815 -0.16759339  0.22086754  0.0029603710  0.26999936
## [10,]  0.023438590  0.05119212  0.03767948 -0.0285003001  0.03957441
## [11,] -0.132540814  0.05637193 -0.26002182 -0.2436608232 -0.27425571
##              [,6]          [,7]        [,8]         [,9]       [,10]
##  [1,] -0.10613247 -0.0217500047  0.09673787  0.114092815  0.02343859
##  [2,] -0.06295931 -0.0905511361 -0.15007654 -0.167593391  0.05119212
##  [3,]  0.12895375  0.2546603820  0.26744748  0.220867536  0.03767948
##  [4,]  0.16581645 -0.0003295592  0.18978890  0.002960371 -0.02850030
##  [5,]  0.07752818  0.3410320836  0.25316476  0.269999360  0.03957441
##  [6,]  1.00000000  0.1338664381  0.18775337  0.150430857  0.13259963
##  [7,]  0.13386644  1.0000000000  0.23037411  0.350054789  0.01923470
##  [8,]  0.18775337  0.2303741052  1.00000000  0.192426183  0.15133855
##  [9,]  0.15043086  0.3500547892  0.19242618  1.000000000  0.06265629
## [10,]  0.13259963  0.0192347005  0.15133855  0.062656291  1.00000000
## [11,] -0.18753500 -0.2671386957 -0.31660237 -0.231177680  0.05321163
##             [,11]
##  [1,] -0.13254081
##  [2,]  0.05637193
##  [3,] -0.26002182
##  [4,] -0.24366082
##  [5,] -0.27425571
##  [6,] -0.18753500
##  [7,] -0.26713870
##  [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.28375492 0.388975466 0.17745766
##  [2,] 0.1826962 1.0000000 0.14297199 0.13448708 0.187863093 0.21923033
##  [3,] 0.4322416 0.1429720 1.00000000 0.33401256 0.710492864 0.39318838
##  [4,] 0.2837549 0.1344871 0.33401256 1.00000000 0.479479243 0.42456838
##  [5,] 0.3889755 0.1878631 0.71049286 0.47947924 1.000000000 0.34835840
##  [6,] 0.1774577 0.2192303 0.39318838 0.42456838 0.348358396 1.00000000
##  [7,] 0.2581606 0.1926523 0.49772714 0.27804366 0.565682141 0.39740624
##  [8,] 0.3652506 0.1338631 0.50797948 0.44464711 0.496523543 0.44295210
##  [9,] 0.3810140 0.1169268 0.47089637 0.28177941 0.510581983 0.41218017
## [10,] 0.2998302 0.3249101 0.31274711 0.25184531 0.314458233 0.39631966
## [11,] 0.1513921 0.3295487 0.01975575 0.03721061 0.004430199 0.09581421
##             [,7]        [,8]      [,9]     [,10]        [,11]
##  [1,] 0.25816063  0.36525057 0.3810140 0.2998302  0.151392129
##  [2,] 0.19265233  0.13386310 0.1169268 0.3249101  0.329548703
##  [3,] 0.49772714  0.50797948 0.4708964 0.3127471  0.019755746
##  [4,] 0.27804366  0.44464711 0.2817794 0.2518453  0.037210609
##  [5,] 0.56568214  0.49652354 0.5105820 0.3144582  0.004430199
##  [6,] 0.39740624  0.44295210 0.4121802 0.3963197  0.095814214
##  [7,] 1.00000000  0.47806625 0.5731221 0.2959977  0.012109451
##  [8,] 0.47806625  1.00000000 0.4474513 0.4123175 -0.041951667
##  [9,] 0.57312208  0.44745126 1.0000000 0.3358362  0.051175496
## [10,] 0.29599767  0.41231750 0.3358362 1.0000000  0.326720183
## [11,] 0.01210945 -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()

chart.Correlation(pp.wb, histogram=TRUE)

#multiple mediators
# effect of standards on well-being is mediated by satisfying psych need for competence and personal project dimension of competence

pp$X <- pp$aps.standards
pp$M1 <- pp$competence
pp$M2 <- pp$pp.competence
pp$Y <- pp$mhc


mmed <- '
Y ~ b1 * M1 + b2 * M2 + c * X
M1 ~ a1 * X
M2 ~ a2 * X
indirect1 := a1 * b1
indirect2 := a2 * b2
total := c + (a1 * b1) + (a2 * b2)
M1 ~~ M2
'
fit <- sem(model = mmed, data = pp)
summary(fit)
## lavaan 0.6-8 ended normally after 28 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
##                                                       
##   Number of observations                           191
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   Y ~                                                 
##     M1        (b1)    0.399    0.048    8.318    0.000
##     M2        (b2)    0.051    0.014    3.642    0.000
##     X          (c)   -0.007    0.011   -0.664    0.507
##   M1 ~                                                
##     X         (a1)    0.079    0.015    5.370    0.000
##   M2 ~                                                
##     X         (a2)    0.220    0.050    4.358    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .M1 ~~                                               
##    .M2                0.599    0.359    1.670    0.095
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .Y                 0.619    0.063    9.772    0.000
##    .M1                1.428    0.146    9.772    0.000
##    .M2               16.975    1.737    9.772    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1         0.031    0.007    4.511    0.000
##     indirect2         0.011    0.004    2.795    0.005
##     total             0.035    0.012    3.026    0.002
fit <- sem(
    model = mmed,
    data  = pp,
    se = "bootstrap",
    bootstrap = 5000)

summary(fit, fit.measures=TRUE, standardize=TRUE, rsquare=TRUE,
estimates = TRUE, ci = TRUE)
## lavaan 0.6-8 ended normally after 28 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
##                                                       
##   Number of observations                           191
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               131.298
##   Degrees of freedom                                 6
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1070.307
##   Loglikelihood unrestricted model (H1)      -1070.307
##                                                       
##   Akaike (AIC)                                2158.613
##   Bayesian (BIC)                              2187.884
##   Sample-size adjusted Bayesian (BIC)         2159.375
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value RMSEA <= 0.05                             NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                            Bootstrap
##   Number of requested bootstrap draws             5000
##   Number of successful bootstrap draws            5000
## 
## Regressions:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##   Y ~                                                                   
##     M1        (b1)    0.399    0.045    8.954    0.000    0.313    0.486
##     M2        (b2)    0.051    0.012    4.206    0.000    0.028    0.075
##     X          (c)   -0.007    0.011   -0.652    0.514   -0.028    0.014
##   M1 ~                                                                  
##     X         (a1)    0.079    0.013    5.855    0.000    0.053    0.105
##   M2 ~                                                                  
##     X         (a2)    0.220    0.056    3.925    0.000    0.113    0.332
##    Std.lv  Std.all
##                   
##     0.399    0.523
##     0.051    0.224
##    -0.007   -0.043
##                   
##     0.079    0.362
##                   
##     0.220    0.301
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##  .M1 ~~                                                                 
##    .M2                0.599    0.471    1.272    0.203   -0.257    1.594
##    Std.lv  Std.all
##                   
##     0.599    0.122
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .Y                 0.619    0.066    9.382    0.000    0.489    0.744
##    .M1                1.428    0.163    8.735    0.000    1.108    1.752
##    .M2               16.975    2.618    6.483    0.000   11.933   22.153
##    Std.lv  Std.all
##     0.619    0.646
##     1.428    0.869
##    16.975    0.910
## 
## R-Square:
##                    Estimate
##     Y                 0.354
##     M1                0.131
##     M2                0.090
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     indirect1         0.031    0.007    4.794    0.000    0.020    0.045
##     indirect2         0.011    0.004    2.710    0.007    0.004    0.021
##     total             0.035    0.011    3.194    0.001    0.014    0.057
##    Std.lv  Std.all
##     0.031    0.189
##     0.011    0.067
##     0.035    0.214
parameterEstimates(fit, boot.ci.type="bca.simple")
##          lhs op               rhs     label    est    se      z pvalue ci.lower
## 1          Y  ~                M1        b1  0.399 0.045  8.954  0.000    0.311
## 2          Y  ~                M2        b2  0.051 0.012  4.206  0.000    0.027
## 3          Y  ~                 X         c -0.007 0.011 -0.652  0.514   -0.027
## 4         M1  ~                 X        a1  0.079 0.013  5.855  0.000    0.053
## 5         M2  ~                 X        a2  0.220 0.056  3.925  0.000    0.117
## 6         M1 ~~                M2            0.599 0.471  1.272  0.203   -0.182
## 7          Y ~~                 Y            0.619 0.066  9.382  0.000    0.508
## 8         M1 ~~                M1            1.428 0.163  8.735  0.000    1.138
## 9         M2 ~~                M2           16.975 2.618  6.483  0.000   12.576
## 10         X ~~                 X           34.969 0.000     NA     NA   34.969
## 11 indirect1 :=             a1*b1 indirect1  0.031 0.007  4.794  0.000    0.020
## 12 indirect2 :=             a2*b2 indirect2  0.011 0.004  2.710  0.007    0.005
## 13     total := c+(a1*b1)+(a2*b2)     total  0.035 0.011  3.194  0.001    0.015
##    ci.upper
## 1     0.485
## 2     0.075
## 3     0.015
## 4     0.105
## 5     0.336
## 6     1.706
## 7     0.772
## 8     1.796
## 9    23.158
## 10   34.969
## 11    0.046
## 12    0.021
## 13    0.058
with(pp, mediation.effect.plot(x = aps.standards, mediator = competence, dv = mhc, ylab = "Well-Being", xlab = "Perfectionistic standards"))

semPaths(fit, "model", "est", layout ="tree2", node.label.cex=4, title = TRUE, edge.label.cex=.7, curve=TRUE)