#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Read and Merge
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wd <- "C:/Users/Mihai/Desktop/O.4 prealabil pt Frontiers/O.4 Scale Scoring/scorate 21.08.2019"
setwd(wd)
## Read
Data_arsq <- readRDS("Data_arsq.RDS")
Data_nycq <- readRDS("Data_nycq.RDS")
Data_vas <- xlsx::read.xlsx2("VAS, IOS.xlsx", sheetName = "Toti")
## Transform
Data_arsq <-
Data_arsq %>%
select(-"Data.x", -"Data.y") %>%
na_if("NA") %>% # make NA chars NA so to not get warning message of NAs introduced by coercion
mutate_at(vars(- c(1:5)), funs(as.numeric(as.character(.))))
Data_nycq <-
Data_nycq %>%
select(-"Data.x", -"Data.y") %>%
na_if("NA") %>% # make NA chars NA so to not get warning message of NAs introduced by coercion
mutate_at(vars(- c(1:5)), funs(as.numeric(as.character(.))))
## To numeric
Data_vas <-
Data_vas %>%
mutate_at(vars(6:11), function(x) as.numeric(as.character(x))) %>%
filter(ID %in% c(2:8, 10:14, 16:33))
## Calc Diff Scores
Data_vas$VaS_Diff <- Data_vas$VaS_post - Data_vas$VaS_pre
Data_vas$VaB_Diff <- Data_vas$VaB_post - Data_vas$VaB_pre
Data_vas$IOS_Diff <- Data_vas$IOS_post - Data_vas$IOS_pre
# Fix a typo
Data_arsq[(Data_arsq$ID == 32 & Data_arsq$Order == 2) ,]$Conditie <- "ECRAN"
Data_nycq[(Data_nycq$ID == 32 & Data_nycq$Order == 2) ,]$Conditie <- "ECRAN"
Data_vas[(Data_vas$ID == 32 & Data_vas$Data == "18.07.2019") ,]$Conditie <- "ECRAN"
## Func t test si boxplot simplu
func_t_box <- function(df, ind, cond, pre_var, post_var){
df_modif <-
df %>%
filter(Conditie == cond) %>%
select(ind, pre_var, post_var) %>%
tidyr::drop_na() %>%
gather(pre_var, post_var, key = "PrePost", value = "value") %>%
mutate_at(vars(c(1, 2)), funs(as.factor)) %>%
mutate(PrePost = factor(PrePost, levels = c(pre_var, post_var)))
stat_comp <- ggpubr::compare_means(value ~ PrePost, data = df_modif, method = "t.test", paired = TRUE)
#sample_size <- sum(duplicated(df_modif[, ind])) # get nr of duplicates = sample size for paired test
sample_size <-
df_modif %>%
filter(duplicated(.[["ID"]])) %>% # something is fishy: OGL 31, ECRAN 29 observations
dplyr::summarize(n = n())
stat_comp2 <-
df_modif %>%
do(tidy(t.test(.$value ~ .$PrePost,
paired = TRUE,
data=.)))
plot <-
ggpubr::ggpaired(df_modif, x = "PrePost", y = "value", id = ind, title = cond,
color = "PrePost", line.color = "gray", line.size = 0.4,
palette = c("#00AFBB", "#FC4E07"), legend = "none") +
stat_summary(fun.data = mean_se, colour = "darkred") +
ggpubr::stat_compare_means(method = "t.test", paired = TRUE, label.x = as.numeric(df_modif$PrePost)-0.4, label.y = max(df_modif$value)+1) +
ggpubr::stat_compare_means(method = "t.test", paired = TRUE, label = "p.signif", comparisons = list(c(pre_var, post_var)))
cat(paste0("#### ", cond, " - ", pre_var, " ", post_var, " (N = ", sample_size$n, ")", "\n", "\n"))
print(stat_comp)
print(stat_comp2)
cat("\n")
print(plot)
cat("\n")
plot.new() # Need this workaround for interleaving tables and plots in R Markdown, within loop
dev.off()
}
## Simple before-after analyses with t test
# func_t_box(Data_arsq, "ID", "OGL", "discont_pre", "discont_post") # sig
# func_t_box(Data_arsq, "ID", "ECRAN", "discont_pre", "discont_post") # nonsig
cat("#### VAS Stress")
func_t_box(Data_vas, "ID", "OGL", "VaS_pre", "VaS_post")
NANA
null device 1
func_t_box(Data_vas, "ID", "ECRAN", "VaS_pre", "VaS_post")
NANA
null device 1
cat("#### VAS Well-being")
func_t_box(Data_vas, "ID", "OGL", "VaB_pre", "VaB_post")
NANA
null device 1
func_t_box(Data_vas, "ID", "ECRAN", "VaB_pre", "VaB_post")
NANA
null device 1
cat("#### IOS")
func_t_box(Data_vas, "ID", "OGL", "IOS_pre", "IOS_post")
NANA
null device 1
func_t_box(Data_vas, "ID", "ECRAN", "IOS_pre", "IOS_post")
NANA
null device 1
cat("#### ARSQ")
var_names <- colnames(Data_arsq)[c(6:15, 41:50)] # dont add individual items
var_names_pre <- grep("_pre", var_names, value = TRUE)
var_names_post <- grep("_post", var_names, value = TRUE)
var_names_length <- length(var_names_pre)
var_cond <- cbind(rep("OGL", var_names_length), rep("ECRAN", var_names_length))
for (i in seq_along(1:var_names_length)){
func_t_box(Data_arsq, "ID", var_cond[i,1], var_names_pre[i], var_names_post[i]) # "OGL"
func_t_box(Data_arsq, "ID", var_cond[i,2] , var_names_pre[i], var_names_post[i]) # "ECRAN"
}
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
cat("#### NYCQ")
var_names <- colnames(Data_nycq)[-c(1:5)]
var_names_pre <- grep("_pre", var_names, value = TRUE)
var_names_post <- grep("_post", var_names, value = TRUE)
var_names_length <- length(var_names_pre)
var_cond <- cbind(rep("OGL", var_names_length), rep("ECRAN", var_names_length))
for (i in seq_along(1:var_names_length)){
func_t_box(Data_nycq, "ID", var_cond[i,1], var_names_pre[i], var_names_post[i]) # "OGL"
func_t_box(Data_nycq, "ID", var_cond[i,2] , var_names_pre[i], var_names_post[i]) # "ECRAN"
}
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NANA
NA
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 LC_NUMERIC=C
[5] LC_TIME=Romanian_Romania.1250
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] bindrcpp_0.2.2 rio_0.5.16 plyr_1.8.4 summarytools_0.9.3 DT_0.5 ggpubr_0.2 magrittr_1.5
[8] broom_0.5.1 papaja_0.1.0.9842 psych_1.8.10 forcats_0.3.0 stringr_1.3.1 dplyr_0.7.8 purrr_0.2.5
[15] readr_1.3.0 tidyr_0.8.2 tibble_1.4.2 ggplot2_3.2.0 tidyverse_1.2.1 pacman_0.5.1
loaded via a namespace (and not attached):
[1] httr_1.4.0 jsonlite_1.6 modelr_0.1.2 assertthat_0.2.1 pander_0.6.3 xlsxjars_0.6.1 cellranger_1.1.0
[8] yaml_2.2.0 pillar_1.3.1 backports_1.1.3 lattice_0.20-38 glue_1.3.1 digest_0.6.18 pryr_0.1.4
[15] ggsignif_0.4.0 checkmate_1.8.5 rvest_0.3.2 colorspace_1.3-2 htmltools_0.3.6 pkgconfig_2.0.2 haven_2.1.1
[22] magick_2.0 scales_1.0.0 openxlsx_4.1.0 generics_0.0.2 withr_2.1.2 lazyeval_0.2.1 cli_1.0.1
[29] mnormt_1.5-5 crayon_1.3.4 readxl_1.1.0 nlme_3.1-137 xml2_1.2.0 foreign_0.8-71 rapportools_1.0
[36] tools_3.5.2 data.table_1.12.2 hms_0.4.2 matrixStats_0.54.0 xlsx_0.6.1 munsell_0.5.0 zip_1.0.0
[43] compiler_3.5.2 rlang_0.4.0 grid_3.5.2 RCurl_1.95-4.11 rstudioapi_0.8 htmlwidgets_1.3 labeling_0.3
[50] bitops_1.0-6 tcltk_3.5.2 gtable_0.2.0 codetools_0.2-15 curl_4.0 R6_2.4.0 lubridate_1.7.4
[57] knitr_1.24 bindr_0.1.1 rJava_0.9-10 stringi_1.2.4 parallel_3.5.2 Rcpp_1.0.2 tidyselect_0.2.5
[64] xfun_0.8
A work by Claudiu Papasteri