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

Bug Report: {p_miss_unweighted} (and possibly {p_miss}) in tbl_svysummary Displays Proportion Instead of Percentage #2078

Closed
ken1th opened this issue Nov 22, 2024 · 1 comment · Fixed by #2079

Comments

@ken1th
Copy link

ken1th commented Nov 22, 2024

Bug: {p_miss_unweighted} in tbl_svysummary Displays Proportion Instead of Percentage

Description

When using {p_miss_unweighted} in the missing_stat argument of tbl_svysummary, it incorrectly displays the proportion (on a 0–1 scale) instead of the percentage (on a 0–100 scale). This inconsistency can cause confusion and misinterpretation.

Reproducible Example

library(gtsummary)
library(survey)

# Example data with missing values
data <- data.frame(
  var1 = c(1, 2, NA, 4, NA, 6),
  var2 = c("A", "B", "A", "B", NA, NA),
  weights = c(1, 2, 1, 1, 2, 1)
)

# Create survey design
design <- svydesign(id = ~1, data = data, weights = ~weights)

# Create tbl_svysummary
tbl <- tbl_svysummary(
  design,
  include = c(var1, var2),
  statistic = list(all_categorical() ~ "{n} ({p}%)"),
  missing = "ifany",
  missing_stat = "{N_miss_unweighted} ({p_miss_unweighted}%)"
)

# Display table
tbl

Observed Output

For the missing rows (e.g., Unknown), the table shows:

  • For var1: 2 (0.3%)
  • For var2: 2 (0.3%)

The percentage value (0.3%) appears to be incorrectly derived as if it were a proportion (0.003) formatted directly as a percentage without multiplying by 100.

image

Expected Output

The correct missing statistics should display percentages:

  • For var1: 2 (30.0%)
  • For var2: 2 (30.0%)

Proposed Fix

Update the internal logic for {p_miss_unweighted} to ensure the proportion is multiplied by 100 before formatting. This will align the behavior of {p_miss_unweighted} with other placeholders like {p}, which correctly display percentages.


This bug leads to significant misinterpretation in summary tables. Thank you for addressing this issue!

@ddsjoberg
Copy link
Owner

Thanks for the report and the reprex! This should be fixed in the dev version of the package.

library(gtsummary)

# Example data with missing values
data <- data.frame(
  var1 = c(1, 2, NA, 4, NA, 6),
  var2 = c("A", "B", "A", "B", NA, NA),
  weights = c(1, 2, 1, 1, 2, 1)
)

# Create survey design
design <- survey::svydesign(id = ~1, data = data, weights = ~weights)

# Create tbl_svysummary
tbl_svysummary(
  design,
  include = c(var1, var2),
  statistic = list(all_categorical() ~ "{n} ({p}%)"),
  missing = "ifany",
  missing_stat = "{N_miss_unweighted} ({p_miss_unweighted}%)"
) |> 
  as_kable()
Characteristic N = 8
var1
1 1 (20%)
2 2 (40%)
4 1 (20%)
6 1 (20%)
Unknown 2 (33%)
var2
A 2 (40%)
B 3 (60%)
Unknown 2 (33%)

Created on 2024-11-21 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants