Psychonomics Bulletin and Review - Supplementary Materials - R Code & Results
2021-04-15
Introduction
This document shares the code underlying the results reported in Trémolière & Gosling (2021). Raw data are available in supplementary materials. Any question about this supplementary materials should be sent at bastien.tremoliere@unimes.fr.
To reproduce the described analyses you should have:
1. These packages installed and loaded
install.packages(c("knitr", "geepack" ,"gt", "ggplot2", "tidyr", "dplyr", "car", "lavaan", "metafor", "lme4", "lmerTest", "stringr", "ggplot2", "DescTools", "broom"))
library(knitr);library(geepack); library(gt); library(ggplot2); library(tidyr); library(dplyr);library(car);library(lavaan); library(metafor); library(kableExtra); library(gt);library(lme4);library(lmerTest); library(DescTools);library(stringr); library(broom); library(Hmisc)
2. The datasets of the individual studies loaded
If you want to reproduce our analyses, load the raw data provided in supplementary materials using this code.
<- read.delim(file.choose())
Data_Study1_sleep_moral <- read.delim(file.choose())
Data_Study2_sleep_moral <- read.delim(file.choose())
Data_Study3_sleep_moral <- read.delim(file.choose())
Data_Study4_sleep_moral <- read.delim(file.choose())
Data_Study5_sleep_moral <- read.delim(file.choose()) Data_Study6_sleep_moral
Scoring and combining the datasets from primary studies
<- Data_Study1_sleep_moral %>%
Data_Study1_Raw ::mutate(
dplyr#Check for missing Data
CCA = case_when(
is.na(Moral_DIL) | is.na(SleepQualCro) | is.na(SleepQualAcu) | is.na(SleepQuantCro) |
is.na(SleepQuantAcu) ~ 0,
!is.na(Moral_DIL) & !is.na(SleepQualCro) & !is.na(SleepQualAcu) & !is.na(SleepQuantCro) &
!is.na(SleepQuantAcu) ~ 1),
#Detect participants failing at attention check
Attention_check=case_when(
== 2 ~ 1,
Tromp != 2 ~ 0),
Tromp SleepEfficiency.criteria = 1)
<- Data_Study2_sleep_moral %>%
Data_Study2_Raw mutate(
#Check for missing Data
CCA = case_when(
is.na(Moral_SCA) | is.na(SleepQualCro) | is.na(SleepQualAcu) | is.na(SleepQuantCro) |
is.na(SleepQuantAcu) ~ 0,
!is.na(Moral_SCA) & !is.na(SleepQualCro) & !is.na(SleepQualAcu) & !is.na(SleepQuantCro) &
!is.na(SleepQuantAcu) ~ 1),
#Detect participants failing at attention check
Attention_check = case_when(
== 2 ~ 1,
Tromp != 2 ~ 0),
Tromp SleepEfficiency.criteria = 1)
<- Data_Study3_sleep_moral %>%
Data_Study3_Raw mutate(
#Check for missing Data
CCA = case_when(
is.na(Moral_SCA) | is.na(SleepQualCro) | is.na(SleepQualAcu) | is.na(SleepQuantCro) |
is.na(SleepQuantAcu) ~ 0,
!is.na(Moral_SCA) & !is.na(SleepQualCro) & !is.na(SleepQualAcu) & !is.na(SleepQuantCro) &
!is.na(SleepQuantAcu) ~ 1),
#Attention check and sleep efficiency criterion set to 1 since there are none in the task
Attention_check = 1,
SleepEfficiency.criteria = 1)
<- Data_Study4_sleep_moral %>%
Data_Study4_Raw mutate(
#Check for missing Data
CCA = case_when(
is.na(Moral_DIL) |is.na(Moral_SCA) | is.na(Moral_CAR) | is.na(SleepQualCro) | is.na(SleepQualAcu) | is.na(SleepQuantCro) | is.na(SleepQuantAcu) ~0,
!is.na(Moral_DIL) & !is.na(Moral_SCA) & !is.na(Moral_CAR) & !is.na(SleepQualCro)
& !is.na(SleepQualAcu) & !is.na(SleepQuantCro) & !is.na(SleepQuantAcu) ~1),
#Detect participants failing at attention check
Attention_check=case_when(
== 0 ~ 1,
Tromp == 1 ~ 0))
Tromp
<- Data_Study5_sleep_moral %>%
Data_Study5_Raw mutate(
#Check for missing Data
CCA = case_when(
is.na(Moral_DIL) |is.na(Moral_SCA) | is.na(SleepQualCro) | is.na(SleepQualAcu)
| is.na(SleepQuantCro) | is.na(SleepQuantAcu) ~0,
!is.na(Moral_DIL) & !is.na(Moral_SCA) & !is.na(SleepQualCro) &
!is.na(SleepQualAcu) & !is.na(SleepQuantCro) & !is.na(SleepQuantAcu) ~1),
#Attention check set to 1 since there are none in the task
Attention_check = 1)
<- Data_Study6_sleep_moral %>%
Data_Study6_Raw mutate(
#Check for missing Data
CCA=case_when(
is.na(Moral_DIL) |is.na(Moral_SCA) | is.na(SleepQualCro) | is.na(SleepQualAcu)
| is.na(SleepQuantCro) | is.na(SleepQuantAcu) ~0,
!is.na(Moral_DIL) & !is.na(Moral_SCA) & !is.na(SleepQualCro) &
!is.na(SleepQualAcu) & !is.na(SleepQuantCro) & !is.na(SleepQuantAcu) ~1),
#Detect participants failing at attention check
Attention_check=case_when(
== 1 ~ 1,
Tromp != 1 ~ 0))
Tromp
# For each dataset, we select only the required columns, we create an outcome column describing the type of outcome used in the study, we create unique participant identifiers, and we set the minimal duration under which the task could not be correctly performed
<- Data_Study1_Raw %>%
Data_MetaS1 ::select(ResponseId, Moral_DIL, SleepQualCro, SleepQualAcu, SleepQuantCro, SleepQuantAcu,
dplyr
Age_correct, Attention_check, CCA, Duration, SleepEfficiency.criteria, %>%
CRT_TOT, Num_TOT, AOT_TOT, Psych_TOT, Narc_TOT, Mach_TOT) ::mutate(
dplyrOutcome="Dilemmas", Study="1",
ID=paste0(1:nrow(Data_Study1_Raw), "_S1"),
ResponseId=as.character(ResponseId),
Duration.min=200
)
<- Data_Study2_Raw %>%
Data_MetaS2 ::select(ResponseId,Moral_SCA, SleepQualCro, SleepQualAcu, SleepQuantCro, SleepQuantAcu,
dplyr
Age_correct, Attention_check, CCA, Duration, SleepEfficiency.criteria,%>%
CRT_TOT, Num_TOT, AOT_TOT, Psych_TOT, Narc_TOT, Mach_TOT)::mutate(
dplyrOutcome = "Scale",
Study = "2",
ID = paste0(1:nrow(Data_Study2_Raw), "_S2"),
ResponseId=as.character(ResponseId),
Duration.min = 150
)
<- Data_Study3_Raw %>%
Data_MetaS3 ::select(ResponseId,Moral_SCA, SleepQualCro, SleepQualAcu, SleepQuantCro, SleepQuantAcu,
dplyr
Age_correct, CCA, Duration, Attention_check, SleepEfficiency.criteria,%>%
CRT_TOT, Num_TOT, AOT_TOT, Psych_TOT, Narc_TOT, Mach_TOT)::mutate(
dplyrOutcome = "Scale", Study = "3",
ID = paste0(1:nrow(Data_Study3_Raw), "_S3"),
ResponseId=as.character(ResponseId),
Duration.min = 150
)
<- Data_Study4_Raw %>%
Data_MetaS4.wide ::select(ResponseId,Moral_DIL, Moral_SCA, Moral_CAR, SleepQualCro, SleepQualAcu, SleepQuantCro, SleepQuantAcu, Age_correct, Attention_check, CCA, Duration, SleepEfficiency.criteria)%>%
dplyr::mutate(
dplyrStudy = "4",
ID = paste0(1:nrow(Data_Study4_Raw), "_S4"),
ResponseId=as.character(ResponseId),
Duration.min = 200
)
<- Data_MetaS4.wide %>%
Data_MetaS4 pivot_longer(cols=c("Moral_DIL", "Moral_SCA", "Moral_CAR"), values_to="Moral_TOT",
names_to="Outcome_transit") %>%
::mutate(Outcome=case_when(
dplyr=="Moral_DIL"~"Dilemmas",
Outcome_transit=="Moral_SCA"~"Scale",
Outcome_transit=="Moral_CAR"~"Autonomouscars"))
Outcome_transit
<-Data_Study5_Raw %>%
Data_MetaS5.wide::select(ResponseId,Moral_DIL, Moral_SCA, SleepQualCro, SleepQualAcu, SleepQuantCro, SleepQuantAcu,Age_correct, CCA, Duration, Attention_check, SleepEfficiency.criteria) %>%
dplyr::mutate(Study="5",
dplyrID=paste0(1:nrow(Data_Study5_Raw), "_S5"),
ResponseId=as.character(ResponseId),
Duration.min=150,
)
<- Data_MetaS5.wide %>%
Data_MetaS5pivot_longer(cols=c("Moral_DIL", "Moral_SCA"), values_to="Moral_TOT", names_to="Outcome_transit") %>%
::mutate(Outcome=case_when(
dplyr=="Moral_DIL"~"Dilemmas",
Outcome_transit=="Moral_SCA"~"Scale"))
Outcome_transit
<-Data_Study6_Raw %>%
Data_MetaS6.wide::select(ResponseId,Moral_DIL, Moral_SCA, SleepQualCro, SleepQualAcu, SleepQuantCro, SleepQuantAcu, Age_correct, CCA,Attention_check, Duration, SleepEfficiency.criteria) %>%
dplyr::mutate(Study="6",
dplyrID=paste0(1:nrow(Data_Study6_Raw), "_S6"),
ResponseId=as.character(ResponseId),
Duration.min=150
)
<- Data_MetaS6.wide %>%
Data_MetaS6pivot_longer(cols=c("Moral_DIL", "Moral_SCA"), values_to="Moral_TOT", names_to="Outcome_transit") %>%
::mutate(Outcome=case_when(
dplyr=="Moral_DIL"~"Dilemmas",
Outcome_transit=="Moral_SCA"~"Scale")) Outcome_transit
List of the combined datasets
Wide dataset: 1 row per participant
# we create 3 datasets with one row per participant
# one dataset with all participants
<- bind_rows(
Data_Meta_Raw_transit.wide
Data_MetaS1, Data_MetaS2, Data_MetaS3, Data_MetaS4.wide, Data_MetaS5.wide, Data_MetaS6.wide)
$Exclusion <- case_when(
Data_Meta_Raw_transit.wide$CCA == 1 & Data_Meta_Raw_transit.wide$SleepEfficiency.criteria == 1 &
Data_Meta_Raw_transit.wide$SleepQuantCro >= 2 & Data_Meta_Raw_transit.wide$SleepQuantCro <= 16 &
Data_Meta_Raw_transit.wide$SleepQuantAcu >= 0 & Data_Meta_Raw_transit.wide$Attention_check == 1 ~ 0,
Data_Meta_Raw_transit.wide$CCA != 1 | Data_Meta_Raw_transit.wide$SleepEfficiency.criteria == 1 |
Data_Meta_Raw_transit.wide$SleepQuantCro < 2 | Data_Meta_Raw_transit.wide$SleepQuantCro > 16 |
Data_Meta_Raw_transit.wide$SleepQuantAcu < 0 | Data_Meta_Raw_transit.wide$Attention_check != 1 ~ 1)
Data_Meta_Raw_transit.wide
# one dataset with participants who have no missing values
<- filter(Data_Meta_Raw_transit.wide,CCA==1)
Data_Meta_Raw.wide_CCA # one dataset with participants who have who have no missing values and correct sleep efficiency ratio
<- filter(Data_Meta_Raw.wide_CCA, SleepEfficiency.criteria ==1)
Data_Meta_Raw.wide_CCA_excl1 # one dataset with participants who have who have no missing values, correct sleep efficiency ratio and credible sleep quantity
<- filter(Data_Meta_Raw.wide_CCA_excl1,
Data_Meta_Raw.wide_CCA_excl2 >= 2 & SleepQuantCro <= 16 &
SleepQuantCro >= 0)
SleepQuantAcu # one dataset with participants who have who have no missing values, correct sleep efficiency ratio, credible sleep quantity and correct attentional check
<-filter(Data_Meta_Raw.wide_CCA_excl2, Attention_check==1) Data_Meta_Raw.wide
Semi-long dataset: 1 row per outcome per participant
# we create a dataset with one row per outcome per participant
# For example, in a study with 3 outcomes, a participants will have 3 rows
<- bind_rows(
Data_Meta_Raw_transit1.Slong
Data_MetaS1, Data_MetaS2, Data_MetaS3, Data_MetaS4, Data_MetaS5, Data_MetaS6)
<-Data_Meta_Raw_transit1.Slong %>%
Data_Meta_Raw_transit.Slong rowwise() %>%
mutate(
Study.out.delim=paste0(Study,"_", Outcome),
Moral_tot=sum(Moral_DIL, Moral_SCA, Moral_TOT, na.rm=T))
<- filter(Data_Meta_Raw_transit.Slong,
Data_Meta_Raw.Slong ==1 & CCA==1 &
Attention_check==1 &
SleepEfficiency.criteria >= 2 & SleepQuantCro <= 16 & SleepQuantAcu >= 0) SleepQuantCro
Long dataset: 1 row per outcome and per sleep indicator per participant
# we create a dataset with one row per outcome and per sleep indicator per participant
# For example, in a study with 3 outcomes, a participants will have 3*4 = 12 rows since 4 sleep indicators are included in every studies
<- Data_Meta_Raw.Slong %>%
Data_Meta_Raw.long pivot_longer(cols=starts_with("SleepQu"), values_to="Sleep", names_to="SleepType")
Demographic characteristics of the sample
# we work on raw datasets regardless the missing values or the correctness of the attention check
<- list(
list_data_demographics
Data_Study1_Raw, Data_Study2_Raw, Data_Study3_Raw, Data_Study4_Raw, Data_Study5_Raw, Data_Study6_Raw
)
# we create a function extracting the total number of participants, the number of participants with no missing value and the participants with a correct attentional check in each study. We also extracted the percentage of female.
<-function(x){
DEMOFUNCTION= nrow(x)
N.TOT = nrow(x) - nrow(subset(x, CCA==1))
N.CCA = nrow(subset(x, CCA==1)) - nrow(subset(x, CCA == 1 & SleepEfficiency.criteria == 1))
N.excl.eff = nrow(subset(x, CCA == 1 & SleepEfficiency.criteria == 1)) - nrow(subset(x, CCA == 1 & SleepEfficiency.criteria == 1 & SleepQuantCro >= 2 & SleepQuantCro <= 16 & SleepQuantAcu > 0))
N.excl.quant = nrow(subset(x, CCA == 1 & SleepEfficiency.criteria == 1 & SleepQuantCro >= 2 & SleepQuantCro <= 16 & SleepQuantAcu > 0)) - nrow(subset(x, CCA == 1 & SleepEfficiency.criteria == 1 &
N.attcheck >= 2 & SleepQuantCro <= 16 & SleepQuantAcu > 0 &
SleepQuantCro == 1))
Attention_check = nrow(subset(x, CCA == 1 & SleepEfficiency.criteria == 1 &
N.Analysis >= 2 & SleepQuantCro <= 16 & SleepQuantAcu > 0 &
SleepQuantCro == 1))
Attention_check = 1-nrow(subset(x, Gender == 1))/nrow(x)
Perc.Female cbind(N.TOT, N.CCA, N.excl.eff, N.excl.quant, N.attcheck, N.Analysis, Perc.Female)
}
<-data.frame(do.call(rbind, lapply(list_data_demographics, DEMOFUNCTION)))
data_demographics
<- data.frame(
Data_presentation Study=1:6,
data_demographics)
colnames(Data_presentation)<-c(
"Study",
"Total number of participants",
"Number of participants with missing values",
"Number of participants with an incorrect sleep efficiency ratio",
"Number of participants with a uncredible sleep quantity",
"Number of participants with an incorrect attentional check",
"Number of participants retained in final analysis",
"% of female")
gt(Data_presentation)
Study | Total number of participants | Number of participants with missing values | Number of participants with an incorrect sleep efficiency ratio | Number of participants with a uncredible sleep quantity | Number of participants with an incorrect attentional check | Number of participants retained in final analysis | % of female |
---|---|---|---|---|---|---|---|
1 | 124 | 0 | 0 | 0 | 2 | 122 | 0.4032258 |
2 | 115 | 0 | 0 | 1 | 9 | 105 | 0.4086957 |
3 | 54 | 1 | 0 | 1 | 0 | 52 | 0.8703704 |
4 | 123 | 6 | 5 | 1 | 12 | 99 | 0.3658537 |
5 | 121 | 16 | 0 | 0 | 0 | 105 | 0.7107438 |
6 | 105 | 2 | 0 | 0 | 4 | 99 | 0.7523810 |
Mean values and SD for each of the independent and dependent variable across studies
<- function(x){
MeanFUNCTION cbind(
m.dil=paste0(round(mean(x$Moral_DIL),digits=2), " (", round(sd(x$Moral_DIL),digits=2), ")"),
m.sca=paste0(round(mean(x$Moral_SCA),digits=2)," (",round(sd(x$Moral_SCA),digits=2), ")"),
m.car=paste0(round(mean(x$Moral_CAR),digits=2)," (", round(sd(x$Moral_CAR),digits=2), ")"),
m.sleepquantcro=paste0(round(mean(x$SleepQuantCro),digits=2)," (", round(sd(x$SleepQuantCro),digits=2), ")"),
m.sleepquantacu=paste0(round(mean(x$SleepQuantAcu),digits=2)," (", round(sd(x$SleepQuantAcu),digits=2), ")"),
m.sleepqualcro=paste0(round(mean(x$SleepQualCro),digits=2)," (", round(sd(x$SleepQualCro),digits=2),")"),
m.sleepqualacu=paste0(round(mean(x$SleepQualAcu),digits=2)," (", round(sd(x$SleepQualAcu),digits=2), ")")
)
}<- function(x){
MeanFUNCTION2 cbind(
A=min(x$SleepQuantAcu),
B=min(x$SleepQualCro),
C=min(x$SleepQualAcu)
)
}
<- data.frame(
data_mean cbind(
Study = 1:6,
do.call(rbind, lapply(split(Data_Meta_Raw.wide, Data_Meta_Raw.wide$Study),
MeanFUNCTION))))colnames(data_mean)<-
c(
"Study",
"Mean score (SD) at sacrificial dilemmas",
"Mean score (SD) at moral scale",
"Mean score (SD) at autonomours car dilemmas",
"Mean quantity (SD) of chronic sleep (hours)",
"Mean quantity (SD) of acute sleep (hours)",
"Mean quality (SD) of chronic sleep",
"Mean quality (SD) of acute sleep")
gt(data_mean)
Study | Mean score (SD) at sacrificial dilemmas | Mean score (SD) at moral scale | Mean score (SD) at autonomours car dilemmas | Mean quantity (SD) of chronic sleep (hours) | Mean quantity (SD) of acute sleep (hours) | Mean quality (SD) of chronic sleep | Mean quality (SD) of acute sleep |
---|---|---|---|---|---|---|---|
1 | 49.81 (29.6) | NA (NA) | NA (NA) | 6.68 (1.32) | 6.52 (1.46) | 6.19 (2.24) | 6.28 (2.52) |
2 | NA (NA) | 34.1 (4.4) | NA (NA) | 7.02 (0.98) | 6.89 (1.27) | 6.42 (2.16) | 6.84 (2.18) |
3 | NA (NA) | 37.08 (3.98) | NA (NA) | 7.13 (1.48) | 7.29 (1.68) | 5.23 (2.06) | 5.63 (2.28) |
4 | 58.51 (25.94) | 33.55 (4.11) | 0.82 (0.19) | 7.19 (1.09) | 7 (1.32) | 14.29 (3.69) | 6.77 (2.19) |
5 | 43.05 (23.07) | 36.75 (4.38) | NA (NA) | 8.22 (1.23) | 8.21 (1.95) | 15.78 (2.84) | 17.26 (2.24) |
6 | 34.18 (24.03) | 34.81 (3.36) | NA (NA) | 7.89 (1.05) | 7.77 (1.25) | 15.89 (2.67) | 17.33 (1.83) |