1 Data Preparation

dataset <- read.csv(file = params$file, header = T, sep = ",")
#run parallel cores 
options(mc.cores = 8, brms.backend = "cmdstanr", brms.file_refit = "on_change")
#install.packages("loo")
#remotes::install_github("stan-dev/loo")
library(remotes)
library(loo)
library(psych)
library(relativeVariability)
library(brms)
library(cmdstanr)
library(data.table)
library(ggplot2)
library(dplyr)
library(haven)
#library(rstanarm)
library(knitr)
library(rstan)
library(shinystan)

1.1 Rescale Data

dataset$negemo_full_m <- (dataset$negemo_full_m -1)*(4/6)+1
dataset$posemo_full_m <- (dataset$posemo_full_m -1)*(4/6)+1

dataset$neuro_t <- (dataset$neuro_t -1)*(4/6)+1

hist(dataset$negemo_full_m)

1.2 Censoring Data

range(dataset$negemo_full_m, na.rm = T)
## [1] 1 5
range(dataset$posemo_full_m, na.rm = T)
## [1] 1 5
sd(dataset$negemo_full_m, na.rm = T)
## [1] 0.6183701
mean(dataset$negemo_full_m, na.rm = T)
## [1] 1.41108
sd(dataset$posemo_full_m, na.rm = T)
## [1] 0.6631019
mean(dataset$posemo_full_m, na.rm = T)
## [1] 2.950298
sd(dataset$neuro_t, na.rm = T)
## [1] 0.8655486
mean(dataset$neuro_t, na.rm = T)
## [1] 2.524973
qplot(dataset$negemo_full_, binwidth = .1)
## Warning: Removed 1092 rows containing non-finite values (`stat_bin()`).

qplot(dataset$posemo_full_, binwidth = .1)
## Warning: Removed 1085 rows containing non-finite values (`stat_bin()`).

dataset$Acens <- case_when(dataset$negemo_full_m == 1 ~ "left",
                         dataset$negemo_full_m == 5 ~ "right",
                         TRUE ~ "none")
table(dataset$Acens)
## 
##  left  none right 
##  1474  4656     8
dataset$Acens_p <- case_when(dataset$posemo_full_m == 1 ~ "left",
                         dataset$posemo_full_m == 5 ~ "right",
                         TRUE ~ "none")
table(dataset$Acens_p)
## 
##  left  none right 
##    16  6114     8

2 BCLSM Negative Emotion

Kn_model_neuro3 <- brm(bf(negemo_full_m | cens(Acens) ~ neuro_t + (1|person_id),
                       sigma ~ neuro_t+ (1|person_id)), data = dataset,
                       iter = 7000, warmup = 2000,  chains = 4,
                       control = list(adapt_delta = .99), init = 0.1,
                       file = paste("models/", params$file, "Kn_model_neuro3"))
## Warning: Rows containing NAs were excluded from the model.
print(Kn_model_neuro3)
##  Family: gaussian 
##   Links: mu = identity; sigma = log 
## Formula: negemo_full_m | cens(Acens) ~ neuro_t + (1 | person_id) 
##          sigma ~ neuro_t + (1 | person_id)
##    Data: dataset (Number of observations: 4650) 
##   Draws: 4 chains, each with iter = 7000; warmup = 2000; thin = 1;
##          total post-warmup draws = 20000
## 
## Group-Level Effects: 
## ~person_id (Number of levels: 78) 
##                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept)           0.51      0.05     0.43     0.60 1.00     1670     3013
## sd(sigma_Intercept)     0.65      0.06     0.55     0.77 1.00     1895     3736
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           0.91      0.18     0.57     1.26 1.00      875     2075
## sigma_Intercept    -1.28      0.23    -1.72    -0.82 1.00     1249     2482
## neuro_t             0.15      0.07     0.02     0.27 1.00     1014     2656
## sigma_neuro_t       0.13      0.09    -0.04     0.30 1.00     1290     2726
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
plot(Kn_model_neuro3)

pp_check(Kn_model_neuro3)
## Using 10 posterior draws for ppc type 'dens_overlay' by default.
## Warning: Censored responses are not shown in 'pp_check'.

prior_summary(Kn_model_neuro3)
##                   prior     class      coef     group resp  dpar nlpar lb ub       source
##                  (flat)         b                                                 default
##                  (flat)         b   neuro_t                                  (vectorized)
##                  (flat)         b                          sigma                  default
##                  (flat)         b   neuro_t                sigma             (vectorized)
##  student_t(3, 1.1, 2.5) Intercept                                                 default
##    student_t(3, 0, 2.5) Intercept                          sigma                  default
##    student_t(3, 0, 2.5)        sd                                       0         default
##    student_t(3, 0, 2.5)        sd                          sigma        0         default
##    student_t(3, 0, 2.5)        sd           person_id                   0    (vectorized)
##    student_t(3, 0, 2.5)        sd Intercept person_id                   0    (vectorized)
##    student_t(3, 0, 2.5)        sd           person_id      sigma        0    (vectorized)
##    student_t(3, 0, 2.5)        sd Intercept person_id      sigma        0    (vectorized)

