#### 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               192         192         192         192           192            192         192         191         192           192   
##    Missing           0           0           0           0             0              0           0           1           0             0   
##    Mean       4.755053    4.403987    5.037326    5.450521      5.070312       4.591146    4.194010    52.51812    4.383712      3.195226   
##    Median     4.777778    4.375000    5.166667    5.500000      5.500000       5.000000    4.437500    50.77778    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               192         192         191         185         185              192              192   
##    Missing           0           0           1           7           7                0                0   
##    Mean       4.755053    4.403987    8.513089    8.600000    3.816216         20.43229         41.71354   
##    Median     4.777778    4.375000           9           9           4         21.00000         42.00000   
##    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.560470946 1.281221e-05 4.096158e-02 2.466828e-04
##  [2,] 5.604709e-01 0.000000000 9.901076e-01 9.149568e-01 4.905519e-01
##  [3,] 1.281221e-05 0.990107643 0.000000e+00 5.045867e-03 1.370982e-22
##  [4,] 4.096158e-02 0.914956807 5.045867e-03 0.000000e+00 2.819366e-07
##  [5,] 2.466828e-04 0.490551876 1.370982e-22 2.819366e-07 0.000000e+00
##  [6,] 6.200131e-01 0.277730629 1.915565e-04 2.255691e-05 2.896390e-03
##  [7,] 9.375999e-02 0.453127943 4.735015e-08 5.101829e-02 1.207790e-11
##  [8,] 1.023695e-03 0.903156510 1.503672e-08 4.863224e-06 6.217574e-08
##  [9,] 4.367471e-04 0.773031558 7.665335e-07 4.758358e-02 6.990444e-09
## [10,] 2.182575e-02 0.006966574 1.373290e-02 1.183619e-01 9.219113e-03
## [11,] 8.947897e-01 0.006267636 9.034165e-02 1.459756e-01 5.779363e-02
##               [,6]         [,7]         [,8]         [,9]        [,10]
##  [1,] 6.200131e-01 9.375999e-02 1.023695e-03 4.367471e-04 0.0218257471
##  [2,] 2.777306e-01 4.531279e-01 9.031565e-01 7.730316e-01 0.0069665745
##  [3,] 1.915565e-04 4.735015e-08 1.503672e-08 7.665335e-07 0.0137328964
##  [4,] 2.255691e-05 5.101829e-02 4.863224e-06 4.758358e-02 0.1183618563
##  [5,] 2.896390e-03 1.207790e-11 6.217574e-08 6.990444e-09 0.0092191129
##  [6,] 0.000000e+00 1.708009e-04 5.462004e-06 8.369439e-05 0.0001959114
##  [7,] 1.708009e-04 0.000000e+00 3.312111e-07 4.765741e-12 0.0210150006
##  [8,] 5.462004e-06 3.312111e-07 0.000000e+00 6.028332e-06 0.0000645129
##  [9,] 8.369439e-05 4.765741e-12 6.028332e-06 0.000000e+00 0.0030789718
## [10,] 1.959114e-04 2.101500e-02 6.451290e-05 3.078972e-03 0.0000000000
## [11,] 5.195168e-01 7.269995e-02 1.115231e-02 2.097341e-01 0.0073915546
##             [,11]
##  [1,] 0.894789738
##  [2,] 0.006267636
##  [3,] 0.090341655
##  [4,] 0.145975555
##  [5,] 0.057793631
##  [6,] 0.519516774
##  [7,] 0.072699953
##  [8,] 0.011152308
##  [9,] 0.209734072
## [10,] 0.007391555
## [11,] 0.000000000
## 
## $lowCI
##               [,1]        [,2]        [,3]          [,4]        [,5]
##  [1,]  1.000000000 -0.09993676  0.17522600  0.0061827559  0.12464266
##  [2,] -0.099936763  1.00000000 -0.14072557 -0.1492017287 -0.09221079
##  [3,]  0.175225996 -0.14072557  1.00000000  0.0617570115  0.53567574
##  [4,]  0.006182756 -0.14920173  0.06175701  1.0000000000  0.23057881
##  [5,]  0.124642658 -0.09221079  0.53567574  0.2305788095  1.00000000
##  [6,] -0.106142412 -0.06359052  0.12931547  0.1661702223  0.07450869
##  [7,] -0.020671410 -0.08782840  0.25368932 -0.0005787949  0.34512449
##  [8,]  0.096815953 -0.15025890  0.26779644  0.1901674130  0.25025657
##  [9,]  0.114106469 -0.16249906  0.21749929  0.0015973500  0.27764546
## [10,]  0.024412660  0.05403138  0.03690001 -0.0289909076  0.04711207
## [11,] -0.132181568  0.05659101 -0.25966554 -0.2433011042 -0.27346707
##              [,6]          [,7]        [,8]        [,9]       [,10]       [,11]
##  [1,] -0.10614241 -0.0206714103  0.09681595  0.11410647  0.02441266 -0.13218157
##  [2,] -0.06359052 -0.0878283993 -0.15025890 -0.16249906  0.05403138  0.05659101
##  [3,]  0.12931547  0.2536893189  0.26779644  0.21749929  0.03690001 -0.25966554
##  [4,]  0.16617022 -0.0005787949  0.19016741  0.00159735 -0.02899091 -0.24330110
##  [5,]  0.07450869  0.3451244899  0.25025657  0.27764546  0.04711207 -0.27346707
##  [6,]  1.00000000  0.1314099542  0.18841207  0.14450113  0.12890317 -0.18713225
##  [7,]  0.13140995  1.0000000000  0.22841581  0.35504671  0.02546011 -0.26652957
##  [8,]  0.18841207  0.2284158052  1.00000000  0.18741100  0.14862159 -0.31622640
##  [9,]  0.14450113  0.3550467120  0.18741100  1.00000000  0.07333224 -0.23016717
## [10,]  0.12890317  0.0254601118  0.14862159  0.07333224  1.00000000  0.05258569
## [11,] -0.18713225 -0.2665295661 -0.31622640 -0.23016717  0.05258569  1.00000000
## 
## $uppCI
##            [,1]      [,2]       [,3]       [,4]       [,5]      [,6]       [,7]
##  [1,] 1.0000000 0.1827839 0.43186416 0.28334528 0.38883520 0.1767143 0.25846092
##  [2,] 0.1827839 1.0000000 0.14249079 0.13399816 0.19031050 0.2179057 0.19456508
##  [3,] 0.4318642 0.1424908 1.00000000 0.33368432 0.70802087 0.3928591 0.49637527
##  [4,] 0.2833453 0.1339982 0.33368432 1.00000000 0.47764868 0.4242457 0.27711466
##  [5,] 0.3888352 0.1903105 0.70802087 0.47764868 1.00000000 0.3450201 0.56831547
##  [6,] 0.1767143 0.2179057 0.39285907 0.42424573 0.34502009 1.0000000 0.39465940
##  [7,] 0.2584609 0.1945651 0.49637527 0.27711466 0.56831547 0.3946594 1.00000000
##  [8,] 0.3646624 0.1329360 0.50769641 0.44435451 0.49360790 0.4428922 0.47588442
##  [9,] 0.3803731 0.1213369 0.46754182 0.27982066 0.51610738 0.4064988 0.57642809
## [10,] 0.3000279 0.3267781 0.31135898 0.25067578 0.32056777 0.3925044 0.30098140
## [11,] 0.1510094 0.3290695 0.01938064 0.03683619 0.00452546 0.0954774 0.01200796
##              [,8]      [,9]     [,10]       [,11]
##  [1,]  0.36466244 0.3803731 0.3000279  0.15100941
##  [2,]  0.13293601 0.1213369 0.3267781  0.32906945
##  [3,]  0.50769641 0.4675418 0.3113590  0.01938064
##  [4,]  0.44435451 0.2798207 0.2506758  0.03683619
##  [5,]  0.49360790 0.5161074 0.3205678  0.00452546
##  [6,]  0.44289223 0.4064988 0.3925044  0.09547740
##  [7,]  0.47588442 0.5764281 0.3009814  0.01200796
##  [8,]  1.00000000 0.4426668 0.4093777 -0.04229059
##  [9,]  0.44266682 1.0000000 0.3446451  0.05147860
## [10,]  0.40937769 0.3446451 1.0000000  0.32548245
## [11,] -0.04229059 0.0514786 0.3254825  1.00000000
#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 29 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
##                                                       
##   Number of observations                           192
##                                                       
## 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.405    0.048    8.472    0.000
##     M2        (b2)    0.050    0.014    3.593    0.000
##     X          (c)   -0.007    0.011   -0.697    0.486
##   M1 ~                                                
##     X         (a1)    0.078    0.015    5.354    0.000
##   M2 ~                                                
##     X         (a2)    0.220    0.050    4.369    0.000
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##  .M1 ~~                                               
##    .M2                0.584    0.358    1.632    0.103
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .Y                 0.621    0.063    9.798    0.000
##    .M1                1.433    0.146    9.798    0.000
##    .M2               16.899    1.725    9.798    0.000
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|)
##     indirect1         0.032    0.007    4.526    0.000
##     indirect2         0.011    0.004    2.775    0.006
##     total             0.035    0.012    3.009    0.003
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 29 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         9
##                                                       
##   Number of observations                           192
##                                                       
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               132.465
##   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)              -1076.194
##   Loglikelihood unrestricted model (H1)      -1076.194
##                                                       
##   Akaike (AIC)                                2170.388
##   Bayesian (BIC)                              2199.705
##   Sample-size adjusted Bayesian (BIC)         2171.196
## 
## 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.405    0.044    9.154    0.000    0.320    0.493
##     M2        (b2)    0.050    0.012    4.179    0.000    0.027    0.075
##     X          (c)   -0.007    0.011   -0.686    0.493   -0.029    0.014
##   M1 ~                                                                  
##     X         (a1)    0.078    0.013    5.815    0.000    0.052    0.105
##   M2 ~                                                                  
##     X         (a2)    0.220    0.057    3.856    0.000    0.110    0.333
##    Std.lv  Std.all
##                   
##     0.405    0.529
##     0.050    0.220
##    -0.007   -0.045
##                   
##     0.078    0.360
##                   
##     0.220    0.301
## 
## Covariances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##  .M1 ~~                                                                 
##    .M2                0.584    0.469    1.245    0.213   -0.265    1.582
##    Std.lv  Std.all
##                   
##     0.584    0.119
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##    .Y                 0.621    0.065    9.511    0.000    0.490    0.744
##    .M1                1.433    0.160    8.967    0.000    1.123    1.743
##    .M2               16.899    2.600    6.499    0.000   12.048   22.118
##    Std.lv  Std.all
##     0.621    0.643
##     1.433    0.870
##    16.899    0.910
## 
## R-Square:
##                    Estimate
##     Y                 0.357
##     M1                0.130
##     M2                0.090
## 
## Defined Parameters:
##                    Estimate  Std.Err  z-value  P(>|z|) ci.lower ci.upper
##     indirect1         0.032    0.007    4.811    0.000    0.020    0.046
##     indirect2         0.011    0.004    2.645    0.008    0.004    0.020
##     total             0.035    0.011    3.244    0.001    0.014    0.057
##    Std.lv  Std.all
##     0.032    0.191
##     0.011    0.066
##     0.035    0.212
parameterEstimates(fit, boot.ci.type="bca.simple")
##          lhs op               rhs     label    est    se      z pvalue ci.lower
## 1          Y  ~                M1        b1  0.405 0.044  9.154  0.000    0.317
## 2          Y  ~                M2        b2  0.050 0.012  4.179  0.000    0.027
## 3          Y  ~                 X         c -0.007 0.011 -0.686  0.493   -0.027
## 4         M1  ~                 X        a1  0.078 0.013  5.815  0.000    0.052
## 5         M2  ~                 X        a2  0.220 0.057  3.856  0.000    0.111
## 6         M1 ~~                M2            0.584 0.469  1.245  0.213   -0.211
## 7          Y ~~                 Y            0.621 0.065  9.511  0.000    0.512
## 8         M1 ~~                M1            1.433 0.160  8.967  0.000    1.153
## 9         M2 ~~                M2           16.899 2.600  6.499  0.000   12.615
## 10         X ~~                 X           34.788 0.000     NA     NA   34.788
## 11 indirect1 :=             a1*b1 indirect1  0.032 0.007  4.811  0.000    0.020
## 12 indirect2 :=             a2*b2 indirect2  0.011 0.004  2.645  0.008    0.004
## 13     total := c+(a1*b1)+(a2*b2)     total  0.035 0.011  3.244  0.001    0.015
##    ci.upper
## 1     0.490
## 2     0.074
## 3     0.015
## 4     0.105
## 5     0.335
## 6     1.696
## 7     0.774
## 8     1.783
## 9    23.034
## 10   34.788
## 11    0.047
## 12    0.021
## 13    0.057
with(pp, mediation.effect.plot(x = aps.standards, mediator = competence, dv = mhc, ylab = "Well-Being", xlab = "Perfectionistic standards"))