## Define function that recodes to numeric, but watches out to coercion to not introduce NAs
<- function(df){
colstonumeric tryCatch({
<- as.data.frame(
df_num 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
<- function(df, tonumeric = FALSE, min = NULL, max = NULL) {
ReverseCode if(tonumeric) df <- colstonumeric(df)
<- (max + min) - df
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.
<- function(df, napercent = .1, tonumeric = FALSE, reversecols = NULL, min = NULL, max = NULL) {
ScoreLikert <- list(reversecols = reversecols, min = min, max = max)
reverse_list <- !sapply(reverse_list, is.null)
reverse_check
# Recode to numeric, but watch out to coercion to not introduce NAs
<- function(df){
colstonumeric tryCatch({
<- as.data.frame(
df_num 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)){
<- (max + min) - df[ ,reversecols]
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)
)
}##
<- function(data, title, x, y, outlier.label, xlab, ylab) {
my_ggwithinstats <- rlang::enquo(x)
x <- rlang::enquo(y)
y <- rlang::enquo(outlier.label)
outlier.label
%>%
data ::ggwithinstats(
ggstatsplotx = !!x,
y = !!y,
title = title,
xlab = xlab,
ylab = ylab,
outlier.tagging = TRUE, # whether outliers need to be tagged
outlier.label = !!outlier.label, # variable to be used for tagging outliers
outlier.coef = 2,
pairwise.comparisons = TRUE,
pairwise.display = "significant",
results.subtitle = TRUE,
type = "parametric",
bf.message = FALSE,
p.adjust.method = "none",
point.path = TRUE,
ggtheme = ggprism::theme_prism(),
# package = "RColorBrewer", # "ggsci",
# palette = "Dark", # "default_jco",
violin.args = list(width = 0.9, alpha = 0.2, size = 1, color = "black"),
centrality.point.args = list(size = 5, color = "darkred"),
centrality.label.args = list(size = 3, nudge_x = 0.2, segment.linetype = 5, fill = "#FFF8E7"),
ggplot.component = list(
theme(
plot.title = element_text(hjust = 0, size = 16),
plot.subtitle = element_text(hjust = 0, size = 12),
plot.caption = element_text(hjust = 0, size = 12),
text = element_text(size = 14)
))+ scale_colour_grey(start = 0.2, end = 0.2) # hacky way to change point color
)
}
# For publication
<- function(data, title, x, y, outlier.label, xlab, ylab,
my_ggwithinstats2 outlier.tagging = FALSE, results.subtitle = TRUE,
centrality.label.args = TRUE, point.path = TRUE,
type = "parametric",
# ... for limits and breaks
...) { <- rlang::enquo(x)
x <- rlang::enquo(y)
y <- rlang::enquo(outlier.label)
outlier.label
if(centrality.label.args){
<- list(size = 3, nudge_x = 0.2, segment.linetype = 5, fill = "#FFF8E7")
centrality.label.args else{
}<- list(size = 0, nudge_x = 10, segment.linetype = 0, alpha = 0) # very hacky way of not showing label
centrality.label.args
}
%>%
data ::ggwithinstats(
ggstatsplotx = !!x,
y = !!y,
title = title,
xlab = xlab,
ylab = ylab,
outlier.tagging = outlier.tagging, # whether outlines need to be tagged
outlier.label = !!outlier.label, # variable to be used for tagging outliers
outlier.coef = 2,
pairwise.comparisons = TRUE,
pairwise.display = "all",
results.subtitle = results.subtitle,
type = type,
bf.message = FALSE,
p.adjust.method = "none",
point.path = point.path,
ggtheme = ggprism::theme_prism(),
# package = "RColorBrewer", # "ggsci",
# palette = "Dark", # "default_jco",
violin.args = list(width = 0.9, alpha = 0.2, size = 1, color = "black"),
centrality.plotting = TRUE,
centrality.type = "parameteric",
centrality.point.args = list(size = 5, color = "darkred"),
centrality.label.args = centrality.label.args,
ggplot.component = list(
theme(
plot.title = element_text(hjust = 0, size = 16),
plot.subtitle = element_text(hjust = 0, size = 12),
plot.caption = element_text(hjust = 0, size = 12),
text = element_text(size = 14)
))+ scale_colour_grey(start = 0.2, end = 0.2) + # hacky way to change point color
) scale_y_continuous(...)
}
# Fast ggsave - saves plot with filename of R plot object
<- function(plot, device = "png", path = NULL,
fast_ggsave units = "in", dpi = 300, width = 5, height = 5, ...){
<- deparse(substitute(plot))
plot_name ::ggsave(filename = paste0(plot_name, ".", device), plot = plot,
ggplot2device = device, path = path,
units = units, dpi = dpi,
width = width, height = height,
...
)
# use: fast_ggsave(jrad_ox_p, path = savefolder) }
<- "C:/Users/Mihai/Desktop/R Notebooks/notebooks/PA4-geronto"
folder <- "Set teste zi 1 pre grup vârstnici.xlsx"
file_beh_pre <- "Set teste zi 5 post grup vârstnici.xlsx"
file_beh_post
### Pre
<- rio::import(file.path(folder, file_beh_pre))
behav_pre <- behav_pre[-1,]
behav_pre names(behav_pre)[6] <- "id"
$id == "CU16PA1",]$id <- "CN16PA1" # typo, should be "CN_16" instead of "CU-16" -- in vas its "CN_16"
behav_pre[behav_pre<- behav_pre %>%
behav_pre ::clean_names() %>%
janitor::mutate(id = stringr::str_remove(id, "PA1"),
dplyrid = gsub('^(.{2})(.*)$', '\\1_\\2', id)) %>% # insert "_" afeter code
::separate(id, into = c("forma", "ID_no"), remove = FALSE, sep = "_") %>%
tidyr::mutate(forma = gsub('^(.{1})(.*)$', '\\1_\\2', id)) %>%
dplyr::separate(forma, into = c("Cond", "Cog_imp"), remove = FALSE, sep = "_") %>%
tidyr::mutate(Cond = dplyr::case_when(Cond == "C" ~ "CTRL",
dplyr== "E" ~ "TR",
Cond TRUE ~ NA_character_)) %>%
::mutate(id = stringr::str_replace(id, "O", "0"),
dplyrid = stringr::str_replace(id, "o", "0")) %>%
::mutate(ID_no = stringr::str_replace(ID_no, "O", "0"),
dplyrID_no = stringr::str_replace(ID_no, "o", "0"),
ID_no = as.numeric(ID_no))
## Post
<- rio::import(file.path(folder, file_beh_post))
behav_post <- behav_post[-1,]
behav_post <- behav_post[,-c(6:9)]
behav_post names(behav_post)[6] <- "id"
<- behav_post %>%
behav_post ::clean_names() %>%
janitor::mutate(id = stringr::str_remove(id, "PA1"),
dplyrid = gsub('^(.{2})(.*)$', '\\1_\\2', id)) %>% # insert "_" afeter code
::separate(id, into = c("forma", "ID_no"), remove = FALSE, sep = "_") %>%
tidyr::mutate(forma = gsub('^(.{1})(.*)$', '\\1_\\2', id)) %>%
dplyr::separate(forma, into = c("Cond", "Cog_imp"), remove = FALSE, sep = "_") %>%
tidyr::mutate(Cond = dplyr::case_when(Cond == "C" ~ "CTRL",
dplyr== "E" ~ "TR",
Cond TRUE ~ NA_character_)) %>%
::mutate(id = stringr::str_replace(id, "O", "0"),
dplyrid = stringr::str_replace(id, "o", "0")) %>%
::mutate(ID_no = stringr::str_replace(ID_no, "O", "0"),
dplyrID_no = stringr::str_replace(ID_no, "o", "0"),
ID_no = as.numeric(ID_no))
# checks
<- data.frame(Pre = names(behav_pre), Post = names(behav_post))
bla
%>%
behav_pre ::count(id) %>% # 40 x 1
dplyr::arrange(id) %>%
dplyr::pull(id) -> behav_pre_ids
dplyr
%>%
behav_post ::count(id) %>% # 40 x 1
dplyr::arrange(id) %>%
dplyr::pull(id) -> behav_post_ids
dplyr
identical(behav_pre_ids, behav_post_ids) # one has CU_16, the other CN_16
[1] TRUE
## PANAS: Positive Affect Score = sum items 1, 3, 5, 9, 10, 12, 14, 16, 17, 19. Negative Affect Score = sum items 2, 4, 6, 7, 8, 11, 13, 15, 18, 20.
<- 11:30
index_item_panas colnames(behav_pre)[index_item_panas] <- sprintf("PANAS_%d", 1:20)
colnames(behav_post)[index_item_panas] <- sprintf("PANAS_%d", 1:20)
<- data.frame(lapply(behav_pre[, index_item_panas],
behav_pre[, index_item_panas] function(x) {gsub(".*în foarte mică măsură.*", "1", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_pre[, index_item_panas],
behav_pre[, index_item_panas] function(x) {gsub(".*în mică măsură.*", "2", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_pre[, index_item_panas],
behav_pre[, index_item_panas] function(x) {gsub(".*într-o oarecare măsură.*", "3", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_pre[, index_item_panas],
behav_pre[, index_item_panas] function(x) {gsub(".*în mare măsură.*", "4", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_pre[, index_item_panas],
behav_pre[, index_item_panas] function(x) {gsub(".*în foarte mare măsură.*", "5", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_post[, index_item_panas],
behav_post[, index_item_panas] function(x) {gsub(".*în foarte mică măsură.*", "1", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_post[, index_item_panas],
behav_post[, index_item_panas] function(x) {gsub(".*în mică măsură.*", "2", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_post[, index_item_panas],
behav_post[, index_item_panas] function(x) {gsub(".*într-o oarecare măsură.*", "3", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_post[, index_item_panas],
behav_post[, index_item_panas] function(x) {gsub(".*în mare măsură.*", "4", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_post[, index_item_panas],
behav_post[, index_item_panas] function(x) {gsub(".*în foarte mare măsură.*", "5", x)}), stringsAsFactors = FALSE)
# Scoring
$PA_Total <- ScoreLikert(behav_pre[, index_item_panas][c(1, 3, 5, 9, 10, 12, 14, 16, 17, 19)],
behav_pretonumeric = TRUE, napercent = .11) # not more than 1 NAs for 10 items
$NA_Total <- ScoreLikert(behav_pre[, index_item_panas][c(2, 4, 6, 7, 8, 11, 13, 15, 18, 20)],
behav_pretonumeric = TRUE, napercent = .11) # not more than 1 NAs for 10 items
$PA_Total <- ScoreLikert(behav_post[, index_item_panas][c(1, 3, 5, 9, 10, 12, 14, 16, 17, 19)],
behav_posttonumeric = TRUE, napercent = .11) # not more than 1 NAs for 10 items
$NA_Total <- ScoreLikert(behav_post[, index_item_panas][c(2, 4, 6, 7, 8, 11, 13, 15, 18, 20)],
behav_posttonumeric = TRUE, napercent = .11) # not more than 1 NAs for 10 items
## PSS-SF 14 (likert 0-4). Items 4, 5, 6, 7, 9, 10, and 13 are scored in reverse direction.
<- 31:44
index_item_pss <- c(4, 5, 6, 7, 9, 10, 13)
index_item_revPSS colnames(behav_pre)[index_item_pss] <- sprintf("PSS_%d", 1:14)
colnames(behav_post)[index_item_pss] <- sprintf("PSS_%d", 1:14)
<- data.frame(lapply(behav_pre[, index_item_pss],
behav_pre[, index_item_pss] function(x) {gsub(".*niciodată.*", "0", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_pre[, index_item_pss],
behav_pre[, index_item_pss] function(x) {gsub(".*aproape niciodată.*", "1", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_pre[, index_item_pss],
behav_pre[, index_item_pss] function(x) {gsub(".*uneori.*", "2", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_pre[, index_item_pss],
behav_pre[, index_item_pss] function(x) {gsub(".*destul de des.*", "3", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_pre[, index_item_pss],
behav_pre[, index_item_pss] function(x) {gsub(".*foarte des.*", "4", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_post[, index_item_pss],
behav_post[, index_item_pss] function(x) {gsub(".*niciodată.*", "0", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_post[, index_item_pss],
behav_post[, index_item_pss] function(x) {gsub(".*aproape niciodată.*", "1", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_post[, index_item_pss],
behav_post[, index_item_pss] function(x) {gsub(".*uneori.*", "2", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_post[, index_item_pss],
behav_post[, index_item_pss] function(x) {gsub(".*destul de des.*", "3", x)}), stringsAsFactors = FALSE)
<- data.frame(lapply(behav_post[, index_item_pss],
behav_post[, index_item_pss] function(x) {gsub(".*foarte des.*", "4", x)}), stringsAsFactors = FALSE)
# Score
<- colstonumeric(behav_pre[, index_item_pss])
behav_pre[, index_item_pss] <- colstonumeric(behav_post[, index_item_pss])
behav_post[, index_item_pss]
<- ReverseCode(behav_pre[, index_item_pss][index_item_revPSS], tonumeric = FALSE, min = 0, max = 4)
behav_pre[, index_item_pss][index_item_revPSS] <- ReverseCode(behav_post[, index_item_pss][index_item_revPSS], tonumeric = FALSE, min = 0, max = 4)
behav_post[, index_item_pss][index_item_revPSS]
$PSS_Total <- ScoreLikert(behav_pre[, index_item_pss], napercent = .11)
behav_pre$PSS_Total <- ScoreLikert(behav_post[, index_item_pss], napercent = .11)
behav_post
## PS int
<- 45:48 # 45:55
index_item_PSint colnames(behav_pre)[index_item_PSint] <- sprintf("PSint_%d", 1:4)
colnames(behav_post)[index_item_PSint] <- sprintf("PSint_%d", 1:4)
sprintf("PSint_%d", 1:4)] <-
behav_pre[, sprintf("PSint_%d", 1:4)] %>%
behav_pre[, ::mutate_all(readr::parse_number)
dplyr
sprintf("PSint_%d", 1:4)] <-
behav_post[, sprintf("PSint_%d", 1:4)] %>%
behav_post[, ::mutate_all(readr::parse_number)
dplyr
# Score
$PSint_Total <- ScoreLikert(behav_pre[, index_item_PSint], tonumeric = TRUE, napercent = .33)
behav_pre$PSint_Total <- ScoreLikert(behav_post[, index_item_PSint], tonumeric = TRUE, napercent = .33)
behav_post
## PS mot
<- 49:52
index_item_PSmot colnames(behav_pre)[index_item_PSmot] <- sprintf("PSmot_%d", 1:4)
colnames(behav_post)[index_item_PSmot] <- sprintf("PSmot_%d", 1:4)
sprintf("PSmot_%d", 1:4)] <-
behav_pre[, sprintf("PSmot_%d", 1:4)] %>%
behav_pre[, ::mutate_all(~case_when(stringr::str_detect(., "dezacord puternic") ~ 1,
dplyr::str_detect(., "dezacord") ~ 2,
stringr::str_detect(., "neutru") ~ 3,
stringr::str_detect(., "acord") ~ 4,
stringr::str_detect(., "acord puternic") ~ 5,
stringrTRUE ~ NA_real_))
sprintf("PSmot_%d", 1:4)] <-
behav_post[, sprintf("PSmot_%d", 1:4)] %>%
behav_post[, ::mutate_all(~case_when(stringr::str_detect(., "dezacord puternic") ~ 1,
dplyr::str_detect(., "dezacord") ~ 2,
stringr::str_detect(., "neutru") ~ 3,
stringr::str_detect(., "acord") ~ 4,
stringr::str_detect(., "acord puternic") ~ 5,
stringrTRUE ~ NA_real_))
# Score
$PSmot_Total <- ScoreLikert(behav_pre[, index_item_PSmot], tonumeric = TRUE, napercent = .33)
behav_pre$PSmot_Total <- ScoreLikert(behav_post[, index_item_PSmot], tonumeric = TRUE, napercent = .33)
behav_post
## PS iden
<- 53:55
index_item_PSiden colnames(behav_pre)[index_item_PSiden] <- sprintf("PSiden_%d", 1:3)
colnames(behav_post)[index_item_PSiden] <- sprintf("PSiden_%d", 1:3)
sprintf("PSiden_%d", 1:3)] <-
behav_pre[, sprintf("PSiden_%d", 1:3)] %>%
behav_pre[, ::mutate_all(~case_when(stringr::str_detect(., "dezacord puternic") ~ 1,
dplyr::str_detect(., "dezacord") ~ 2,
stringr::str_detect(., "neutru") ~ 3,
stringr::str_detect(., "acord") ~ 4,
stringr::str_detect(., "acord puternic") ~ 5,
stringrTRUE ~ NA_real_))
sprintf("PSiden_%d", 1:3)] <-
behav_post[, sprintf("PSiden_%d", 1:3)] %>%
behav_post[, ::mutate_all(~case_when(stringr::str_detect(., "dezacord puternic") ~ 1,
dplyr::str_detect(., "dezacord") ~ 2,
stringr::str_detect(., "neutru") ~ 3,
stringr::str_detect(., "acord") ~ 4,
stringr::str_detect(., "acord puternic") ~ 5,
stringrTRUE ~ NA_real_))
# Score
$PSiden_Total <- ScoreLikert(behav_pre[, index_item_PSiden], tonumeric = TRUE, napercent = .33)
behav_pre$PSiden_Total <- ScoreLikert(behav_post[, index_item_PSiden], tonumeric = TRUE, napercent = .33)
behav_post
## IOS
colnames(behav_pre)[56] <- "IOS"
colnames(behav_post)[56] <- "IOS"
$IOS <- as.numeric(behav_pre$IOS)
behav_pre$IOS <- as.numeric(behav_post$IOS)
behav_post
## Add some columns
<- behav_pre %>%
behav_pre ::mutate(PrePost = rep("Pre", nrow(.)))
dplyr
<- behav_post %>%
behav_post ::mutate(PrePost = rep("Post", nrow(.)))
dplyr
# Long format
<- dplyr::bind_rows(behav_pre, behav_post) %>%
behav_long ::mutate(PrePost = factor(PrePost, levels = c("Pre", "Post"))) dplyr
# PSS
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSS_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Stress",
title = "CTRL")
Registered S3 methods overwritten by 'parameters':
method from
as.double.parameters_kurtosis datawizard
as.double.parameters_skewness datawizard
as.double.parameters_smoothness datawizard
as.numeric.parameters_kurtosis datawizard
as.numeric.parameters_skewness datawizard
as.numeric.parameters_smoothness datawizard
print.parameters_distribution datawizard
print.parameters_kurtosis datawizard
print.parameters_skewness datawizard
summary.parameters_kurtosis datawizard
summary.parameters_skewness datawizard
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSS_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Stress",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# NA
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = NA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Negative Affect",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = NA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Negative Affect",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# PA
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Positive Affect",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Positive Affect",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# Psint
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial intention",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial intention",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# Psmot
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial motivation",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial motivation",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# IOS
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = IOS, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Closeness",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = IOS, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Closeness",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
<- "C:/Users/Mihai/Desktop/R Notebooks/notebooks/PA4-geronto"
folder <- "date VAS STRES PRE varstnici - modif.xls"
file_pre <- "date VAS STRES POST varstnici.xls"
file_post
<- rio::import(file.path(folder, file_pre)) stres_pre
<- stres_pre[-1, c(3:5, 10:11)]
stres_pre
<- stres_pre[-37,] # Sofi notebook: on 31-07 id 19 wanted 0 but put 25 ... he has two recodings with 25 and 0 ... delete the one with 25
stres_pre #stres_pre <- stres_pre[-122,] # no data
names(stres_pre)[4:5] <- c("id", "vas_stres")
$id == "EN08PA1",]$id <- "EU08PA1" # typo "EN_08", should be "EU_08"
stres_pre[stres_pre<- stres_pre %>%
stres_pre ::clean_names() %>%
janitor::mutate(id = stringr::str_remove(id, "PA1"),
dplyrid = gsub('^(.{2})(.*)$', '\\1_\\2', id)) %>% # insert "_" after code
::separate(id, into = c("forma", "ID_no"), remove = FALSE, sep = "_") %>%
tidyr::mutate(forma = gsub('^(.{1})(.*)$', '\\1_\\2', id)) %>%
dplyr::separate(forma, into = c("Cond", "Cog_imp"), remove = FALSE, sep = "_") %>%
tidyr::mutate(Cond = dplyr::case_when(Cond == "C" ~ "CTRL",
dplyr== "E" ~ "TR",
Cond TRUE ~ NA_character_)) %>%
::mutate(vas_stres = as.numeric(vas_stres)) %>%
dplyr::mutate(id = stringr::str_replace(id, "O", "0"),
dplyrid = stringr::str_replace(id, "o", "0")) %>%
::mutate(ID_no = stringr::str_replace(ID_no, "O", "0"),
dplyrID_no = stringr::str_replace(ID_no, "o", "0"),
ID_no = as.numeric(ID_no))
<- rio::import(file.path(folder, file_post)) stres_post
<- stres_post[-1, c(3:5, 10:11)]
stres_post names(stres_post)[4:5] <- c("id", "vas_stres")
$id == "CN12PA1",]$id <- "CU12PA1" # typo "CN_12", should be "CU_12"
stres_post[stres_post<- stres_post %>%
stres_post ::clean_names() %>%
janitor::mutate(id = stringr::str_remove(id, "PA1"),
dplyrid = gsub('^(.{2})(.*)$', '\\1_\\2', id)) %>% # insert "_" after code
::separate(id, into = c("forma", "ID_no"), remove = FALSE, sep = "_") %>%
tidyr::mutate(forma = gsub('^(.{1})(.*)$', '\\1_\\2', id)) %>%
dplyr::separate(forma, into = c("Cond", "Cog_imp"), remove = FALSE, sep = "_") %>%
tidyr::mutate(Cond = dplyr::case_when(Cond == "C" ~ "CTRL",
dplyr== "E" ~ "TR",
Cond TRUE ~ NA_character_)) %>%
::mutate(vas_stres = as.numeric(vas_stres)) %>%
dplyr::mutate(id = stringr::str_replace(id, "O", "0"),
dplyrid = stringr::str_replace(id, "o", "0")) %>%
::mutate(ID_no = stringr::str_replace(ID_no, "O", "0"),
dplyrID_no = stringr::str_replace(ID_no, "o", "0"),
ID_no = as.numeric(ID_no))
# retrasi
<- c(1, 6, 7)
retrasi_post
# check
%>%
stres_pre ::count(id) %>%
dplyr::pull(id) -> stres_pre_ids
dplyrsetdiff(stres_pre_ids, behav_pre_ids)
%>%
stres_post ::count(id) %>%
dplyr::pull(id) -> stres_post_ids
dplyrsetdiff(stres_post_ids, behav_pre_ids)
setdiff(stres_pre_ids, stres_post_ids)
# Add variable Time
# its: pre-post x4 + pre-post-post => pre = 1,3,5,7,9 post = 2,4,6,8,10,11
# in pre EN_19 have 6 instead of 5 data -- fixed now; EN_04 fixed in excel file
<-
stres_pre_time %>%
stres_pre ::mutate(row_numb = dplyr::row_number()) %>%
dplyr::arrange(desc(row_numb)) %>% # order of rows needs to be inverted to be in chronological order
dplyr::select(-row_numb) %>%
dplyr::group_by(id) %>%
dplyr::mutate(Time = dplyr::row_number()) %>%
dplyr::mutate(Time = dplyr::case_when(Cond == "CTRL" & Time == 1 ~ 9,
dplyr== "TR" & Time == 1 ~ 1,
Cond == "TR" & Time == 2 ~ 3,
Cond == "TR" & Time == 3 ~ 5,
Cond == "TR" & Time == 4 ~ 7,
Cond == "TR" & Time == 5 ~ 9,
Cond TRUE ~ NA_real_)) %>%
::ungroup()
dplyr
<-
stres_post_time %>%
stres_post ::drop_na(vas_stres) %>% # there is one NA that ruins the flow
tidyr::mutate(row_numb = dplyr::row_number()) %>%
dplyr::arrange(desc(row_numb)) %>% # order of rows needs to be inverted to be in chronological order
dplyr::select(-row_numb) %>%
dplyr::group_by(id) %>%
dplyr::mutate(Time = dplyr::row_number()) %>%
dplyr::mutate(Time = dplyr::case_when(Cond == "CTRL" & Time == 1 ~ 10,
dplyr== "CTRL" & Time == 2 ~ 11,
Cond == "TR" & Time == 1 ~ 2,
Cond == "TR" & Time == 2 ~ 4,
Cond == "TR" & Time == 3 ~ 6,
Cond == "TR" & Time == 4 ~ 8,
Cond == "TR" & Time == 5 ~ 10,
Cond == "TR" & Time == 6 ~ 11,
Cond TRUE ~ NA_real_)) %>%
::ungroup()
dplyr
# check
%>%
stres_pre_time ::group_by(id) %>%
dplyrfilter(Time == max(Time)) %>%
print(n = Inf) # all are 1 and 9 -- good
%>%
stres_post_time ::group_by(id) %>%
dplyrfilter(Time == max(Time)) %>%
print(n = Inf) # all are 11 -- good because last post is same in TR and CTRL
# Long data
<- dplyr::bind_rows(stres_pre_time, stres_post_time) %>%
stres_time_long ::mutate(Time = factor(Time, levels = c(1:11))) dplyr
%>%
stres_time_long ::filter(Cond == "CTRL") %>%
dplyr::ggwithinstats(x = Time, y = vas_stres, outlier.label = id,
ggstatsplottype = "np")
%>%
stres_time_long ::filter(Cond == "TR") %>%
dplyr::filter(Time %in% c(9, 10, 11)) %>%
dplyr::ggwithinstats(x = Time, y = vas_stres, outlier.label = id,
ggstatsplottype = "np")
%>%
stres_time_long ::filter(Cond == "TR") %>%
dplyr::ggwithinstats(x = Time, y = vas_stres, outlier.label = id,
ggstatsplottype = "np")
Warning: Number of labels is greater than default palette color count.
Try using another color `palette` (and/or `package`).
%>%
stres_time_long ::filter(Cond == "TR") %>%
dplyr::filter(!Time %in% c(9, 10, 11)) %>%
dplyr::ggwithinstats(x = Time, y = vas_stres, outlier.label = id,
ggstatsplottype = "np")
# deficienta cognitiva (N-normal, U-usor deficit cognitiv)
# PSS
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSS_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Stress",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSS_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Stress",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# NA
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = NA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Negative Affect",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = NA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Negative Affect",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# PA
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Positive Affect",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Positive Affect",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# Psint
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial intention",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial intention",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# Psmot
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial motivation",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial motivation",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# IOS
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = IOS, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Closeness",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "N") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = IOS, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Closeness",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# deficienta cognitiva (N-normal, U-usor deficit cognitiv)
# PSS
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSS_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Stress",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSS_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Stress",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# NA
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = NA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Negative Affect",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = NA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Negative Affect",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# PA
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Positive Affect",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PA_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Positive Affect",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# Psint
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial intention",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial intention",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# Psmot
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial motivation",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = PSint_Total, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Prosocial motivation",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# IOS
%>%
behav_long ::filter(Cond == "CTRL") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = IOS, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Closeness",
title = "CTRL")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
%>%
behav_long ::filter(Cond == "TR") %>%
dplyr::filter(Cog_imp == "U") %>%
dplyrmy_ggwithinstats2(x = PrePost, y = IOS, outlier.label = ID_no, type = "np",
xlab = "", ylab = "Closeness",
title = "TR")
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
# experiment has the F1-LD-F1 designstructure with treatment being the whole-plot factor. (p. 13 art R Journal)
<-
stres_f1ldf1 %>%
stres_time_long ::filter(Time %in% c(9, 10, 11)) %>%
dplyr::mutate(Time = factor(Time, levels = c(9, 10, 11)))
dplyr
::f1.ld.f1(y = stres_f1ldf1$vas_stres, time = stres_f1ldf1$Time, group = stres_f1ldf1$Cond, subject = stres_f1ldf1$id) nparLD
Attaching package: ‘MASS’
The following object is masked from ‘package:rstatix’:
select
The following object is masked from ‘package:dplyr’:
select
Total number of observations: 120
Total number of subjects: 40
Total number of missing observations: 0
Class level information
-----------------------
Levels of Time (sub-plot factor time) : 3
Levels of Group (whole-plot factor group) : 2
Abbreviations
-----------------------
RankMeans = Rank means
Nobs = Number of observations
RTE = Relative treatment effect
case2x2 = tests for 2-by-2 design
Wald.test = Wald-type test statistic
ANOVA.test = ANOVA-type test statistic with Box approximation
ANOVA.test.mod.Box = modified ANOVA-type test statistic with Box approximation
Wald.test.time = Wald-type test statistic for simple time effect
ANOVA.test.time = ANOVA-type test statistic for simple time effect
N = Standard Normal Distribution N(0,1)
T = Student's T distribution with respective degrees of freedom
pattern.time (time effects) = Test against patterned alternatives in time using normal distribution ( no pattern specified )
pair.comparison = Tests for pairwise comparisions (without specifying a pattern)
pattern.pair.comparison = Test for pairwise comparisons with patterned alternatives in time ( no pattern specified )
pattern.group (group effects) = Test against patterned alternatives in group ( no pattern specified )
covariance = Covariance matrix
Note: The description output above will disappear by setting description=FALSE in the input. See the help file for details.
F1 LD F1 Model
-----------------------
Check that the order of the time and group levels are correct.
Time level: 9 10 11
Group level: CTRL TR
If the order is not correct, specify the correct order in time.order or group.order.
$RTE
$case2x2
NULL
$Wald.test
Statistic df p-value
Group 0.11896719 1 0.73015736878659
Time 34.63043649 2 0.00000003020621
Group:Time 0.00187216 2 0.99906435792943
$ANOVA.test
Statistic df p-value
Group 0.118967187 1.00000 0.7301573687866
Time 14.613795919 1.86256 0.0000009784007
Group:Time 0.001182357 1.86256 0.9981935278646
$ANOVA.test.mod.Box
Statistic df1 df2 p-value
Group 0.1189672 1 37.99886 0.7320593
$Wald.test.time
$ANOVA.test.time
$pattern.time
NULL
$pair.comparison
$pattern.pair.comparison
NULL
$covariance
NULL
$model.name
[1] "F1 LD F1 Model"
R version 4.1.0 (2021-05-18)
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
system code page: 1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] MASS_7.3-54 rio_0.5.27 scales_1.1.1 ggpubr_0.4.0 summarytools_1.0.0
[6] rstatix_0.7.0 broom_0.7.9 PerformanceAnalytics_2.0.4 xts_0.12.1 zoo_1.8-9
[11] psych_2.1.6 plyr_1.8.6 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7
[16] purrr_0.3.4 readr_2.0.1 tidyr_1.1.3 tibble_3.1.4 ggplot2_3.3.5
[21] tidyverse_1.3.1 papaja_0.1.0.9997 pacman_0.5.1
loaded via a namespace (and not attached):
[1] readxl_1.3.1 pairwiseComparisons_3.1.6 backports_1.2.1 splines_4.1.0 gmp_0.6-2
[6] kSamples_1.2-9 ipmisc_6.0.2 TH.data_1.0-10 pryr_0.1.5 digest_0.6.28
[11] SuppDists_1.1-9.5 htmltools_0.5.2 magick_2.7.3 fansi_0.5.0 magrittr_2.0.1
[16] checkmate_2.0.0 memoise_2.0.0 paletteer_1.4.0 tzdb_0.1.2 openxlsx_4.2.4
[21] modelr_0.1.8 matrixStats_0.60.1 sandwich_3.0-1 colorspace_2.0-2 rvest_1.0.1
[26] ggrepel_0.9.1 haven_2.4.3 xfun_0.25 prismatic_1.0.0 tcltk_4.1.0
[31] crayon_1.4.1 jsonlite_1.7.2 zeallot_0.1.0 survival_3.2-13 glue_1.4.2
[36] gtable_0.3.0 emmeans_1.6.3 MatrixModels_0.5-0 statsExpressions_1.1.0 car_3.0-11
[41] Rmpfr_0.8-4 abind_1.4-5 rapportools_1.0 mvtnorm_1.1-2 DBI_1.1.1
[46] PMCMRplus_1.9.0 Rcpp_1.0.7 performance_0.7.3 xtable_1.8-4 tmvnsim_1.0-2
[51] foreign_0.8-81 datawizard_0.2.0.1 httr_1.4.2 ellipsis_0.3.2 farver_2.1.0
[56] pkgconfig_2.0.3 reshape_0.8.8 multcompView_0.1-8 dbplyr_2.1.1 utf8_1.2.2
[61] janitor_2.1.0 labeling_0.4.2 effectsize_0.4.5 tidyselect_1.1.1 rlang_0.4.11
[66] munsell_0.5.0 cellranger_1.1.0 tools_4.1.0 cachem_1.0.6 ggprism_1.0.3
[71] cli_3.0.1 generics_0.1.0 fastmap_1.1.0 BWStest_0.2.2 rematch2_2.1.2
[76] knitr_1.33 fs_1.5.0 zip_2.2.0 pander_0.6.4 WRS2_1.1-3
[81] pbapply_1.4-3 nlme_3.1-152 xml2_1.3.2 correlation_0.7.0 compiler_4.1.0
[86] rstudioapi_0.13 curl_4.3.2 ggsignif_0.6.2 reprex_2.0.1 stringi_1.7.4
[91] parameters_0.14.0 lattice_0.20-44 Matrix_1.3-4 vctrs_0.3.8 pillar_1.6.3
[96] lifecycle_1.0.1 mc2d_0.1-21 BiocManager_1.30.16 estimability_1.3 data.table_1.14.0
[101] insight_0.14.4 patchwork_1.1.1 R6_2.5.1 BayesFactor_0.9.12-4.2 codetools_0.2-18
[106] boot_1.3-28 gtools_3.9.2 assertthat_0.2.1 nparLD_2.1 withr_2.4.2
[111] mnormt_2.0.2 multcomp_1.4-17 bayestestR_0.11.0 parallel_4.1.0 hms_1.1.0
[116] quadprog_1.5-8 grid_4.1.0 coda_0.19-4 snakecase_0.11.0 carData_3.0-4
[121] lubridate_1.7.10 base64enc_0.1-3 ggstatsplot_0.8.0
A work by Claudiu Papasteri