## Define function that recodes to numeric, but watches out to coercion to not introduce NAs
colstonumeric <- function(df){
tryCatch({
df_num <- as.data.frame(
lapply(df,
function(x) { as.numeric(as.character(x))}))
},warning = function(stop_on_warning) {
message("Stoped the execution of numeric conversion: ", conditionMessage(stop_on_warning))
})
}
##
## Define function that reverse codes items
ReverseCode <- function(df, tonumeric = FALSE, min = NULL, max = NULL) {
if(tonumeric) df <- colstonumeric(df)
df <- (max + min) - df
}
##
## Define function that scores only rows with less than 10% NAs (returns NA if all or above threshold percentage of rows are NA); can reverse code if vector of column indexes and min, max are provided.
ScoreLikert <- function(df, napercent = .1, tonumeric = FALSE, reversecols = NULL, min = NULL, max = NULL) {
reverse_list <- list(reversecols = reversecols, min = min, max = max)
reverse_check <- !sapply(reverse_list, is.null)
# Recode to numeric, but watch out to coercion to not introduce NAs
colstonumeric <- function(df){
tryCatch({
df_num <- as.data.frame(
lapply(df,
function(x) { as.numeric(as.character(x))}))
},warning = function(stop_on_warning) {
message("Stoped the execution of numeric conversion: ", conditionMessage(stop_on_warning))
})
}
if(tonumeric) df <- colstonumeric(df)
if(all(reverse_check)){
df[ ,reversecols] <- (max + min) - df[ ,reversecols]
}else if(any(reverse_check)){
stop("Insuficient info for reversing. Please provide: ", paste(names(reverse_list)[!reverse_check], collapse = ", "))
}
ifelse(rowSums(is.na(df)) > ncol(df) * napercent,
NA,
rowSums(df, na.rm = TRUE) * NA ^ (rowSums(!is.na(df)) == 0)
)
}
##
data_incadrari <- rio::import(file.path(folder, file_incadrari), skip = 0)
data_incadrari <-
data_incadrari %>%
mutate(ID = toupper(ID),
ID = stringr::str_replace_all(ID, "[[:blank:]]", ""),
ID = stringr::str_remove(ID, "ID"),
ID = stringr::str_remove(ID, "^0")) %>%
mutate(Email = tolower(Email))
# Read data
data <- rio::import(file.path(folder, file),
skip = 0, which = "APS")
data <- data[-c(1:33), ] # data begins with row 34, until 34 its a pilot study
# Rename
names(data)[13:28] <- sprintf("APS_%d", 1:16)
names(data)[29] <- "ID"
data <-
data %>%
dplyr::rename_all(~stringr::str_replace_all(., fixed(" "), "_")) %>%
dplyr:: mutate(ID = toupper(ID),
ID = stringr::str_replace_all(ID, "[[:blank:]]", ""),
ID = stringr::str_replace_all(ID, "@.*", ""),
ID = stringr::str_remove(ID, "ID"),
ID = stringr::str_remove(ID, "^0")) %>%
dplyr::filter(stringr::str_detect(ID, "A10"))
data <- dplyr::left_join(data, data_incadrari, by = "ID")
# Cherck - should be 3 for 21 subjs, but some will have 2 (Pre + Post, no Followup)
data %>%
count(ID)
# Add PrePost Column
data <-
data %>%
dplyr::group_by(ID) %>% # can do arrange on Dates column if rows are not in order, but here they are
dplyr::mutate(numbering = row_number()) %>%
dplyr::mutate(PrePost = dplyr::case_when(numbering == 1 ~ "Pre",
numbering == 2 ~ "Post",
numbering == 3 ~ "Followup",
TRUE ~ "Other")) %>%
dplyr::mutate(PrePost = factor(PrePost, levels = c("Pre", "Post", "Followup"))) %>%
dplyr::ungroup() %>%
dplyr::mutate(Conditie = factor(Conditie, levels = c("experimental", "ctrl")))
p_aps_1 <-
data %>%
dplyr::filter(Conditie == "experimental", PrePost %in% c("Pre", "Post")) %>%
ggwithinstats(
x = PrePost,
y = APS_Total,
type = "np", # non-parametric statistics
xlab = "",
outlier.tagging = TRUE,
outlier.label = ID,
annotation.args = list(title = "Experimental - Pre & Post"))
p_aps_1
p_aps_2 <-
data %>%
dplyr::filter(Conditie == "experimental") %>%
group_by(ID) %>%
dplyr::filter(n() == 3) %>%
dplyr::ungroup() %>%
ggwithinstats(
x = PrePost,
y = APS_Total,
type = "np", # non-parametric statistics
xlab = "",
outlier.tagging = TRUE,
outlier.label = ID,
annotation.args = list(title = "Experimental - Pre, Post and Followup"))
p_aps_2
p_aps_3 <-
data %>%
dplyr::filter(Conditie == "ctrl", PrePost %in% c("Pre", "Post")) %>%
ggwithinstats(
x = PrePost,
y = APS_Total,
type = "np", # non-parametric statistics
xlab = "",
outlier.tagging = TRUE,
outlier.label = ID,
annotation.args = list(title = "Control - Pre & Post"))
p_aps_3
p_aps_4 <-
data %>%
dplyr::filter(Conditie == "ctrl") %>%
group_by(ID) %>%
dplyr::filter(n() == 3) %>%
dplyr::ungroup() %>%
ggwithinstats(
x = PrePost,
y = APS_Total,
type = "np", # non-parametric statistics
xlab = "",
outlier.tagging = TRUE,
outlier.label = ID,
annotation.args = list(title = "Experimental - Pre, Post and Followup"))
p_aps_4
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
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] ggstatsplot_0.8.0 pwr_1.2-2 rlang_0.4.11 emmeans_1.5.4 rio_0.5.26
[6] scales_1.1.1 ggpubr_0.4.0 tadaatoolbox_0.16.1 summarytools_0.8.8 rstatix_0.7.0
[11] broom_0.7.6 PerformanceAnalytics_1.5.2 xts_0.11-2 zoo_1.8-4 psych_2.0.12
[16] forcats_0.5.1 stringr_1.4.0 dplyr_1.0.6 purrr_0.3.4 readr_1.4.0
[21] tidyr_1.1.3 tibble_3.1.1 ggplot2_3.3.3 tidyverse_1.3.1 papaja_0.1.0.9997
[26] pacman_0.5.1
loaded via a namespace (and not attached):
[1] utf8_1.2.1 tidyselect_1.1.0 lme4_1.1-26 grid_3.6.1 gmp_0.5-13.2
[6] munsell_0.5.0 codetools_0.2-16 effectsize_0.4.5 statmod_1.4.35 withr_2.4.1
[11] colorspace_2.0-0 knitr_1.31 rstudioapi_0.13 DescTools_0.99.40 ipmisc_6.0.2
[16] ggsignif_0.6.1 labeling_0.4.2 mnormt_2.0.2 farver_2.1.0 coda_0.19-2
[21] vctrs_0.3.8 generics_0.1.0 TH.data_1.0-9 afex_0.28-1 xfun_0.22
[26] BWStest_0.2.2 R6_2.5.0 BayesFactor_0.9.12-4.2 bitops_1.0-6 reshape_0.8.8
[31] assertthat_0.2.1 multcomp_1.4-8 rootSolve_1.8.2.1 gtable_0.3.0 multcompView_0.1-7
[36] lmom_2.8 sandwich_2.5-0 MatrixModels_0.4-1 zeallot_0.1.0 PMCMRplus_1.9.0
[41] splines_3.6.1 rapportools_1.0 prismatic_1.0.0 BiocManager_1.30.10 yaml_2.2.1
[46] reshape2_1.4.4 abind_1.4-5 modelr_0.1.8 backports_1.2.1 tools_3.6.1
[51] ellipsis_0.3.2 WRS2_1.1-1 Rcpp_1.0.6 plyr_1.8.6 RCurl_1.95-4.11
[56] pbapply_1.3-4 viridis_0.5.1 correlation_0.6.1 haven_2.4.1 ggrepel_0.9.1
[61] fs_1.5.0 magrittr_2.0.1 data.table_1.14.0 openxlsx_4.1.0 lmerTest_3.0-1
[66] reprex_2.0.0 tmvnsim_1.0-2 mvtnorm_1.1-1 pixiedust_0.9.1 matrixStats_0.54.0
[71] hms_1.0.0 patchwork_1.1.1 evaluate_0.14 xtable_1.8-4 pairwiseComparisons_3.1.6
[76] readxl_1.3.1 gridExtra_2.3 compiler_3.6.1 crayon_1.4.1 minqa_1.2.4
[81] htmltools_0.5.1.1 mc2d_0.1-18 expm_0.999-3 Exact_2.1 lubridate_1.7.10
[86] DBI_1.0.0 SuppDists_1.1-9.4 kSamples_1.2-9 dbplyr_2.1.1 MASS_7.3-51.4
[91] boot_1.3-22 Matrix_1.2-17 car_3.0-10 cli_2.5.0 pryr_0.1.4
[96] quadprog_1.5-5 parallel_3.6.1 insight_0.14.2 pkgconfig_2.0.3 statsExpressions_1.1.0
[101] numDeriv_2016.8-1.1 foreign_0.8-71 xml2_1.3.2 paletteer_1.3.0 estimability_1.3
[106] rvest_1.0.0 snakecase_0.9.2 digest_0.6.27 parameters_0.14.0 janitor_2.1.0
[111] rmarkdown_2.7 cellranger_1.1.0 nortest_1.0-4 gld_2.6.2 curl_4.3
[116] gtools_3.8.1 nloptr_1.2.2.2 lifecycle_1.0.0 nlme_3.1-140 jsonlite_1.7.2
[121] carData_3.0-2 viridisLite_0.3.0 fansi_0.4.2 pillar_1.6.1 lattice_0.20-38
[126] httr_1.4.2 survival_2.44-1.1 glue_1.4.2 bayestestR_0.10.0 zip_1.0.0
[131] pander_0.6.3 class_7.3-15 stringi_1.5.3 performance_0.7.2 rematch2_2.1.2
[136] memoise_1.1.0 Rmpfr_0.7-1 e1071_1.7-0
A work by Claudiu Papasteri