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

Fix issues with different nlevels in groups, enable "n" argument #368

Merged
merged 5 commits into from
May 13, 2023

Conversation

strengejacke
Copy link
Member

library(report)
wilson2017 <- readxl::read_excel("c:/Users/mail/Downloads/pmed.1002479.s001.xls", sheet = "data")

wilson2017 |>
  report::report_sample(
    group_by = "ethnicgrp",
    select = c("gender", "partners", "msm")
  )
#> # Descriptive Statistics
#> 
#> Variable                | Asian/ Asian British (n=127) | Black/ Black British (n=191) | Mixed/ Multiple ethnicity (n=188) | Other (n=29) | White/ White British (n=1528) | Total (n=2063)
#> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#> gender [Female], %      |                         55.1 |                         57.6 |                              63.3 |         48.3 |                          58.9 |           58.8
#> gender [Male], %        |                         44.9 |                         42.4 |                              36.2 |         48.3 |                          41.0 |           41.0
#> gender [Transgender], % |                          0.0 |                          0.0 |                               0.5 |          3.4 |                           0.1 |            0.2
#> partners [1], %         |                         41.7 |                         36.1 |                              33.5 |         37.9 |                          26.8 |           29.4
#> partners [10+], %       |                          6.3 |                          5.2 |                               6.9 |         10.3 |                           9.6 |            8.8
#> partners [2], %         |                         17.3 |                         24.6 |                              16.5 |         13.8 |                          17.1 |           17.7
#> partners [3], %         |                          9.4 |                         11.0 |                              17.0 |          6.9 |                          14.3 |           13.8
#> partners [4], %         |                         11.0 |                          8.9 |                               9.0 |          3.4 |                          10.9 |           10.4
#> partners [5], %         |                          5.5 |                          8.4 |                               8.0 |         17.2 |                           8.7 |            8.5
#> partners [6], %         |                          5.5 |                          3.1 |                               3.7 |          3.4 |                           6.0 |            5.5
#> partners [7], %         |                          0.0 |                          1.6 |                               2.7 |          6.9 |                           3.3 |            2.9
#> partners [8], %         |                          2.4 |                          1.0 |                               1.6 |          0.0 |                           2.1 |            1.9
#> partners [9], %         |                          0.8 |                          0.0 |                               1.1 |          0.0 |                           1.2 |            1.1
#> msm [99], %             |                          1.6 |                          1.6 |                               0.0 |          3.4 |                           1.1 |            1.1
#> msm [msm], %            |                         14.2 |                          5.8 |                               8.5 |         17.2 |                          13.9 |           12.7
#> msm [other], %          |                         84.3 |                         92.7 |                              91.5 |         79.3 |                          85.0 |           86.2

wilson2017 |>
  report::report_sample(
    select = c("gender", "partners", "msm", "ethnicgrp"),
    n = TRUE
  )
#> # Descriptive Statistics
#> 
#> Variable                                    |    Summary
#> --------------------------------------------------------
#> gender [Female], %, n                       | 58.8, 1213
#> gender [Male], %, n                         |  41.0, 846
#> gender [Transgender], %, n                  |     0.2, 4
#> partners [1], %, n                          |  29.4, 606
#> partners [10+], %, n                        |   8.8, 181
#> partners [2], %, n                          |  17.7, 365
#> partners [3], %, n                          |  13.8, 285
#> partners [4], %, n                          |  10.4, 215
#> partners [5], %, n                          |   8.5, 176
#> partners [6], %, n                          |   5.5, 113
#> partners [7], %, n                          |    2.9, 60
#> partners [8], %, n                          |    1.9, 40
#> partners [9], %, n                          |    1.1, 22
#> msm [99], %, n                              |    1.1, 23
#> msm [msm], %, n                             |  12.7, 262
#> msm [other], %, n                           | 86.2, 1778
#> ethnicgrp [Asian/ Asian British], %, n      |   6.2, 127
#> ethnicgrp [Black/ Black British], %, n      |   9.3, 191
#> ethnicgrp [Mixed/ Multiple ethnicity], %, n |   9.1, 188
#> ethnicgrp [Other], %, n                     |    1.4, 29
#> ethnicgrp [White/ White British], %, n      | 74.1, 1528

report::report_sample(iris, n = TRUE)
#> # Descriptive Statistics
#> 
#> Variable                   |          Summary
#> ---------------------------------------------
#> Mean Sepal.Length (SD), n  | 5.84 (0.83), 150
#> Mean Sepal.Width (SD), n   | 3.06 (0.44), 150
#> Mean Petal.Length (SD), n  | 3.76 (1.77), 150
#> Mean Petal.Width (SD), n   | 1.20 (0.76), 150
#> Species [setosa], %, n     |         33.3, 50
#> Species [versicolor], %, n |         33.3, 50
#> Species [virginica], %, n  |         33.3, 50

Created on 2023-04-28 with reprex v2.0.2

@strengejacke
Copy link
Member Author

We may think about a different layout of n (extra column?), but I think none of the above examples were previously working, so this PR is actually fixing some bugs.

@strengejacke
Copy link
Member Author

strengejacke commented Apr 28, 2023

  1. Issue: When grouping, and a variable had no values in one group (like "gender" -> "transgender" above), we had different numbers of rows as zero-rows were omitted.

  2. Issue: n did not work for factors.

@rempsyc
Copy link
Member

rempsyc commented May 6, 2023

Cool, thank you @strengejacke!

I updated the snapshots but now am getting the following on the tests:

── Error ('test-report_performance.R:105:3'): report_performance Bayesian) ─────
Error in `eval(code, test_env)`: cannot change value of locked binding for 'stan_glmer'

That looks like a false positive because it didn't do that when you ran your tests and I didn't change anything but the snapshot, and it has nothing to do with the current changes. Should I just squash and merge the change and hope this error goes away by itself?

@rempsyc
Copy link
Member

rempsyc commented May 6, 2023

Is this the same as #367?

@codecov-commenter
Copy link

Codecov Report

Merging #368 (74d92fb) into main (43faa6c) will increase coverage by 4.41%.
The diff coverage is 73.68%.

❗ Current head 74d92fb differs from pull request most recent head 1470444. Consider uploading reports for the commit 1470444 to get more accurate results

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##             main     #368      +/-   ##
==========================================
+ Coverage   67.31%   71.72%   +4.41%     
==========================================
  Files          46       46              
  Lines        3246     3254       +8     
==========================================
+ Hits         2185     2334     +149     
+ Misses       1061      920     -141     
Impacted Files Coverage Δ
R/report_sample.R 88.34% <73.68%> (-0.04%) ⬇️

... and 7 files with indirect coverage changes

@rempsyc rempsyc merged commit 28db1fa into main May 13, 2023
@rempsyc rempsyc deleted the fix_report_sample_issues branch May 13, 2023 17:25
@rempsyc
Copy link
Member

rempsyc commented May 13, 2023

Thanks for the PR @strengejacke ! :)

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 this pull request may close these issues.

3 participants