2.1 Model comparison

2.1.1 scale vs. no scale parameter

Kn_model_neuro2 <- brm(negemo_full_m | cens(Acens) ~ neuro_t + (1|person_id), data = dataset,
                    iter = 6000, warmup = 2000,  chains = 4,
                    control = list(adapt_delta = .98), inits = 0.1 ,
                    file = paste("models/", params$file, "Kn_model_neuro2"))
## Warning: Argument 'inits' is deprecated. Please use argument 'init' instead.
## Warning: Rows containing NAs were excluded from the model.
print(Kn_model_neuro2)
##  Family: gaussian 
##   Links: mu = identity; sigma = identity 
## Formula: negemo_full_m | cens(Acens) ~ neuro_t + (1 | person_id) 
##    Data: dataset (Number of observations: 4650) 
##   Draws: 4 chains, each with iter = 6000; warmup = 2000; thin = 1;
##          total post-warmup draws = 16000
## 
## Group-Level Effects: 
## ~person_id (Number of levels: 78) 
##               Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept)     0.54      0.05     0.46     0.64 1.00      935     1634
## 
## Population-Level Effects: 
##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept     0.82      0.19     0.46     1.19 1.01      737     1681
## neuro_t       0.18      0.07     0.04     0.31 1.01      753     1594
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.54      0.01     0.53     0.56 1.00    12281    11072
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
modelA <- Kn_model_neuro2
modelB <- Kn_model_neuro3

modelA <- add_criterion(modelA, "loo")
modelB <- add_criterion(modelB, "loo")

loo <- loo_compare(modelA,modelB, criterion = "loo")

loo <- as.data.frame(loo)

loo$Dataset <- params$file
loo <- tibble::rownames_to_column(loo, "model")
library("writexl")
write_xlsx(loo,paste0("loo", params$file, ".xlsx"))

kable(loo)
model elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looic Dataset
modelB 0.00 0.00000 -2437.735 88.03844 252.72362 28.332715 4875.471 176.0769 Dataset 8 public.csv
modelA -1095.98 77.20871 -3533.716 83.72160 80.11214 3.362601 7067.431 167.4432 Dataset 8 public.csv

2.1.2 censoring vs. no censoring

Kn_model_neuro4 <- brm(bf(negemo_full_m  ~ neuro_t + (1|person_id),
                       sigma ~ neuro_t+ (1|person_id)), data = dataset,
                       iter = 7000, warmup = 2000,  chains = 4,
                       control = list(adapt_delta = .9999), init = 0,
                       file = paste("models/", params$file, "Kn_model_neuro4"))
## Warning: Rows containing NAs were excluded from the model.
print(Kn_model_neuro4)
##  Family: gaussian 
##   Links: mu = identity; sigma = log 
## Formula: negemo_full_m ~ neuro_t + (1 | person_id) 
##          sigma ~ neuro_t + (1 | person_id)
##    Data: dataset (Number of observations: 4650) 
##   Draws: 4 chains, each with iter = 7000; warmup = 2000; thin = 1;
##          total post-warmup draws = 20000
## 
## Group-Level Effects: 
## ~person_id (Number of levels: 78) 
##                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept)           0.42      0.04     0.36     0.50 1.01      905     1610
## sd(sigma_Intercept)     0.69      0.06     0.59     0.81 1.00      838     2050
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           1.09      0.14     0.82     1.36 1.01      469      795
## sigma_Intercept    -1.89      0.24    -2.37    -1.42 1.01      432      871
## neuro_t             0.12      0.05     0.02     0.23 1.01      554     1336
## sigma_neuro_t       0.24      0.09     0.07     0.42 1.00      452      905
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
extract_param <- function(model, parameter) {
  ci <- posterior_summary(model, variable = parameter)
  est <- sprintf("%.2f %.2f [%.2f;%.2f]", ci[,"Estimate"],ci[,"Est.Error"], ci[,"Q2.5"], ci[,"Q97.5"])
  est
}

results_Cens <- data.frame(matrix(nrow = 2, 
                             ncol = 6+1)) 
names(results_Cens) <- c("model", "negemo_b_neuro", "negemo_b_neuro_sigma", "negemo_sigma",
                    "posemo_b_neuro", "posemo_b_neuro_sigma", "posemo_sigma"
                    )

results_Cens$model <- c("modelCensoring", "modelnoCensoring")

#NA

results_Cens[1, "negemo_b_neuro"] <- extract_param(Kn_model_neuro3, "b_neuro_t")
results_Cens[1, "negemo_b_neuro_sigma"] <- extract_param(Kn_model_neuro3, "b_sigma_neuro_t")
results_Cens[1, "negemo_sigma"] <- extract_param(Kn_model_neuro3, "b_sigma_Intercept")

results_Cens[2, "negemo_b_neuro"] <- extract_param(Kn_model_neuro4, "b_neuro_t")
results_Cens[2, "negemo_b_neuro_sigma"] <- extract_param(Kn_model_neuro4, "b_sigma_neuro_t")
results_Cens[2, "negemo_sigma"] <- extract_param(Kn_model_neuro4, "b_sigma_Intercept")

