I am trying to include Pooled Standard Error (PSE) and Eta square to tbl_summary(). PSE is calculated by sqrt(mean(residuals^2)/n), I tried to calculate step by step by extracting the residuals from either aov() or lm(), but I got the error saying The dimension of respected variable and the added statistic do not match. Expecting statistic/dataframe to be length/ no. rows 1. Here is my code:
PSE <- function(data, variable, by,...) {
aov(data[["variable"]] ~ as.factor(data[[by]]))$residuals
}
Dataset_TPA_Full %>%
select(diet,hardness_g,adhesiveness_g_sec, resilence, cohesion, springiness, gumminess, chewiness, firmness_g_force_1, density_g_l)%>%
tbl_summary(
by = diet,
statistic = all_continuous() ~ "{mean} ± {sem}",
label = list(hardness_g = "Hardness (g)",
adhesiveness_g_sec = "Adhesiveness (g/ sec)",
resilence = "Resilience",
cohesion = "Cohesion",
springiness = "Springiness",
gumminess = "Gumminess",
chewiness = "Chewiness",
firmness_g_force_1 = "Firmness (g)",
density_g_l = "Density (g/ L)")
) %>%
add_p(
test = all_continuous() ~ "aov",
) %>%
add_stat(fns = all_continuous() ~ PSE) %>%
modify_header(label = "**Treatment**", p.value = "**p-value**") %>%
bold_labels() %>%
bold_levels()
Also when I tried to add Eta squared using this code, it return missing data argument when I put it in the add_stat() function
my_ES_test <- function(data, variable, by, ...) {
aovmod = aov(data[[variable]] ~ data[[by]])
lsr::etaSquared(aovmod)[1,1]
}
Can you help me with this? Thank you.
