Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update surveys gt #316

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Imports:
ggalluvial,
ggplot2 (>= 3.0.0),
ggspatial,
gtsummary,
gtsummary (>= 1.7.0),
glue,
here,
janitor,
Expand Down
3 changes: 0 additions & 3 deletions inst/rmarkdown/templates/ajs_outbreak/skeleton/skeleton.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ knitr::opts_chunk$set(echo = FALSE, # hide all code chunks in output



## set default NA to - in output, define figure width/height
options(knitr.kable.NA = "-")

# Ensures the package "pacman" is installed
if (!require("pacman")) {
install.packages("pacman") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ knitr::opts_chunk$set(echo = FALSE, # hide all code chunks in output



## set default NA to - in output, define figure width/height
options(knitr.kable.NA = "-")

# Ensures the package "pacman" is installed
if (!require("pacman")) {
install.packages("pacman") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ knitr::opts_chunk$set(echo = FALSE, # hide all code chunks in output



## set default NA to - in output, define figure width/height
options(knitr.kable.NA = "-")

# Ensures the package "pacman" is installed
if (!require("pacman")) {
install.packages("pacman") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ knitr::opts_chunk$set(echo = FALSE, # hide all code chunks in output



## set default NA to - in output, define figure width/height
options(knitr.kable.NA = "-")

# Ensures the package "pacman" is installed
if (!require("pacman")) {
install.packages("pacman") }
Expand Down
66 changes: 31 additions & 35 deletions inst/rmarkdown/templates/mortality/skeleton/skeleton.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ knitr::opts_chunk$set(echo = FALSE, error = TRUE, warning = FALSE, message = FAL
fig.width = 6*1.25, fig.height = 6)


## set default NA to - in output, define figure width/height
options(knitr.kable.NA = "-")

# Ensures the package "pacman" is installed
if (!require("pacman")) {
Expand Down Expand Up @@ -1649,13 +1647,11 @@ Note the possibility of negative confidence intervals if there are low counts
## weighted counts and proportion of dead
death_props <- survey_design %>%
select(died) %>%
tbl_svysummary() %>%
tbl_svysummary(type = died ~ "categorical") %>%
## add the weighted total
add_n() %>%
## add in confidence intervals

## add in deff

add_ci() %>%
## modify the column headers
modify_header(
list(
Expand All @@ -1678,10 +1674,9 @@ CMR <- survey_design %>%


During the recall period the weighted number and proportion of deaths in the
population was `r inline_text(death_props, variable = "died", column = "stat_0")`,
population was `r inline_text(death_props, variable = "died", column = "stat_0", level = "TRUE")`,
with a weighted confidence interval of
`r #inline_text(death_props, variable = "died", column = "add_stat_1")`, and a
design effect of `r #inline_text(death_props, variable = "died", column = "add_stat_2")`.
`r inline_text(death_props, variable = "died", column = "ci_stat_0", level = "TRUE")`.
This is a crude mortality rate of `r CMR` deaths per 10000 person-days.


Expand Down Expand Up @@ -1709,9 +1704,7 @@ death_props_strat <- survey_design %>%
## add the weighted total
add_n() %>%
## add in confidence intervals

## add in deff

add_ci() %>%
## modify the column headers
modify_header(
list(
Expand All @@ -1737,16 +1730,14 @@ CMR_strat <- survey_design %>%
```


In District A the weighted number of deaths was
`r inline_text(death_props_strat, variable = "died", column = "stat_1")`,
which gives a weighted proportion of
`r #death_props_strat %>% pull("district_a ci")`, and a design effect of
`r #death_props_strat %>% pull("district_a deff") %>% round(digits = 3)`.
In comparison, the weighted number of deaths in District B was
`r inline_text(death_props_strat, variable = "died", column = "stat_2")`,
the weighted proportion was
`r #death_props_strat %>% pull("district_b ci")` and the design effect was
`r #death_props_strat %>% pull("district_b deff") %>% round(digits = 3)`.
In District A the weighted number and proportion of deaths was
`r inline_text(death_props_strat, variable = "died", column = "stat_1")`,
with a weighted confidence interval of
`r inline_text(death_props_strat, variable = "died", column = "ci_stat_1")`.
In comparison, the weighted number and proportion of deaths in District B was
`r inline_text(death_props_strat, variable = "died", column = "stat_2")`,
the weighted confidence interval was
`r inline_text(death_props_strat, variable = "died", column = "ci_stat_2")`.
The crude mortality rate in District A was
`r CMR_strat %>% filter(health_district == "district_a") %>% pull(Mortality)`
deaths per 10000 person-days and in District B was
Expand Down Expand Up @@ -1861,8 +1852,9 @@ cause_of_death_prop <- survey_design %>%
# proportions only among those who died
filter(died) %>%
select(cause) %>%
tbl_svysummary()
tbl_svysummary() %>%
## add ci
add_ci()

## weighted cause-specific mortality ratios
cause_of_death_mort <- survey_design %>%
Expand Down Expand Up @@ -1893,7 +1885,7 @@ cause_of_death_prop %>%
## modify the column headers
modify_header(
list(
stat_0 ~ "**Deaths \n N={round(N, digits = 0)}**",
stat_0 ~ "Deaths \n N={round(N, digits = 0)}",
mortality ~ "**Mortality per 10,000 person/days (95% CI)**"
)
) %>%
Expand Down Expand Up @@ -1929,7 +1921,7 @@ survey_design %>%
select(cause, health_district) %>%
tbl_svysummary(by = health_district) %>%
## add in confidence intervals

add_ci() %>%
## modify the column headers
modify_header(
list(
Expand Down Expand Up @@ -2033,6 +2025,10 @@ by age group.
Note that low counts or short observation times may lead to a confidence interval
that crosses zero (i.e. negative) for mortality ratios. These should be interpreted
as if no deaths or recoded to zero (impossible to have negative deaths).
Note that confidence intervals would make this table crowded; in addition
groups with zero counts result in an error, from the original survey package.
If you would like to have confidence intervals we suggest only showing a subset
of the levels or groups in your data.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

```{r weighted_death_cause_by_age, warning = FALSE}
Expand All @@ -2042,7 +2038,7 @@ survey_design %>%
select(cause, age_group) %>%
tbl_svysummary(by = age_group) %>%
## add in confidence intervals

# add_ci() %>%
## modify the column headers
modify_spanning_header(
all_stat_cols() ~ "**Age group (years)**"
Expand Down Expand Up @@ -2079,7 +2075,7 @@ survey_design %>%
select(cause, sex) %>%
tbl_svysummary(by = sex) %>%
## add in confidence intervals

add_ci() %>%
## make variable names bold
bold_labels() %>%
# change to flextable format
Expand Down Expand Up @@ -2214,7 +2210,7 @@ ggplot(data = flow_table,
aes(axis1 = startcause, axis2 = endcause,
y = n)) +
## define x axis limits and labels
scale_x_discrete(limits = c("Start\ncause", "End\ncause")) +
scale_x_discrete(limits = c("Start\ncause", "End\ncause"), expand = c(0,0)) +
## colour lines by sex
geom_alluvium(aes(fill = sex)) +
## fill in the label boxes grey
Expand Down Expand Up @@ -2313,7 +2309,7 @@ survey_design %>%
select(cause_illness) %>%
tbl_svysummary() %>%
## add in confidence intervals

add_ci() %>%
## make variable names bold
bold_labels() %>%
# change to flextable format
Expand Down Expand Up @@ -2344,7 +2340,7 @@ survey_design %>%
select(cause_illness, age_group) %>%
tbl_svysummary(by = age_group) %>%
## add in confidence intervals

add_ci() %>%
## make variable names bold
bold_labels() %>%
# change to flextable format
Expand Down Expand Up @@ -2375,7 +2371,7 @@ survey_design %>%
select(cause_illness, sex) %>%
tbl_svysummary(by = sex) %>%
## add in confidence intervals

add_ci() %>%
## make variable names bold
bold_labels() %>%
# change to flextable format
Expand Down Expand Up @@ -2408,7 +2404,7 @@ survey_design %>%
select(no_care_illness) %>%
tbl_svysummary() %>%
## add in confidence intervals

add_ci() %>%
## make variable names bold
bold_labels() %>%
# change to flextable format
Expand Down Expand Up @@ -2440,7 +2436,7 @@ survey_design %>%
select(violent_episodes_number, violence_nature, uniform, place_violence) %>%
tbl_svysummary() %>%
## add in confidence intervals

add_ci() %>%
## make variable names bold
bold_labels() %>%
# change to flextable format
Expand Down Expand Up @@ -2473,7 +2469,7 @@ survey_design %>%
select(fever_now, care_fever) %>%
tbl_svysummary() %>%
## add in confidence intervals

add_ci() %>%
## make variable names bold
bold_labels() %>%
# change to flextable format
Expand Down Expand Up @@ -2504,7 +2500,7 @@ survey_design %>%
select(reason_no_care) %>%
tbl_svysummary() %>%
## add in confidence intervals

add_ci() %>%
## make variable names bold
bold_labels() %>%
# change to flextable format
Expand Down
46 changes: 29 additions & 17 deletions inst/rmarkdown/templates/nutrition/skeleton/skeleton.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ For help installing packages, please visit https://r4epis.netlify.com/welcome
knitr::opts_chunk$set(echo = FALSE, error = TRUE, fig.width = 6*1.25, fig.height = 6)


## set default NA to - in output, define figure width/height
options(knitr.kable.NA = "-")

# Ensures the package "pacman" is installed
if (!require("pacman")) {
install.packages("pacman") }
Expand Down Expand Up @@ -908,9 +905,9 @@ study_data_cleaned <- study_data_cleaned %>%
mutate(
across(
.cols = c(stunting_haz,
moderate_stunting_haz,
severe_stunting_haz), # select the indicator variables
.funs = ~if_else(!is.na(flen) & flen == 1, NA, .))) # NA if flagged, else leave as is
moderate_stunting_haz,
severe_stunting_haz), # select the indicator variables
.fns = ~if_else(!is.na(flen) & flen == 1, NA, .))) # NA if flagged, else leave as is



Expand Down Expand Up @@ -1996,7 +1993,9 @@ overall <- survey_design %>%
select(all_of(MUAC)) %>%
## only show the row with TRUE
tbl_svysummary(missing = "no",
value = everything() ~ TRUE)
value = everything() ~ TRUE) %>%
## add confidence intervals
add_ci()

age_strat <- survey_design %>%
## tabulate multiple variables with same values
Expand All @@ -2013,7 +2012,8 @@ tbl_merge(list(overall, age_strat)) %>%
list(
## rename the spanning header
## you can see what the columns are called by putting in an object and inspecting table_body
stat_0_1 ~ "**Overall**",
c(stat_0_1,
ci_stat_0_1) ~ "**Overall**",
c(stat_1_2,
stat_2_2,
stat_3_2,
Expand Down Expand Up @@ -2052,7 +2052,9 @@ overall <- survey_design %>%
select(all_of(MUAC)) %>%
## only show the row with TRUE
tbl_svysummary(missing = "no",
value = everything() ~ TRUE)
value = everything() ~ TRUE) %>%
## add confidence intervals
add_ci()

age_strat <- survey_design %>%
## only keep children less than 87cm
Expand All @@ -2063,15 +2065,16 @@ age_strat <- survey_design %>%
tbl_svysummary(missing = "no",
value = everything() ~ TRUE,
## stratify by age group
by = age_group)
by = age_group)

## combine the overall and stratified tables
tbl_merge(list(overall, age_strat)) %>%
modify_spanning_header(
list(
## rename the spanning header
## you can see what the columns are called by putting in an object and inspecting table_body
stat_0_1 ~ "**Overall**",
c(stat_0_1,
ci_stat_0_1) ~ "**Overall**",
c(stat_1_2,
stat_2_2,
stat_3_2,
Expand Down Expand Up @@ -2110,7 +2113,9 @@ overall <- survey_design %>%
select(all_of(HAZ)) %>%
## only show the row with TRUE
tbl_svysummary(missing = "no",
value = everything() ~ TRUE)
value = everything() ~ TRUE) %>%
## add confidence intervals
add_ci()

age_strat <- survey_design %>%
## tabulate multiple variables with same values
Expand All @@ -2127,7 +2132,8 @@ tbl_merge(list(overall, age_strat)) %>%
list(
## rename the spanning header
## you can see what the columns are called by putting in an object and inspecting table_body
stat_0_1 ~ "**Overall**",
c(stat_0_1,
ci_stat_0_1) ~ "**Overall**",
c(stat_1_2,
stat_2_2,
stat_3_2,
Expand Down Expand Up @@ -2165,7 +2171,9 @@ overall <- survey_design %>%
select(all_of(WAZ)) %>%
## only show the row with TRUE
tbl_svysummary(missing = "no",
value = everything() ~ TRUE)
value = everything() ~ TRUE) %>%
## add confidence intervals
add_ci()

age_strat <- survey_design %>%
## tabulate multiple variables with same values
Expand All @@ -2182,7 +2190,8 @@ tbl_merge(list(overall, age_strat)) %>%
list(
## rename the spanning header
## you can see what the columns are called by putting in an object and inspecting table_body
stat_0_1 ~ "**Overall**",
c(stat_0_1,
ci_stat_0_1) ~ "**Overall**",
c(stat_1_2,
stat_2_2,
stat_3_2,
Expand Down Expand Up @@ -2221,7 +2230,9 @@ overall <- survey_design %>%
select(all_of(WHZ)) %>%
## only show the row with TRUE
tbl_svysummary(missing = "no",
value = everything() ~ TRUE)
value = everything() ~ TRUE) %>%
## add confidence intervals
add_ci()

age_strat <- survey_design %>%
## tabulate multiple variables with same values
Expand All @@ -2238,7 +2249,8 @@ tbl_merge(list(overall, age_strat)) %>%
list(
## rename the spanning header
## you can see what the columns are called by putting in an object and inspecting table_body
stat_0_1 ~ "**Overall**",
c(stat_0_1,
ci_stat_0_1) ~ "**Overall**",
c(stat_1_2,
stat_2_2,
stat_3_2,
Expand Down
Loading