2.1.3 BCLSM vs. model C (two-part model)

dataset <- dataset %>% left_join(dataset %>% distinct(person_id, neuro_t) %>% mutate(neuro_Q =Hmisc::cut2(neuro_t, g = 4)), by = c("person_id", "neuro_t"))


Kn_model_neuro_jinxed <- brm(bf(negemo_full_m | cens(Acens) ~ neuro_t + (1|gr(person_id, by = neuro_Q)),
  sigma ~ neuro_t + (1|person_id)), data = dataset,
  iter = 5000, warmup = 2000,  chains = 4,
  control = list(adapt_delta = .99), init = 0.1,
  file = paste("models/", params$file, "Kn_model_neuro_jinxed"))
## Warning: Rows containing NAs were excluded from the model.
print(Kn_model_neuro_jinxed)
##  Family: gaussian 
##   Links: mu = identity; sigma = log 
## Formula: negemo_full_m | cens(Acens) ~ neuro_t + (1 | gr(person_id, by = neuro_Q)) 
##          sigma ~ neuro_t + (1 | person_id)
##    Data: dataset (Number of observations: 4650) 
##   Draws: 4 chains, each with iter = 5000; warmup = 2000; thin = 1;
##          total post-warmup draws = 12000
## 
## Group-Level Effects: 
## ~person_id (Number of levels: 78) 
##                                  Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept:neuro_Q[1.00,2.00))     0.47      0.09     0.34     0.68 1.00     1712     2916
## sd(Intercept:neuro_Q[2.00,2.67))     0.31      0.06     0.21     0.45 1.00     1642     3389
## sd(Intercept:neuro_Q[2.67,3.33))     0.72      0.14     0.50     1.05 1.00     1083     2035
## sd(Intercept:neuro_Q[3.33,5.00])     0.63      0.13     0.44     0.94 1.00     1858     3465
## sd(sigma_Intercept)                  0.64      0.06     0.55     0.76 1.01     1145     1820
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           0.83      0.18     0.46     1.18 1.01      797     1426
## sigma_Intercept    -1.29      0.23    -1.73    -0.85 1.00      943     1738
## neuro_t             0.15      0.08     0.01     0.30 1.00      713     1566
## sigma_neuro_t       0.13      0.08    -0.03     0.30 1.00      883     1756
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
modelB <- Kn_model_neuro3
modelC <- Kn_model_neuro_jinxed

modelB <- add_criterion(modelB, "loo")
modelC <- add_criterion(modelC, "loo")

loo_c <- loo_compare(modelB,modelC, criterion = "loo")

loo_c <- as.data.frame(loo_c)

loo_c$Dataset <- params$file
loo_c <- tibble::rownames_to_column(loo_c, "model")

library("writexl")
write_xlsx(loo_c,paste0("loo_c", params$file, ".xlsx"))

kable(loo_c)
model elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looic Dataset
modelC 0.000000 0.000000 -2436.469 87.90374 251.2105 27.98919 4872.938 175.8075 Dataset 8 public.csv
modelB -1.266533 1.952663 -2437.735 88.03844 252.7236 28.33271 4875.471 176.0769 Dataset 8 public.csv

2.2 control for gender

dataset$gender <- as.factor(dataset$gender)

Kn_model_sex <- brm(bf(negemo_full_m | cens(Acens) ~ neuro_t + gender + (1|person_id),
                       sigma ~ neuro_t + gender), data = dataset,
                       iter = 9000, warmup = 2000, chains = 8,
                       control = list(adapt_delta = .99), inits = 0.1,
                    file = paste("models/", params$file, "Kn_model_sex"))
print(Kn_model_sex)
pp_check(Kn_model_sex)

plot(Kn_model_sex)

3 BCLSM Positive Emotion

Kp_model_neuro3 <- brm(bf(posemo_full_m | cens(Acens_p) ~ neuro_t + (1|person_id),
                       sigma ~ neuro_t + (1|person_id)), data = dataset,
                       chains = 4,
                       control = list(adapt_delta = .95), inits = 0.1,
                       iter = 7000, warmup = 2000,
                    file = paste("models/", params$file, "Kp_model_neuro3"))
## Warning: Argument 'inits' is deprecated. Please use argument 'init' instead.
## Warning: Rows containing NAs were excluded from the model.
print(Kp_model_neuro3)
##  Family: gaussian 
##   Links: mu = identity; sigma = log 
## Formula: posemo_full_m | cens(Acens_p) ~ neuro_t + (1 | person_id) 
##          sigma ~ neuro_t + (1 | person_id)
##    Data: dataset (Number of observations: 4655) 
##   Draws: 4 chains, each with iter = 7000; warmup = 2000; thin = 1;
##          total post-warmup draws = 20000
## 
## Group-Level Effects: 
## ~person_id (Number of levels: 78) 
##                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept)           0.47      0.04     0.40     0.55 1.00     1211     2675
## sd(sigma_Intercept)     0.41      0.04     0.35     0.49 1.00     1890     3722
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           3.43      0.16     3.11     3.75 1.01      796     1677
## sigma_Intercept    -1.04      0.15    -1.33    -0.74 1.00     1310     2452
## neuro_t            -0.18      0.06    -0.30    -0.06 1.00      852     1485
## sigma_neuro_t       0.06      0.05    -0.05     0.17 1.00     1376     2543
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
pp_check(Kp_model_neuro3)
## Using 10 posterior draws for ppc type 'dens_overlay' by default.
## Warning: Censored responses are not shown in 'pp_check'.

plot(Kp_model_neuro3)

prior_summary(Kp_model_neuro3)
##                 prior     class      coef     group resp  dpar nlpar lb ub       source
##                (flat)         b                                                 default
##                (flat)         b   neuro_t                                  (vectorized)
##                (flat)         b                          sigma                  default
##                (flat)         b   neuro_t                sigma             (vectorized)
##  student_t(3, 3, 2.5) Intercept                                                 default
##  student_t(3, 0, 2.5) Intercept                          sigma                  default
##  student_t(3, 0, 2.5)        sd                                       0         default
##  student_t(3, 0, 2.5)        sd                          sigma        0         default
##  student_t(3, 0, 2.5)        sd           person_id                   0    (vectorized)
##  student_t(3, 0, 2.5)        sd Intercept person_id                   0    (vectorized)
##  student_t(3, 0, 2.5)        sd           person_id      sigma        0    (vectorized)
##  student_t(3, 0, 2.5)        sd Intercept person_id      sigma        0    (vectorized)

3.1 Model comparison

3.1.1 scale vs. no scale parameter

Kp_model_neuro2 <- brm(posemo_full_m | cens(Acens_p) ~ neuro_t + (1|person_id), data = dataset,
                    iter = 7000, warmup = 2000, chains = 4,
                   control = list(adapt_delta = .95), inits = 0.1,
                    file = paste("models/", params$file, "Kp_model_neuro2"))
## Warning: Argument 'inits' is deprecated. Please use argument 'init' instead.
## Warning: Rows containing NAs were excluded from the model.
print(Kp_model_neuro2)
##  Family: gaussian 
##   Links: mu = identity; sigma = identity 
## Formula: posemo_full_m | cens(Acens_p) ~ neuro_t + (1 | person_id) 
##    Data: dataset (Number of observations: 4655) 
##   Draws: 4 chains, each with iter = 7000; warmup = 2000; thin = 1;
##          total post-warmup draws = 20000
## 
## Group-Level Effects: 
## ~person_id (Number of levels: 78) 
##               Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept)     0.46      0.04     0.39     0.55 1.00     1102     2256
## 
## Population-Level Effects: 
##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept     3.44      0.16     3.11     3.75 1.01      647     1323
## neuro_t      -0.19      0.06    -0.30    -0.06 1.01      677     1228
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.47      0.00     0.46     0.48 1.00    13623    13369
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
modelAp <- Kp_model_neuro2
modelBp <- Kp_model_neuro3


modelAp <- add_criterion(modelAp, "loo")
modelBp <- add_criterion(modelBp, "loo")

looP <- loo_compare(modelAp,modelBp, criterion = "loo")

looP <- as.data.frame(looP)

looP$Dataset <- params$file
looP <- tibble::rownames_to_column(looP, "model")
library("writexl")
write_xlsx(looP,paste0("looP", params$file, ".xlsx"))

kable(looP)
model elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looic Dataset
modelBp 0.0000 0.00000 -2513.414 82.11583 213.22310 24.565637 5026.828 164.2317 Dataset 8 public.csv
modelAp -632.8817 57.16816 -3146.296 70.88340 79.57036 2.466759 6292.592 141.7668 Dataset 8 public.csv

3.1.2 censoring vs. no censoring

Kp_model_neuro4 <- brm(bf(posemo_full_m ~ neuro_t + (1|person_id),
                       sigma ~ neuro_t + (1|person_id)), data = dataset,
                       chains = 4,
                       control = list(adapt_delta = .9999), inits = 0,
                       iter = 7000, warmup = 2000,
                    file = paste("models/", params$file, "Kp_model_neuro4"))
## Warning: Argument 'inits' is deprecated. Please use argument 'init' instead.
## Warning: Rows containing NAs were excluded from the model.
print(Kp_model_neuro4)
##  Family: gaussian 
##   Links: mu = identity; sigma = log 
## Formula: posemo_full_m ~ neuro_t + (1 | person_id) 
##          sigma ~ neuro_t + (1 | person_id)
##    Data: dataset (Number of observations: 4655) 
##   Draws: 4 chains, each with iter = 7000; warmup = 2000; thin = 1;
##          total post-warmup draws = 20000
## 
## Group-Level Effects: 
## ~person_id (Number of levels: 78) 
##                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept)           0.47      0.04     0.40     0.55 1.00     2071     4036
## sd(sigma_Intercept)     0.40      0.03     0.34     0.48 1.00     3073     4941
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           3.43      0.16     3.11     3.76 1.01     1177     2405
## sigma_Intercept    -1.05      0.14    -1.34    -0.77 1.00     2017     4125
## neuro_t            -0.18      0.06    -0.30    -0.06 1.01     1312     2823
## sigma_neuro_t       0.06      0.05    -0.05     0.16 1.00     2033     4329
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
#pa

results_Cens[1, "posemo_b_neuro"] <- extract_param(Kp_model_neuro3, "b_neuro_t")
results_Cens[1, "posemo_b_neuro_sigma"] <- extract_param(Kp_model_neuro3, "b_sigma_neuro_t")
results_Cens[1, "posemo_sigma"] <- extract_param(Kp_model_neuro3, "b_sigma_Intercept")


results_Cens[2, "posemo_b_neuro"] <- extract_param(Kp_model_neuro4, "b_neuro_t")
results_Cens[2, "posemo_b_neuro_sigma"] <- extract_param(Kp_model_neuro4, "b_sigma_neuro_t")
results_Cens[2, "posemo_sigma"] <- extract_param(Kp_model_neuro4, "b_sigma_Intercept")

3.1.3 BCLSM vs. model C (two-part model)

Kp_model_neuro_jinxed <- brm(bf(posemo_full_m | cens(Acens) ~ neuro_t + (1|gr(person_id, by = neuro_Q)),
     sigma ~ neuro_t + (1|person_id)), data = dataset,
  iter = 5000, warmup = 2000,  chains = 4,
  control = list(adapt_delta = .99), init = 0.1,
  file = paste("models/", params$file, "Kp_model_neuro_jinxed"))
## Warning: Rows containing NAs were excluded from the model.
print(Kp_model_neuro_jinxed)
##  Family: gaussian 
##   Links: mu = identity; sigma = log 
## Formula: posemo_full_m | cens(Acens) ~ neuro_t + (1 | gr(person_id, by = neuro_Q)) 
##          sigma ~ neuro_t + (1 | person_id)
##    Data: dataset (Number of observations: 4655) 
##   Draws: 4 chains, each with iter = 5000; warmup = 2000; thin = 1;
##          total post-warmup draws = 12000
## 
## Group-Level Effects: 
## ~person_id (Number of levels: 78) 
##                                  Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept:neuro_Q[1.00,2.00))     0.37      0.07     0.26     0.53 1.00     1800     3601
## sd(Intercept:neuro_Q[2.00,2.67))     0.52      0.09     0.37     0.72 1.00     1042     2455
## sd(Intercept:neuro_Q[2.67,3.33))     0.39      0.08     0.27     0.57 1.00     1423     2877
## sd(Intercept:neuro_Q[3.33,5.00])     0.61      0.12     0.43     0.89 1.00     1592     3567
## sd(sigma_Intercept)                  0.42      0.04     0.36     0.50 1.00     1912     3473
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           3.11      0.15     2.81     3.41 1.00      913     1822
## sigma_Intercept    -0.95      0.15    -1.25    -0.65 1.00     1127     2114
## neuro_t            -0.12      0.06    -0.24    -0.00 1.01      881     1685
## sigma_neuro_t       0.04      0.06    -0.07     0.16 1.00     1136     2232
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
modelB <- Kp_model_neuro3
modelC <- Kp_model_neuro_jinxed

modelB <- add_criterion(modelB, "loo")
modelC <- add_criterion(modelC, "loo")

loo_cP <- loo_compare(modelB,modelC, criterion = "loo")
## Warning: Not all models have the same y variable. ('yhash' attributes do not match)
loo_cP <- as.data.frame(loo_cP)

loo_cP$Dataset <- params$file
#loo_cP <- tibble::rownames_to_column(loo_c, "model")
library("writexl")
write_xlsx(loo_cP,paste0("loo_cP", params$file, ".xlsx"))

kable(loo_cP)
elpd_diff se_diff elpd_loo se_elpd_loo p_loo se_p_loo looic se_looic Dataset
modelC 0.0000 0.00000 -2328.892 59.51439 174.4699 11.20535 4657.785 119.0288 Dataset 8 public.csv
modelB -184.5219 56.00635 -2513.414 82.11583 213.2231 24.56564 5026.828 164.2317 Dataset 8 public.csv
extract_param <- function(model, parameter) {
  ci <- posterior_summary(model, variable = parameter)
  est <- sprintf("%.2f %.2f [%.2f;%.2f]", ci[,"Estimate"],ci[,"Est.Error"], ci[,"Q2.5"], ci[,"Q97.5"])
  est
}

results_K <- data.frame(matrix(nrow = 7, 
                             ncol = 8+1)) 
names(results_K) <- c("model", "negemo_b_neuro", "negemo_b_neuro_sigma", "negemo_sigma", "b_neg_sigma_sex",
                    "posemo_b_neuro", "posemo_b_neuro_sigma", "posemo_sigma", "b_pos_sigma_sex"
                    )

results_K$model <- c("model1", "model2", "model3",
                  "RSD", "RSD_weight", "SD", "gender")

#NA

results_K[2, "negemo_b_neuro"] <- extract_param(Kn_model_neuro2, "b_neuro_t")
results_K[2, "negemo_sigma"] <- extract_param(Kn_model_neuro2, "sigma")

