Sum of a column in tbl_summary? - r

I'm trying to create a table using tbl_summary() that contains sums of columns (a sum of the correct test scores and incorrect test scores), however it seems to keep treating my continuous variables as categorical?
I have tried specifying the type as continuous with no luck.
What I'm aiming for:
library(gtsummary)
library(tidyverse)
test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
"correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
"incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))
output <- test %>%
group_by(With_assistant) %>%
summarize(
total_correct=sum(correct_answers, na.rm=TRUE),
total_incorrect=(sum(incorrect_answers, na.rm=TRUE))
)
output
Table_1
I've tried the below:
library(gtsummary)
library(tidyverse)
test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
"correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
"incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))
output <- test %>%
tbl_summary(
by = With_assistant,
statistic = all_continuous() ~ {n}
)
Produces a count of each result as below:
Table_2
library(gtsummary)
library(tidyverse)
test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
"correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
"incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))
output <- test %>%
tbl_summary(
by = With_assistant,
type = c(correct_answers, incorrect_answers) ~ "continuous",
statistic = all_continuous() ~ {n},
percent = "column",
missing = "no"
) %>%
print(output)
Produces an error "Error: Error processing statistic argument for element 'Anatomy_yes'. Expecting a character as the passed value."

You almost made it. Check the documentation again.The type option since the default for numeric values less than 10 is categorical.
library(gtsummary)
library(tidyverse)
test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
"correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
"incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))
test %>%
tbl_summary(
by = With_assistant,
type = list(c(correct_answers, incorrect_answers) ~ "continuous")
)
Is this what you like to achieve?
With statistic option, specified for the sum:
test %>%
tbl_summary(
by = With_assistant,
type = list(c(correct_answers, incorrect_answers) ~ "continuous"),
statistic = list(c(correct_answers, incorrect_answers) ~ "{sum}")
)

Related

Tbl_Summary Chi Squared Testing

I have created a summary table for some data using tbl_summary(). The table sums numerical values in columns grouped by a factor, producing a 2x2 table.
I'd like to use tlb_summary()'s built-in statistics to calculate the p value using a Chi Squared test, however I can't tell if this is possible. Using the add_p() line gives me a p value for each row, which is incorrect:
library(gtsummary)
library(tidyverse)
test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
"correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
"incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))
test %>%
tbl_summary(
by = With_assistant,
type = list(c(correct_answers, incorrect_answers) ~ "continuous"),
statistic = list(c(correct_answers, incorrect_answers) ~ "{sum}")
) %>%
add_p(test = everything () ~ "chisq.test")
Would you be okay with adding the result of a chisq.test() to the tbl object as a note?
library(gtsummary)
library(tidyverse)
test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
"correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
"incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))
t <- test %>%
group_by(With_assistant) %>%
summarize(sum_cor = sum(correct_answers),
sum_inc =sum(incorrect_answers))
# A tibble: 2 × 3
With_assistant sum_cor sum_inc
<lgl> <dbl> <dbl>
1 FALSE 15 15
2 TRUE 20 14
chi<- chisq.test(t)
test %>%
tbl_summary(
by = With_assistant,
type = list(c(correct_answers, incorrect_answers) ~ "continuous"),
statistic = list(c(correct_answers, incorrect_answers) ~ "{sum}")
) %>%
modify_footnote(all_stat_cols() ~ paste0("Chi Square ", round(chi$statistic,2), " with p = ", round(chi$p.value, 2)))
1.37 is the real test statistic of your four numbers from the contingency table.

How to convert a list of tbl_regression objects to a single tbl_regression object?

I am trying to add significance stars to a gtsummary table that combines multiple models, but I'm getting an error that I don't know how to resolve.
library(gtsummary)
library(tidyverse)
# Create a list of tbl_regression objects
models <- c("disp", "disp + hp") %>%
map(
~ paste("vs", .x, sep = " ~ ") %>%
as.formula() %>%
glm(data = mtcars,
family = binomial(link = "logit")) %>%
tbl_regression(exponentiate = TRUE))
# Try to add significance stars
models %>%
add_significance_stars(
pattern = "{estimate}{stars}",
thresholds = c(0.001, 0.01, 0.05),
hide_ci = TRUE,
hide_p = TRUE,
hide_se = FALSE
)
#> Error: Error in argument 'x='. Expecting object of class 'tbl_regression', or 'tbl_uvregression'
It seems that the list models is not an object of class tbl_regression, and therefore cannot be passed to add_significance_stars(). How can I fix this problem?
Your models object is a list of gtsummary tables. Hence, as you did when you created the tables you have to use map to loop over the list to add significance stars to each table:
library(gtsummary)
library(tidyverse)
models %>%
map(
~ add_significance_stars(.x,
pattern = "{estimate}{stars}",
thresholds = c(0.001, 0.01, 0.05),
hide_ci = TRUE,
hide_p = TRUE,
hide_se = FALSE
)
)
As you indicate, models is a list of objects of class tbl_regression. You can just use lapply, like this:
models = lapply(models,
add_significance_stars,
pattern = "{estimate}{stars}",
thresholds = c(0.001, 0.01, 0.05),
hide_ci = TRUE,
hide_p = TRUE,
hide_se = FALSE
)
Or you could include the call to add_significance_stars() in the original map()

Add Chi test to plot_stackfrq in R

I need to add to the plot_stackfrq in R the Chi test, I didn't see any parameter of the function that accepts chi.test or similar.
This is my code:
library(tidyr)
library(data.table)
homes2 <- homes %>% mutate(rn = rowid(Educ_level)) %>%
pivot_wider(names_from = Educ_level, values_from = Insurance)
plot_stackfrq(homes2[,c("High Scholl","College","Elementary")],
geom.colors = c("YlOrBr"))
I could resolve it with plot_xtab, this did show me Chi test as I needed.
plot_xtab(homes2$Educ_label, homes2$Insurance, bar.pos = "stack", show.total = FALSE, margin = "row", coord.flip = TRUE, show.n = FALSE, show.summary = TRUE,
geom.colors = c("YlOrBr"))

labels order by month name chronically in r plotly [duplicate]

I'm using the plotly package in R to build an R Shiny dashboard. I want to order my pie chart in a custom order (non-alphabetic, non-descending/ascending order). For some reason I can't find how to achieve this.
Help would be highly appreciated!
# Get Manufacturer
mtcars$manuf <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
df <- mtcars %>%
group_by(manuf) %>%
summarize(count = n())
# Create custom order
customOrder <- c(df$manuf[12:22],df$manuf[1:11])
# Order data frame
df <- df %>% slice(match(customOrder, manuf))
# Create factor
df$manuf <- factor(df$manuf, levels = df[["manuf"]])
# Plot
df %>% plot_ly(labels = ~manuf, values = ~count) %>%
add_pie(hole = 0.6) %>%
layout(title = "Donut charts using Plotly", showlegend = F,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
Ok, the answer is apparently twofold. Firstly, there is an argument in plot_ly, asking to sort the data on values (default is TRUE) or work with the custom order. Change this to FALSE.
Then, secondly, the order (clockwise) is different from the order in the data frame. The pie starts in the top right corner, and continues counterclockwise.
Hence, the following solves the problem:
# Get Manufacturer
mtcars$manuf <- sapply(strsplit(rownames(mtcars), " "), "[[", 1)
df <- mtcars %>%
group_by(manuf) %>%
summarize(count = n())
# Create custom order
customOrder <- c(df$manuf[12:22],df$manuf[1:11])
# Adjust customOrder to deal with pie
customOrder <- c(customOrder[1],rev(customOrder[2:length(customOrder)]))
# Order data frame
df <- df %>% slice(match(customOrder, manuf))
# Create factor
df$manuf <- factor(df$manuf, levels = df[["manuf"]])
# Plot
df %>% plot_ly(labels = ~manuf, values = ~count, sort = FALSE) %>%
add_pie(hole = 0.6) %>%
layout(title = "Donut charts using Plotly", showlegend = F,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

GGVIS plot for logical matrix

I'm trying to plot a logical matrix similar to the question here, the difference is that I'm trying to do it using ggvis so that I can use the hover tool (the data has several thousand rows so I'd like to see row/column names when I hover over it). The following code worked for me with ggplot2.
library(reshape2)
library(ggplot2)
melted = melt(matrix)
ggplot(melted, aes(x = Var2, y = Var1)) +
geom_tile(aes(fill = value)) +
scale_file_manual(values = c("black", "red")) +
theme(axis.text.x = element_blank(), axis.text.y = element_blank()) +
coord_fixed(ratio = 1/10)
You can find an example for layer_rects here, I just made some adjustments.
Using the example for the linked question:
Load data
mm <- structure(c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE,
FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE,
FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE,
FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE,
FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE), .Dim = c(10L, 10L), .Dimnames = list(NULL,
c("n1", "n2", "n3", "n4", "n5", "n1.1", "n2.1", "n3.1", "n4.1",
"n5.1")))
Melt and change types
library(reshape2)
melted <- melt(mm)
melted$value <- as.numeric(melted$value)
melted$Var1 <- as.factor(melted$Var1)
Plot
melted %>%
ggvis(~Var2, ~Var1, fill = ~value) %>%
layer_rects(width = band(), height = band()) %>%
scale_nominal("x", padding = 0, points = FALSE) %>%
scale_nominal("y", padding = 0, points = FALSE)

Resources