'Problem with modifying the digits in tbl_summary
I have a problem adjusting the digits for tbl_summary(). Here is my code:
library(flextable)
library(dplyr)
library(officer)
library(gtsummary)
library(janitor)
### Effect size
my_ES_test <- function(data, variable, by, ...) {
aovmod = aov(data[[variable]] ~ data[[by]])
lsr::etaSquared(aovmod)[1,1]
}
### Standard Error Mean
sem <- function(x){
sqrt(var(x, na.rm=TRUE)/sum(!is.na(x)))
}
### Pooled Standard Error
PSE <- function(data, variable, by,...) {
s <- data %>%
group_by(!!sym(by)) %>%
summarise(s = var(!!sym(variable)),
n = n()) %>%
mutate(num = s*(n-1))
psd <- sqrt(sum(s$num)/(sum(s$n) - nrow(s)))
psd*sqrt(sum(1/s$n))
}
### gtsummary
Iris_data <- iris %>%
select(names(iris))%>%
tbl_summary(
by = Species,
digits = all_continuous() ~ c(2,2),
type = list(everything() ~ "continuous"),
statistic = all_continuous() ~ "{mean} ± {sem}",
label = list(Sepal.Length = "Sepal Length",
Sepal.Width = "Sepal Width",
Petal.Length = "Petal Length",
Petal.Width = "Petal Width")
) %>%
add_stat(fns = all_continuous() ~ PSE) %>%
add_stat(fns = all_continuous() ~ my_ES_test) %>%
add_p(
test = all_continuous() ~ "aov", pvalue_fun = function(x) style_pvalue(x, digits = 3)
) %>%
modify_header(label = "**Size**", p.value = "**p-value**", add_stat_1 = "**PSE**", add_stat_2 = "**\U03B7\U00B2**") %>%
modify_footnote(add_stat_1 = "Pooled Standard Error", abbreviation = FALSE) %>%
bold_levels() %>%
bold_labels() %>%
as_flex_table()
In general the table works perfectly fine. However, if I moved the p-value above add_stat(fns = all_continuous() ~ PSE) then the p-value digits will change back to it's original format without three digits. Is it a way to fix this? Also, I cannot adjust the digits for the PSE for some reasons using the purrr::partial(style_ratio, digits = 3). Trying over and over but still cannot solve the problem.
Thank you.
Solution 1:[1]
You can use the modify_fmt_fun() function to modify the function that format/style the columns. Is this what you're asking?
library(dplyr, warn.conflicts = FALSE)
library(officer)
library(gtsummary)
#> #BlackLivesMatter
### Effect size
my_ES_test <- function(data, variable, by, ...) {
aovmod = aov(data[[variable]] ~ data[[by]])
lsr::etaSquared(aovmod)[1,1]
}
### Standard Error Mean
sem <- function(x){
sqrt(var(x, na.rm=TRUE)/sum(!is.na(x)))
}
### Pooled Standard Error
PSE <- function(data, variable, by,...) {
s <- data %>%
group_by(!!sym(by)) %>%
summarise(s = var(!!sym(variable)),
n = n()) %>%
mutate(num = s*(n-1))
psd <- sqrt(sum(s$num)/(sum(s$n) - nrow(s)))
psd*sqrt(sum(1/s$n))
}
### gtsummary
iris %>%
select(names(iris))%>%
tbl_summary(
by = Species,
statistic = all_continuous() ~ "{mean} ± {sem}"
) %>%
add_p(
test = all_continuous() ~ "aov",
pvalue_fun = function(x) style_pvalue(x, digits = 3)
) %>%
add_stat(fns = all_continuous() ~ PSE) %>%
add_stat(fns = all_continuous() ~ my_ES_test) %>%
modify_header(label = "**Size**", p.value = "**p-value**", add_stat_1 = "**PSE**", add_stat_2 = "**\U03B7\U00B2**") %>%
modify_footnote(add_stat_1 = "Pooled Standard Error", abbreviation = FALSE) %>%
modify_fmt_fun( c(add_stat_1, add_stat_2) ~ purrr::partial(style_sigfig, digits = 5)) %>%
as_kable()
| Size | setosa, N = 50 | versicolor, N = 50 | virginica, N = 50 | p-value | PSE | ?² |
|---|---|---|---|---|---|---|
| Sepal.Length | 5.01 ± 0.05 | 5.94 ± 0.07 | 6.59 ± 0.09 | <0.001 | 0.12610 | 0.61871 |
| Sepal.Width | 3.43 ± 0.05 | 2.77 ± 0.04 | 2.97 ± 0.05 | <0.001 | 0.08321 | 0.40078 |
| Petal.Length | 1.46 ± 0.02 | 4.26 ± 0.07 | 5.55 ± 0.08 | <0.001 | 0.10541 | 0.94137 |
| Petal.Width | 0.25 ± 0.01 | 1.33 ± 0.03 | 2.03 ± 0.04 | <0.001 | 0.05013 | 0.92888 |
Created on 2022-04-24 by the reprex package (v2.0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daniel D. Sjoberg |