results_K[3, "negemo_b_neuro"] <- extract_param(Kn_model_neuro3, "b_neuro_t")
results_K[3, "negemo_b_neuro_sigma"] <- extract_param(Kn_model_neuro3, "b_sigma_neuro_t")
results_K[3, "negemo_sigma"] <- extract_param(Kn_model_neuro3, "b_sigma_Intercept")
#gender 

results_K[7, "negemo_b_neuro"] <- extract_param(Kn_model_sex, "b_neuro_t")
results_K[7, "negemo_b_neuro_sigma"] <- extract_param(Kn_model_sex, "b_sigma_neuro_t")
results_K[7, "negemo_sigma"] <- extract_param(Kn_model_sex, "b_sigma_Intercept")
results_K[7, "b_neg_sigma_sex"] <- extract_param(Kn_model_sex, "b_sigma_gender1")
#pa
results_K[2, "posemo_b_neuro"] <- extract_param(Kp_model_neuro2, "b_neuro_t")
results_K[2, "posemo_sigma"] <- extract_param(Kp_model_neuro2, "sigma")

results_K[3, "posemo_b_neuro"] <- extract_param(Kp_model_neuro3, "b_neuro_t")
results_K[3, "posemo_b_neuro_sigma"] <- extract_param(Kp_model_neuro3, "b_sigma_neuro_t")
results_K[3, "posemo_sigma"] <- extract_param(Kp_model_neuro3, "b_sigma_Intercept")

4 RVI (Relative Variability Index)

data_w <- unique(dataset[,2:5])

4.1 Unweighted RVI

data_w$RSD_NA <- NA
for (i in 1:nrow(data_w)) {
      data_w$RSD_NA[i] <- relativeSD(dataset$negemo_full_m[dataset$person_id == data_w$person_id[i]],
                                     1, 5)
    }

range(data_w$RSD_NA, na.rm = T)
## [1] 0.08121707 0.69932240
mean(data_w$RSD_NA, na.rm = T)
## [1] 0.3363936
sd(data_w$RSD_NA, na.rm = T)
## [1] 0.1406409
data_w$logrsd_n <- log(data_w$RSD_NA)

#plot(data_w$logrsd_n)

m_rvi_na <- brm(logrsd_n ~ neuro_t, data= data_w,
                file = paste("models/", params$file, "Kn_model_logrsd_uw"))
print(m_rvi_na)
##  Family: gaussian 
##   Links: mu = identity; sigma = identity 
## Formula: logrsd_n ~ neuro_t 
##    Data: data_w (Number of observations: 78) 
##   Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
##          total post-warmup draws = 4000
## 
## Population-Level Effects: 
##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept    -1.23      0.16    -1.54    -0.92 1.00     3786     2952
## neuro_t       0.03      0.06    -0.08     0.15 1.00     3874     3036
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.45      0.04     0.39     0.53 1.00     3817     2503
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
results_K[4,3] <- extract_param(m_rvi_na, "b_neuro_t")



data_w$RSD_PA <- NA
for (i in 1:nrow(data_w)) {
      data_w$RSD_PA[i] <- relativeSD(dataset$posemo_full_m[dataset$person_id == data_w$person_id[i]],
                                     1, 5)
}

range(data_w$RSD_PA)
## [1] 0.07800037 0.57423965
data_w$logrsd_p <- log(data_w$RSD_PA)


m_rvi_pa <- brm(logrsd_p ~ neuro_t, data= data_w,
                 file = paste("models/", params$file, "Kp_model_logrsd_uw"))
print(m_rvi_pa)
##  Family: gaussian 
##   Links: mu = identity; sigma = identity 
## Formula: logrsd_p ~ neuro_t 
##    Data: data_w (Number of observations: 78) 
##   Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
##          total post-warmup draws = 4000
## 
## Population-Level Effects: 
##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept    -1.75      0.15    -2.04    -1.45 1.00     4031     2881
## neuro_t       0.07      0.05    -0.04     0.18 1.00     4190     2688
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.41      0.03     0.35     0.48 1.00     4483     3136
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
results_K[4,6] <- extract_param(m_rvi_pa, "b_neuro_t")

4.2 Weighted RVI

data_w$mean_NA <- NA
for (i in 1:nrow(data_w)) {
      data_w$mean_NA[i] <- mean(dataset$negemo_full_m[dataset$person_id == data_w$person_id[i]],
                                   na.rm = T)
    }

mean(data_w$mean_NA)
## [1] 1.43376
sd(data_w$mean_NA)
## [1] 0.4758251
data_w$mean_PA <- NA
for (i in 1:nrow(data_w)) {
      data_w$mean_PA[i] <- mean(dataset$posemo_full_m[dataset$person_id == data_w$person_id[i]],
                                   na.rm = T)
}

mean(data_w$mean_PA)
## [1] 2.935263
sd(data_w$mean_PA)
## [1] 0.4851171
data_w$weight_NA <- NA
for (i in 1:nrow(data_w)) {
    if (!is.na(data_w$mean_NA[i])) {
      data_w$weight_NA[i] <- maximumSD(data_w$mean_NA[i], # Mittelwert
                                       1,  # Minimum
                                       5,  # Maximum
                                       sum(!is.na(dataset$negemo_full_m[dataset$person_id == data_w$person_id[i]])) 
      ) 
      # W as reported in paper
      data_w$weight_NA[i] <- data_w$weight_NA[i]^2
    }
  }

mean(data_w$weight_NA)
## [1] 1.299776
sd(data_w$weight_NA)
## [1] 1.150438
range(data_w$weight_NA)
## [1] 0.005664317 4.005961151
m_rvi_na_w <- brm(logrsd_n| weights(weight_NA) ~ neuro_t, data= data_w,
                    file = paste("models/", params$file, "Kn_model_logrsd"))
## Warning: Rows containing NAs were excluded from the model.
print(m_rvi_na_w)
##  Family: gaussian 
##   Links: mu = identity; sigma = identity 
## Formula: logrsd_n | weights(weight_NA) ~ neuro_t 
##    Data: data_w (Number of observations: 78) 
##   Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
##          total post-warmup draws = 4000
## 
## Population-Level Effects: 
##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept    -1.45      0.17    -1.77    -1.12 1.00     2970     2287
## neuro_t       0.11      0.06    -0.00     0.22 1.00     3374     2861
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.49      0.03     0.43     0.57 1.00     3312     2924
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
results_K[5,3] <- extract_param(m_rvi_na_w, "b_neuro_t")



data_w$weight_PA <- NA
for (i in 1:nrow(data_w)) {
    if (!is.na(data_w$mean_PA[i])) {
      data_w$weight_PA[i] <- maximumSD(data_w$mean_PA[i], # Mittelwert
                                       1,  # Minimum
                                       5,  # Maximum
                                       sum(!is.na(dataset$posemo_full_m[dataset$person_id == data_w$person_id[i]])) 
      ) 
      # W as reported in paper
      data_w$weight_PA[i] <- data_w$weight_PA[i]^2
    }
  }

m_rvi_pa_w <- brm(logrsd_p| weights(weight_PA) ~ neuro_t, data= data_w,
                    file = paste("models/", params$file, "Kp_model_logrsd"))
## Warning: Rows containing NAs were excluded from the model.
print(m_rvi_pa_w)
##  Family: gaussian 
##   Links: mu = identity; sigma = identity 
## Formula: logrsd_p | weights(weight_PA) ~ neuro_t 
##    Data: data_w (Number of observations: 78) 
##   Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
##          total post-warmup draws = 4000
## 
## Population-Level Effects: 
##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept    -1.74      0.07    -1.88    -1.60 1.00     3923     3058
## neuro_t       0.07      0.03     0.01     0.12 1.00     3823     2985
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.41      0.02     0.38     0.44 1.00     4973     3197
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
results_K[5,6] <- extract_param(m_rvi_pa_w, "b_neuro_t")

5 SD

data_w$sd_NA <- NA
for (i in 1:nrow(data_w)) {
      data_w$sd_NA[i] <- sd(dataset$negemo_full_m[dataset$person_id == data_w$person_id[i]],
                                   na.rm = T)
    }

data_w$sd_PA <- NA
for (i in 1:nrow(data_w)) {
      data_w$sd_PA[i] <- sd(dataset$posemo_full_m[dataset$person_id == data_w$person_id[i]],
                                   na.rm = T)
    }

mean(data_w$sd_NA)
## [1] 0.3452938
mean(data_w$sd_PA)
## [1] 0.4366325
data_w$sd_PA[data_w$sd_PA == 0] <- NA   
data_w$sd_NA[data_w$sd_NA == 0] <- NA   


data_w$logsd_NA <- log(data_w$sd_NA)
data_w$logsd_PA <- log(data_w$sd_PA)
m_sd_na <- brm(logsd_NA ~ neuro_t, data= data_w,
                    file = paste("models/", params$file, "Kn_model_logsd"))
## Warning: Rows containing NAs were excluded from the model.
m_sd_na
##  Family: gaussian 
##   Links: mu = identity; sigma = identity 
## Formula: logsd_NA ~ neuro_t 
##    Data: data_w (Number of observations: 78) 
##   Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
##          total post-warmup draws = 4000
## 
## Population-Level Effects: 
##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept    -1.88      0.24    -2.36    -1.40 1.00     4601     3241
## neuro_t       0.23      0.09     0.05     0.41 1.00     4694     3277
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.70      0.06     0.59     0.82 1.00     3814     3177
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
results_K[6,3] <- extract_param(m_sd_na, "b_neuro_t")

m_sd_pa <- brm(logsd_PA ~ neuro_t, data= data_w,
                    file = paste("models/", params$file, "Kp_model_logsd"))
