Skip to content

Improve error message in ard_stats_prop_test() for a variable that is all NA #294

@ddsjoberg

Description

@ddsjoberg

The error message says that the by variable must have two levels (which it does), so the error we're returning isn't great.

I suspect this is because we first remove all the NA values, which then does create a data frame where the by variable does indeed have fewer than 2 levels.

Related, the prop.test() doesn't require 2 levels for the stratifying variable, and we should generalize the the function to allow for a single group and 2 or more groups. The returned statistics will depend on the number of levels, so we'll need to account for that as well.

mtcars |> 
  dplyr::mutate(mpg = NA) |> 
  cardx::ard_stats_prop_test(
    variable = mpg,
    by = am
  ) |> 
  cards::print_ard_conditions()
#> The following errors were returned during `print_ard_conditions()`:
#> ✖ For variable `mpg` (`am`) and "estimate", "estimate1", "estimate2",
#>   "statistic", "p.value", "parameter", "conf.low", "conf.high", "method",
#>   "alternative", "p", "conf.level", and "correct" statistics: The `by` column
#>   must have exactly 2 levels. The levels are

Created on 2025-05-03 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions