# Read
file = "DATE O1A.xlsx"
Date <- readxl::read_xlsx(file, sheet = "date", skip = 5, col_names = FALSE)
# Clean
varnames <- c("Nr_crt", "ID", "Nume_Prenume", "Zi", "Vas_stres_pre", "Vas_bine_pre",
sprintf("Stais_pre_%01d", seq(1,20)),
"SOP",
"IOS_mama", "IOS_tata", "IOS_iubit", "IOS_prieten", "IOS_personalitate",
"Vas_rel_global", "Vas_rel_arousal",
"CRQ_1", "CRQ_2", "CRQ_3", "CRQ_4", "CRQ_5", "CRQ_6",
"Vas_stres_post", "Vas_bine_post",
sprintf("Stais_post_%01d", seq(1,20))
)
names(Date) <- varnames # nume noi
Date <- Date[-c(1:2),] # scoatem randurile cu numele precedente
Date$Nr_crt <- 1:nrow(Date) # era gol, asa ca numerotam randurile ca sa avem acesta variabila
# Process NAs
Date <- Date %>%
mutate(NA_per_row = rowSums(is.na(.))) # count NAs by row
Date <- Date %>%
filter(NA_per_row < 20) # arbitrary cutoff for NAs on columns ... it is normal to have 4 NAs for all columns
# Compute new variables
Conditie <- Date %>%
select(Nr_crt, ID, IOS_mama, IOS_tata, IOS_iubit, IOS_prieten, IOS_personalitate) %>%
gather(type, value, -c(Nr_crt, ID)) %>%
mutate(Conditie = ifelse(!is.na(value), type, NA) ) %>%
mutate(Conditie = str_replace(Conditie, "IOS_", "")) %>%
arrange(Nr_crt) %>%
select(Conditie) %>% na.omit()
Date$Conditie <- Conditie$Conditie # tidyverse returns tibble, must do this
IOS <- Date %>%
mutate(IOS = coalesce(IOS_mama, IOS_tata, IOS_iubit, IOS_prieten, IOS_personalitate)) %>%
select(IOS)
Date$IOS <- IOS$IOS # tidyverse returns tibble, must do this
rm(Conditie, IOS) # remove 2 tibbles
# Scoring Stai (convert numeric - VAS)
itemiVAS <- c(5, 6, 41, 42)
itemiStaiS_pre <- 7:26
itemiStaiS_post <- 43:62
ReversedItems <- c(1,2,5,8,10,11,15,16,19,20)
Date <- Date %>%
replace(Date == "na", NA) %>% # scimbam codarea cu na a Doinei
mutate_at(vars(itemiStaiS_pre), funs(as.numeric)) %>% # facem coloanele numerice pt STAI
mutate_at(vars(itemiStaiS_post), funs(as.numeric)) %>%
mutate_at(vars(itemiVAS), funs(as.numeric))
Date[ ,itemiStaiS_pre[ReversedItems]] = 5 - Date[ ,itemiStaiS_pre[ReversedItems]]
Date[ ,itemiStaiS_post[ReversedItems]] = 5 - Date[ ,itemiStaiS_post[ReversedItems]]
Date$StaiS_pre = rowSums(Date[ ,itemiStaiS_pre], na.rm=T ) * NA ^ (rowSums(!is.na(Date[ ,itemiStaiS_pre])) == 0)
Date$StaiS_post = rowSums(Date[ ,itemiStaiS_post], na.rm=T ) * NA ^ (rowSums(!is.na(Date[ ,itemiStaiS_post])) == 0)
## STAI plot
Staimelt <- Date[, c("ID", "Conditie", "StaiS_pre","StaiS_post")] %>%
gather("StaiS_pre", "StaiS_post", key = "Stai_cond", value = "value") %>%
mutate_at(vars(c(1, 2,3)), funs(as.factor)) %>%
mutate(Stai_cond = factor(Stai_cond, levels = c("StaiS_pre","StaiS_post"))) # %>% # change factor order for plot pre, post
ggplot(Staimelt, aes(x = Stai_cond, y = value)) +
geom_boxplot() +
facet_wrap(~Conditie) +
ggpubr::stat_compare_means(method = "t.test", paired = TRUE, comparisons = list(c("StaiS_pre","StaiS_post")))
## STAI tables
Date %>%
group_by(Conditie) %>%
do(broom::tidy(t.test(.$StaiS_pre,
.$StaiS_post,
mu = 0,
alt = "two.sided",
paired = TRUE,
conf.level = 0.95))) %>%
knitr::kable(digits = 2)
Conditie | estimate | statistic | p.value | parameter | conf.low | conf.high | method | alternative |
---|---|---|---|---|---|---|---|---|
iubit | 0.71 | 0.48 | 0.64 | 27 | -2.34 | 3.77 | Paired t-test | two.sided |
mama | 0.09 | 0.09 | 0.93 | 33 | -1.98 | 2.16 | Paired t-test | two.sided |
personalitate | 1.44 | 1.48 | 0.15 | 35 | -0.53 | 3.42 | Paired t-test | two.sided |
prieten | 3.27 | 3.96 | 0.00 | 36 | 1.59 | 4.95 | Paired t-test | two.sided |
tata | -0.69 | -0.91 | 0.37 | 34 | -2.21 | 0.84 | Paired t-test | two.sided |
stai_table <-
Date %>%
group_by(Conditie) %>%
dplyr::summarize(mean_pre = mean(StaiS_pre, na.rm=TRUE),
mean_post = mean(StaiS_post, na.rm=TRUE),
mean_diff = mean(StaiS_pre, na.rm=TRUE) - mean(StaiS_post, na.rm=TRUE), # m_pre - m_post
sd_pooled = sd(c(StaiS_pre, StaiS_post), na.rm=TRUE), # pooled sd
n_pre = sum(!is.na(StaiS_pre)), # n
n_post = sum(!is.na(StaiS_post))) # n
stai_power <-
stai_table %>%
rowwise() %>%
do(broom::tidy(power.t.test(power = .8,
delta = .$mean_diff,
sd = .$sd_pooled,
sig.level = .05,
type = "paired",
alternative = "two.sided"))) # determine sample size requirement
knitr::kable(cbind(stai_table, stai_power),
digits = 2)
Conditie | mean_pre | mean_post | mean_diff | sd_pooled | n_pre | n_post | n | delta | sd | sig.level | power |
---|---|---|---|---|---|---|---|---|---|---|---|
iubit | 30.82 | 30.11 | 0.71 | 9.74 | 28 | 28 | 1462.52 | 0.71 | 9.74 | 0.05 | 0.8 |
mama | 28.79 | 28.71 | 0.09 | 7.92 | 34 | 34 | 63270.54 | 0.09 | 7.92 | 0.05 | 0.8 |
personalitate | 30.44 | 29.00 | 1.44 | 8.05 | 36 | 36 | 245.79 | 1.44 | 8.05 | 0.05 | 0.8 |
prieten | 32.95 | 29.68 | 3.27 | 9.11 | 37 | 37 | 62.87 | 3.27 | 9.11 | 0.05 | 0.8 |
tata | 29.54 | 30.23 | -0.69 | 7.95 | 35 | 35 | 1056.96 | 0.69 | 7.95 | 0.05 | 0.8 |
## Vas Stres plot
Vasstresmelt <- Date[, c("ID", "Conditie", "Vas_stres_pre","Vas_stres_post")] %>%
gather("Vas_stres_pre","Vas_stres_post", key = "Vas_stres_cond", value = "value") %>%
mutate_at(vars(c(1,2,3)), funs(as.factor)) %>%
mutate_at(vars(c(4)), funs(as.numeric)) %>%
mutate(Vas_stres_cond = factor(Vas_stres_cond, levels = c("Vas_stres_pre","Vas_stres_post"))) # change factor order for plot pre, post
ggplot(Vasstresmelt, aes(x = Vas_stres_cond, y = value)) +
geom_boxplot() +
facet_wrap(~Conditie) +
ggpubr::stat_compare_means(method = "t.test", paired = TRUE, comparisons = list(c("Vas_stres_pre","Vas_stres_post")))
## Vas Stres tables
Date %>%
group_by(Conditie) %>%
do(broom::tidy(t.test(.$Vas_stres_pre,
.$Vas_stres_post,
mu = 0,
alt = "two.sided",
paired = TRUE,
conf.level = 0.95))) %>%
knitr::kable(digits = 2)
Conditie | estimate | statistic | p.value | parameter | conf.low | conf.high | method | alternative |
---|---|---|---|---|---|---|---|---|
iubit | 1.54 | 0.52 | 0.60 | 27 | -4.47 | 7.55 | Paired t-test | two.sided |
mama | -1.53 | -0.62 | 0.54 | 33 | -6.56 | 3.50 | Paired t-test | two.sided |
personalitate | 0.06 | 0.03 | 0.97 | 35 | -3.51 | 3.62 | Paired t-test | two.sided |
prieten | 3.65 | 1.78 | 0.08 | 36 | -0.51 | 7.81 | Paired t-test | two.sided |
tata | -2.83 | -1.43 | 0.16 | 34 | -6.84 | 1.18 | Paired t-test | two.sided |
VasS_table <-
Date %>%
group_by(Conditie) %>%
dplyr::summarize(mean_pre = mean(Vas_stres_pre, na.rm=TRUE),
mean_post = mean(Vas_stres_post, na.rm=TRUE),
mean_diff = mean(Vas_stres_pre, na.rm=TRUE) - mean(Vas_stres_post, na.rm=TRUE), # m_pre - m_post
sd_pooled = sd(c(Vas_stres_pre, Vas_stres_post), na.rm=TRUE), # pooled sd
n_pre = sum(!is.na(Vas_stres_pre)), # n
n_post = sum(!is.na(Vas_stres_post))) # n
VasS_power <-
VasS_table %>%
rowwise() %>%
do(broom::tidy(power.t.test(power = .8,
delta = .$mean_diff,
sd = .$sd_pooled,
sig.level = .05,
type = "paired",
alternative = "two.sided"))) # determine sample size requirement
knitr::kable(cbind(VasS_table, VasS_power),
digits = 2)
Conditie | mean_pre | mean_post | mean_diff | sd_pooled | n_pre | n_post | n | delta | sd | sig.level | power |
---|---|---|---|---|---|---|---|---|---|---|---|
iubit | 18.79 | 17.25 | 1.54 | 20.46 | 28 | 28 | 1395.03 | 1.54 | 20.46 | 0.05 | 0.8 |
mama | 13.59 | 15.12 | -1.53 | 15.89 | 34 | 34 | 848.99 | 1.53 | 15.89 | 0.05 | 0.8 |
personalitate | 17.42 | 17.36 | 0.06 | 14.81 | 36 | 36 | 557933.90 | 0.06 | 14.81 | 0.05 | 0.8 |
prieten | 24.65 | 21.00 | 3.65 | 19.48 | 37 | 37 | 225.70 | 3.65 | 19.48 | 0.05 | 0.8 |
tata | 17.80 | 20.63 | -2.83 | 19.25 | 35 | 35 | 365.58 | 2.83 | 19.25 | 0.05 | 0.8 |
## Vas Stres plot
Vasbinemelt <- Date[, c("ID", "Conditie", "Vas_bine_pre","Vas_bine_post")] %>%
gather("Vas_bine_pre","Vas_bine_post", key = "Vas_stres_cond", value = "value") %>%
mutate_at(vars(c(1,2,3)), funs(as.factor)) %>%
mutate_at(vars(c(4)), funs(as.numeric)) %>%
mutate(Vas_stres_cond = factor(Vas_stres_cond, levels = c("Vas_bine_pre","Vas_bine_post"))) # change factor order for plot pre, post
ggplot(Vasbinemelt, aes(x = Vas_stres_cond, y = value)) +
geom_boxplot() +
facet_wrap(~Conditie) +
ggpubr::stat_compare_means(method = "t.test", paired = TRUE, comparisons = list(c("Vas_bine_pre","Vas_bine_post")))
## Vas Stres tables
Date %>%
group_by(Conditie) %>%
do(broom::tidy(t.test(.$Vas_bine_pre,
.$Vas_bine_post,
mu = 0,
alt = "two.sided",
paired = TRUE,
conf.level = 0.95))) %>%
knitr::kable(digits = 2)
Conditie | estimate | statistic | p.value | parameter | conf.low | conf.high | method | alternative |
---|---|---|---|---|---|---|---|---|
iubit | -2.14 | -0.70 | 0.49 | 27 | -8.40 | 4.11 | Paired t-test | two.sided |
mama | -0.21 | -0.08 | 0.94 | 33 | -5.36 | 4.94 | Paired t-test | two.sided |
personalitate | -1.69 | -0.77 | 0.44 | 35 | -6.14 | 2.76 | Paired t-test | two.sided |
prieten | -5.81 | -3.19 | 0.00 | 36 | -9.51 | -2.12 | Paired t-test | two.sided |
tata | -2.29 | -0.65 | 0.52 | 34 | -9.48 | 4.91 | Paired t-test | two.sided |
VasB_table <-
Date %>%
group_by(Conditie) %>%
dplyr::summarize(mean_pre = mean(Vas_bine_pre, na.rm=TRUE),
mean_post = mean(Vas_bine_post, na.rm=TRUE),
mean_diff = mean(Vas_bine_pre, na.rm=TRUE) - mean(Vas_bine_post, na.rm=TRUE), # m_pre - m_post
sd_pooled = sd(c(Vas_bine_pre, Vas_bine_post), na.rm=TRUE), # pooled sd
n_pre = sum(!is.na(Vas_bine_pre)), # n
n_post = sum(!is.na(Vas_bine_post))) # n
VasB_power <-
VasB_table %>%
rowwise() %>%
do(broom::tidy(power.t.test(power = .8,
delta = .$mean_diff,
sd = .$sd_pooled,
sig.level = .05,
type = "paired",
alternative = "two.sided"))) # determine sample size requirement
knitr::kable(cbind(VasB_table, VasB_power),
digits = 2)
Conditie | mean_pre | mean_post | mean_diff | sd_pooled | n_pre | n_post | n | delta | sd | sig.level | power |
---|---|---|---|---|---|---|---|---|---|---|---|
iubit | 75.14 | 77.29 | -2.14 | 21.37 | 28 | 28 | 782.41 | 2.14 | 21.37 | 0.05 | 0.8 |
mama | 78.44 | 78.65 | -0.21 | 17.01 | 34 | 34 | 53603.98 | 0.21 | 17.01 | 0.05 | 0.8 |
personalitate | 74.00 | 75.69 | -1.69 | 20.20 | 36 | 36 | 1117.79 | 1.69 | 20.20 | 0.05 | 0.8 |
prieten | 68.86 | 74.68 | -5.81 | 17.28 | 37 | 37 | 71.37 | 5.81 | 17.28 | 0.05 | 0.8 |
tata | 71.14 | 73.43 | -2.29 | 19.14 | 35 | 35 | 552.39 | 2.29 | 19.14 | 0.05 | 0.8 |
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=Romanian_Romania.1250 LC_CTYPE=Romanian_Romania.1250 LC_MONETARY=Romanian_Romania.1250
[4] LC_NUMERIC=C LC_TIME=Romanian_Romania.1250
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] bindrcpp_0.2.2 ggpubr_0.2 magrittr_1.5 broom_0.5.1 papaja_0.1.0.9842 psych_1.8.10 forcats_0.3.0
[8] stringr_1.3.1 dplyr_0.7.8 purrr_0.2.5 readr_1.3.0 tidyr_0.8.2 tibble_1.4.2 ggplot2_3.1.0
[15] tidyverse_1.2.1 pacman_0.5.0
loaded via a namespace (and not attached):
[1] tidyselect_0.2.5 xfun_0.4 haven_2.1.0 lattice_0.20-38 colorspace_1.3-2 generics_0.0.2 htmltools_0.3.6
[8] yaml_2.2.0 rlang_0.3.1 pillar_1.3.1 foreign_0.8-71 glue_1.3.0 withr_2.1.2 modelr_0.1.2
[15] readxl_1.1.0 bindr_0.1.1 plyr_1.8.4 ggsignif_0.4.0 munsell_0.5.0 gtable_0.2.0 cellranger_1.1.0
[22] rvest_0.3.2 evaluate_0.12 labeling_0.3 knitr_1.21 parallel_3.5.2 highr_0.7 Rcpp_1.0.0
[29] scales_1.0.0 backports_1.1.3 jsonlite_1.6 mnormt_1.5-5 digest_0.6.18 hms_0.4.2 stringi_1.2.4
[36] grid_3.5.2 cli_1.0.1 tools_3.5.2 lazyeval_0.2.1 crayon_1.3.4 pkgconfig_2.0.2 xml2_1.2.0
[43] lubridate_1.7.4 rmarkdown_1.11 assertthat_0.2.0 httr_1.4.0 rstudioapi_0.8 R6_2.3.0 nlme_3.1-137
[50] compiler_3.5.2
A work by Claudiu Papasteri