## Warning: Rows containing NAs were excluded from the model.
m_sd_pa
##  Family: gaussian 
##   Links: mu = identity; sigma = identity 
## Formula: logsd_PA ~ neuro_t 
##    Data: data_w (Number of observations: 78) 
##   Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
##          total post-warmup draws = 4000
## 
## Population-Level Effects: 
##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept    -1.05      0.14    -1.34    -0.77 1.00     3942     3013
## neuro_t       0.06      0.05    -0.05     0.16 1.00     4053     3046
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.41      0.03     0.35     0.49 1.00     3583     2877
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
results_K[6,6] <- extract_param(m_sd_pa, "b_neuro_t")
library("writexl")

write_xlsx(results_K,paste0("", params$file, ".xlsx"))

6 Incremental Validity of SD

na_noneurot <- brm(bf(negemo_full_m | cens(Acens) ~  (1|person_id),
                       sigma ~ (1|person_id)), data = dataset,
                       iter = 7000, warmup = 2000,chains = 4,
                      control = list(adapt_delta = .99), init = 0.1,
                   file = "na_noneurot")
## Warning: Rows containing NAs were excluded from the model.
print(na_noneurot)
##  Family: gaussian 
##   Links: mu = identity; sigma = log 
## Formula: negemo_full_m | cens(Acens) ~ (1 | person_id) 
##          sigma ~ (1 | person_id)
##    Data: dataset (Number of observations: 5046) 
##   Draws: 4 chains, each with iter = 7000; warmup = 2000; thin = 1;
##          total post-warmup draws = 20000
## 
## Group-Level Effects: 
## ~person_id (Number of levels: 85) 
##                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept)           0.55      0.05     0.47     0.64 1.01     1500     3508
## sd(sigma_Intercept)     0.65      0.05     0.55     0.76 1.00     1873     3570
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           1.31      0.06     1.19     1.43 1.01      446     1035
## sigma_Intercept    -0.96      0.07    -1.11    -0.82 1.00      798     1875
## 
## Draws were sampled using sample(hmc). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
rans <- coef(na_noneurot, summary = T)


rans_i <- as.data.frame(rans$person_id[,,"Intercept"]) %>% tibble::rownames_to_column("person_id")
rans_s <- as.data.frame(rans$person_id[,,"sigma_Intercept"]) %>% tibble::rownames_to_column("person_id")
nrow(rans_s)
## [1] 85
nrow(rans_i)
## [1] 85
nrow(data_w)
## [1] 85
dat <- merge(rans_s, rans_i, all = T, by= "person_id")
dat <- merge(dat, data_w, all = T, by= "person_id")

names(dat)[2] <- "Est.SD"
names(dat)[6] <- "Est.M"

fit1 <- lm(neuro_t ~ Est.SD + Est.M , data=dat)
summary(fit1)
## 
## Call:
## lm(formula = neuro_t ~ Est.SD + Est.M, data = dat)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.56872 -0.63540 -0.07836  0.47728  2.54748 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.2267     0.3406   6.537 6.75e-09 ***
## Est.SD        0.1756     0.1605   1.094   0.2774    
## Est.M         0.3692     0.1980   1.865   0.0661 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8547 on 75 degrees of freedom
##   (7 observations deleted due to missingness)
## Multiple R-squared:  0.07438,    Adjusted R-squared:  0.0497 
## F-statistic: 3.014 on 2 and 75 DF,  p-value: 0.0551
fit1.2 <- lm(neuro_t ~  Est.M , data=dat)
summary(fit1.2)
## 
## Call:
## lm(formula = neuro_t ~ Est.M, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.5759 -0.6392 -0.0664  0.4946  2.7480 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   1.9924     0.2652   7.513 9.37e-11 ***
## Est.M         0.4220     0.1922   2.195   0.0312 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8558 on 76 degrees of freedom
##   (7 observations deleted due to missingness)
## Multiple R-squared:  0.05961,    Adjusted R-squared:  0.04724 
## F-statistic: 4.817 on 1 and 76 DF,  p-value: 0.03123
aov <- anova(fit1.2, fit1)
aov
## Analysis of Variance Table
## 
## Model 1: neuro_t ~ Est.M
## Model 2: neuro_t ~ Est.SD + Est.M
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1     76 55.659                           
## 2     75 54.784  1   0.87447 1.1972 0.2774
summary(fit1)$r.squared-summary(fit1.2)$r.squared
## [1] 0.01477476
results_SDin <- data.frame(matrix(nrow = 1, ncol = 9))
names(results_SDin) <- c("Dataset","b_SD","Err.SD","p(b_SD)","b_M","Err.M","p(b_M)","ΔR²", "p")

results_SDin$Dataset <- params$file

results_SDin$`ΔR²` <- summary(fit1)$r.squared-summary(fit1.2)$r.squared
results_SDin$`p` <- aov$`Pr(>F)`[2]
results_SDin$Err.SD <- summary(fit1)$coefficients[2,2]
results_SDin$b_SD <- fit1$coefficients[2]

results_SDin$`p(b_SD)` <- summary(fit1)$coefficients[2,4]
results_SDin$b_M <- fit1$coefficients[3]
results_SDin$`p(b_M)` <- summary(fit1)$coefficients[3,4]
results_SDin$Err.M <- summary(fit1)$coefficients[3,2]

  
library("writexl")
write_xlsx(results_SDin,paste0("SD", params$file, ".xlsx"))