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.000000 4.874286
range(dataset$posemo_full_m, na.rm = T)
## [1] 1 5
sd(dataset$negemo_full_m, na.rm = T)
## [1] 0.5923036
mean(dataset$negemo_full_m, na.rm = T)
## [1] 1.456426
sd(dataset$posemo_full_m, na.rm = T)
## [1] 0.6237622
mean(dataset$posemo_full_m, na.rm = T)
## [1] 3.055032
sd(dataset$neuro_t, na.rm = T)
## [1] 0.9765511
mean(dataset$neuro_t, na.rm = T)
## [1] 2.735712
qplot(dataset$negemo_full_, binwidth = .1)
## Warning: Removed 1362 rows containing non-finite values (`stat_bin()`).

qplot(dataset$posemo_full_, binwidth = .1)
## Warning: Removed 1351 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 
## 1155 5823
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 
##     4  6960    14

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: 4875) 
##   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.45      0.04     0.38     0.52 1.00     1790     3819
## sd(sigma_Intercept)     0.62      0.05     0.52     0.73 1.00     2166     2987
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           1.02      0.15     0.73     1.30 1.00      749     1563
## sigma_Intercept    -1.58      0.20    -1.98    -1.18 1.00     1440     3023
## neuro_t             0.13      0.05     0.03     0.23 1.00      776     1830
## sigma_neuro_t       0.16      0.07     0.03     0.30 1.00     1373     2787
## 
## 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.2, 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: 4875) 
##   Draws: 4 chains, each with iter = 6000; warmup = 2000; thin = 1;
##          total post-warmup draws = 16000
## 
## 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.48      0.04     0.41     0.57 1.01      908     1320
## 
## Population-Level Effects: 
##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept     0.92      0.16     0.62     1.24 1.00      777     1589
## neuro_t       0.16      0.05     0.05     0.27 1.00      828     1734
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.45      0.01     0.44     0.46 1.00    13619    10905
## 
## 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.0000 0.0000 -2135.376 89.94285 281.82853 26.680040 4270.751 179.8857 Dataset 9 public.csv
modelA -951.9463 68.8245 -3087.322 84.67427 86.26997 3.524124 6174.644 169.3485 Dataset 9 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: 4875) 
##   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.41      0.03     0.35     0.48 1.00     1151     2200
## sd(sigma_Intercept)     0.76      0.06     0.65     0.88 1.00     1024     1987
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           1.10      0.13     0.84     1.35 1.01      472     1160
## sigma_Intercept    -2.07      0.25    -2.55    -1.57 1.01      544     1229
## neuro_t             0.12      0.05     0.03     0.21 1.01      501      909
## sigma_neuro_t       0.26      0.09     0.09     0.43 1.01      551     1164
## 
## 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: 4875) 
##   Draws: 4 chains, each with iter = 5000; warmup = 2000; thin = 1;
##          total post-warmup draws = 12000
## 
## 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:neuro_Q[1.00,2.33))     0.44      0.07     0.33     0.60 1.00     1906     3514
## sd(Intercept:neuro_Q[2.33,3.33))     0.42      0.06     0.32     0.56 1.00     1264     2476
## sd(Intercept:neuro_Q3.33)            0.55      0.12     0.38     0.84 1.00     1596     3408
## sd(Intercept:neuro_Q[3.67,4.67])     0.50      0.10     0.35     0.72 1.00     1637     2975
## sd(sigma_Intercept)                  0.62      0.05     0.52     0.72 1.00     1179     1993
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           1.02      0.15     0.73     1.32 1.01      820     1398
## sigma_Intercept    -1.58      0.21    -1.99    -1.16 1.00      882     1734
## neuro_t             0.13      0.05     0.02     0.23 1.00      879     1057
## sigma_neuro_t       0.17      0.07     0.02     0.31 1.00      939     1596
## 
## 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
modelB 0.000000 0.000000 -2135.376 89.94285 281.8285 26.68004 4270.751 179.8857 Dataset 9 public.csv
modelC -0.592416 1.919932 -2135.968 90.01112 282.3491 26.97450 4271.936 180.0222 Dataset 9 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: 4885) 
##   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.39      0.03     0.33     0.45 1.00     1333     2608
## sd(sigma_Intercept)     0.37      0.03     0.32     0.44 1.00     2118     4741
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           3.75      0.13     3.50     4.00 1.00      815     1877
## sigma_Intercept    -1.14      0.12    -1.39    -0.90 1.00     1433     3109
## neuro_t            -0.24      0.04    -0.32    -0.16 1.00      872     1913
## sigma_neuro_t       0.07      0.04    -0.01     0.16 1.00     1446     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).
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.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)

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: 4885) 
##   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.39      0.03     0.33     0.46 1.00      899     2411
## 
## Population-Level Effects: 
##           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept     3.74      0.12     3.49     3.98 1.00      828     1663
## neuro_t      -0.24      0.04    -0.32    -0.15 1.00      797     1645
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.43      0.00     0.42     0.44 1.00    16530    13960
## 
## 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.000 0.00000 -2443.145 98.92135 281.99858 33.323019 4886.29 197.8427 Dataset 9 public.csv
modelAp -468.065 65.12891 -2911.210 78.41387 85.88819 2.792582 5822.42 156.8277 Dataset 9 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: 4885) 
##   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.39      0.03     0.33     0.45 1.00     2222     4357
## sd(sigma_Intercept)     0.37      0.03     0.31     0.43 1.00     3857     6200
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           3.75      0.13     3.49     4.00 1.00     1445     2868
## sigma_Intercept    -1.14      0.12    -1.38    -0.91 1.00     2840     5681
## neuro_t            -0.24      0.05    -0.33    -0.15 1.00     1474     3396
## sigma_neuro_t       0.07      0.04    -0.01     0.15 1.00     2724     5920
## 
## 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: 4885) 
##   Draws: 4 chains, each with iter = 5000; warmup = 2000; thin = 1;
##          total post-warmup draws = 12000
## 
## 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:neuro_Q[1.00,2.33))     0.30      0.05     0.23     0.42 1.00     2603     4558
## sd(Intercept:neuro_Q[2.33,3.33))     0.38      0.06     0.29     0.51 1.00     1413     2376
## sd(Intercept:neuro_Q3.33)            0.41      0.09     0.27     0.64 1.00     1731     3680
## sd(Intercept:neuro_Q[3.67,4.67])     0.37      0.07     0.26     0.54 1.00     2456     4663
## sd(sigma_Intercept)                  0.35      0.03     0.30     0.41 1.00     2817     4785
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           3.55      0.11     3.33     3.76 1.00     1253     2581
## sigma_Intercept    -0.99      0.12    -1.22    -0.76 1.00     1874     3799
## neuro_t            -0.21      0.04    -0.29    -0.13 1.00     1094     2379
## sigma_neuro_t       0.03      0.04    -0.05     0.11 1.00     1861     3647
## 
## 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
modelB 0.00000 0.00000 -2443.145 98.92135 281.9986 33.32302 4886.290 197.8427 Dataset 9 public.csv
modelC -16.14791 50.96083 -2459.293 75.52584 233.6872 22.53285 4918.586 151.0517 Dataset 9 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.1191486 0.5912857
mean(data_w$RSD_NA, na.rm = T)
## [1] 0.3122148
sd(data_w$RSD_NA, na.rm = T)
## [1] 0.1090164
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: 85) 
##   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.27      0.11    -1.49    -1.04 1.00     4169     2955
## neuro_t       0.01      0.04    -0.07     0.09 1.00     4215     3167
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.36      0.03     0.31     0.42 1.00     4395     3346
## 
## 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.06820643 0.43999361
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: 85) 
##   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.79      0.12    -2.03    -1.56 1.00     3542     2707
## neuro_t       0.06      0.04    -0.02     0.14 1.00     3458     2933
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.37      0.03     0.32     0.44 1.00     3510     2692
## 
## 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.46
sd(data_w$mean_NA)
## [1] 0.452036
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] 3.065159
sd(data_w$mean_PA)
## [1] 0.4552138
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.40632
sd(data_w$weight_NA)
## [1] 1.1392
range(data_w$weight_NA)
## [1] 0.003547522 4.031001654
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: 85) 
##   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.22      0.11    -1.43    -1.00 1.00     2668     2396
## neuro_t       0.01      0.03    -0.06     0.08 1.00     3059     2720
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.34      0.02     0.30     0.39 1.00     3406     2782
## 
## 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: 85) 
##   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.78      0.06    -1.90    -1.66 1.00     3751     2952
## neuro_t       0.06      0.02     0.01     0.10 1.00     3985     2914
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.36      0.01     0.34     0.39 1.00     3966     2776
## 
## 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.3358798
mean(data_w$sd_PA)
## [1] 0.4077103
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: 85) 
##   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    -2.09      0.24    -2.56    -1.64 1.00     3889     2831
## neuro_t       0.27      0.08     0.10     0.43 1.00     3857     2708
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.76      0.06     0.66     0.89 1.00     4301     2902
## 
## 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: 85) 
##   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.15      0.12    -1.39    -0.91 1.00     3309     2659
## neuro_t       0.07      0.04    -0.01     0.15 1.00     3474     2758
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma     0.38      0.03     0.33     0.44 1.00     3510     3066
## 
## 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: 5616) 
##   Draws: 4 chains, each with iter = 7000; warmup = 2000; thin = 1;
##          total post-warmup draws = 20000
## 
## Group-Level Effects: 
## ~person_id (Number of levels: 97) 
##                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sd(Intercept)           0.50      0.04     0.43     0.58 1.00     1216     2231
## sd(sigma_Intercept)     0.61      0.05     0.53     0.71 1.01     1281     3288
## 
## Population-Level Effects: 
##                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept           1.40      0.05     1.29     1.50 1.01      370      865
## sigma_Intercept    -1.09      0.06    -1.22    -0.96 1.00      659     1367
## 
## 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] 97
nrow(rans_i)
## [1] 97
nrow(data_w)
## [1] 97
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.6180 -0.8061  0.0733  0.7109  2.1397 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.3885     0.5360   4.456 2.62e-05 ***
## Est.SD        0.2390     0.1999   1.196    0.235    
## Est.M         0.4399     0.2685   1.638    0.105    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9447 on 82 degrees of freedom
##   (12 observations deleted due to missingness)
## Multiple R-squared:  0.09573,    Adjusted R-squared:  0.07367 
## F-statistic:  4.34 on 2 and 82 DF,  p-value: 0.01615
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.5357 -0.7157  0.1332  0.6705  2.0233 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   1.8828     0.3301   5.703 1.76e-07 ***
## Est.M         0.6113     0.2276   2.686  0.00874 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9471 on 83 degrees of freedom
##   (12 observations deleted due to missingness)
## Multiple R-squared:  0.07996,    Adjusted R-squared:  0.06888 
## F-statistic: 7.214 on 1 and 83 DF,  p-value: 0.008735
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     83 74.457                           
## 2     82 73.181  1    1.2757 1.4295 0.2353
summary(fit1)$r.squared-summary(fit1.2)$r.squared
## [1] 0.01576371
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"))