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: as_flex_table() error using p-value from svy.wald.test #1153

Closed
jmbarajas opened this issue Feb 16, 2022 · 3 comments · Fixed by #1173
Closed

Bug Report: as_flex_table() error using p-value from svy.wald.test #1153

jmbarajas opened this issue Feb 16, 2022 · 3 comments · Fixed by #1173
Assignees
Labels
bug 🐛 Something isn't working
Milestone

Comments

@jmbarajas
Copy link

Hi there,

I'm encountering an error when converting a tbl_svysummary object to a flextable using as_flex_table() when it has a p-value from the svy.wald.test. This seems to have broken between release 1.5.0 and 1.5.1. See reprexes below: the first code chunk works when outputting to html.

library(survey)
#> Loading required package: grid
#> Loading required package: Matrix
#> Loading required package: survival
#> 
#> Attaching package: 'survey'
#> The following object is masked from 'package:graphics':
#> 
#>     dotchart
library(gtsummary)

data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)

tbl_svysummary(dstrat,
               by = awards,
               include = c(api00, api99)) %>% 
  add_p(test = list(all_continuous() ~ "svy.wald.test"))
Characteristic No, N = 2,2361 Yes, N = 3,9581 p-value2
api00 641 (528, 743) 671 (588, 765) <0.001
api99 636 (524, 724) 625 (526, 727) <0.001
1 Median (IQR)
2 Wald test of independence for complex survey samples

Created on 2022-02-15 by the reprex package (v2.0.1)

But the second fails when converting.

library(survey)
#> Loading required package: grid
#> Loading required package: Matrix
#> Loading required package: survival
#> 
#> Attaching package: 'survey'
#> The following object is masked from 'package:graphics':
#> 
#>     dotchart
library(gtsummary)

data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)

tbl_svysummary(dstrat,
               by = awards,
               include = c(api00, api99)) %>% 
  add_p(test = list(all_continuous() ~ "svy.wald.test")) %>% 
  as_flex_table()
#> Error:
#> ! Assigned data `df_updated` must be compatible with existing data.
#> i Error occurred for column `p.value`.
#> x Can't convert <character[,1]> to <character>.
#> Cannot decrease dimensions.

Created on 2022-02-15 by the reprex package (v2.0.1)

The error doesn't occur with the default Wilcoxon rank sum test. The code appears to be choking because the p-value is stored as matrix rather than a single numeric value, but I don't know what change was introduced to cause the bug.

@ddsjoberg
Copy link
Owner

Thank you for letting me know! The p-value column was being saved as a nX1 matrix in the data frame, as opposed to a numeric vector. I'll need to fix that, but in the meantime, you can use the code below to get a working table.

library(gtsummary)

data(api, package = "survey")
dstrat <- survey::svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)

tbl <- 
  tbl_svysummary(
    dstrat,
    by = awards,
    include = c(api00, api99)
  ) %>% 
  add_p(test = list(all_continuous() ~ "svy.wald.test")) %>%
  # convert these columns to numeric vectors (somehow was stored as a nX1 matrix)
  modify_table_body(
    ~.x %>% dplyr::mutate(dplyr::across(c(statistic, p.value), as.numeric))
  )

as_flex_table(tbl)

image

Created on 2022-02-15 by the reprex package (v2.0.1)

@ddsjoberg ddsjoberg self-assigned this Feb 16, 2022
@ddsjoberg ddsjoberg added the bug 🐛 Something isn't working label Feb 16, 2022
@ddsjoberg ddsjoberg added this to the v1.5.3 milestone Feb 16, 2022
@ddsjoberg
Copy link
Owner

Issue submitted to the broom package tidymodels/broom#1081

@jmbarajas
Copy link
Author

Thanks for the workaround and upcoming fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants