Skip to content

Estimate hardcoded as character with prop.test() #1197

@andrewheiss

Description

@andrewheiss

When using model_parameters() with a prop.test() object, the reported Proportion column is hardcoded as a percentage and the underlying estimate is gone and un-extractable (unless I'm missing an argument that makes this work!)

This is happening because Proportion is explicitly built as a character string:

.extract_htest_prop <- function(model) {
out <- data.frame(
Proportion = paste(insight::format_value(model$estimate, as_percent = TRUE), collapse = " / "),
stringsAsFactors = FALSE
)

Here's a reproducible example:

library(parameters) table(penguins$sex) |> prop.test() #>  #> 1-sample proportions test with continuity correction #>  #> data: table(penguins$sex), null probability 0.5 #> X-squared = 0.012012, df = 1, p-value = 0.9127 #> alternative hypothesis: true p is not equal to 0.5 #> 95 percent confidence interval: #> 0.4406707 0.5504259 #> sample estimates: #> p  #> 0.4954955

That 0.4954955 (and other non-visible floating point digits) is the estimated proportion.

In model_parameters() it appears rounded, which is fine:

table(penguins$sex) |> prop.test() |> model_parameters() #> 1-sample proportions test #>  #> Proportion | 95% CI | Chi2(1) | Null_value | p #> -------------------------------------------------------- #> 49.55% | [0.44, 0.55] | 0.01 | 0.50 | 0.913 #>  #> Alternative hypothesis: true p is not equal to 0.5

In the underlying data frame, the Proportion column is a character, not a double:

table(penguins$sex) |> prop.test() |> model_parameters() |> as.data.frame() #> Proportion CI CI_low CI_high Chi2 df Null_value p #> 1 49.55% 0.95 0.4406707 0.5504259 0.01201201 1 0.5 0.9127271 #> Method Alternative #> 1 1-sample proportions test two.sided

This makes it tricky to extract that value and use it other places, like in plots:

library(tidyverse) table(penguins$sex) |> prop.test() |> model_parameters() |> mutate(estimate = parse_number(Proportion) / 100) |> ggplot(aes(x = estimate, y = "Proportion")) + geom_vline(xintercept = 0.5, color = "red") + geom_pointrange(aes(xmin = CI_low, xmax = CI_high))

↑ That works, but the value is actually only 0.4955, not the complete true value estimated by prop.test(), since parse_number() converted it from text to a number

It's possible to use broom::tidy() to extract values from htest objects like prop.test(), and it maintains the numeric-ness of the estimate column:

table(penguins$sex) |> prop.test() |> broom::tidy(conf.int = TRUE) |> ggplot(aes(x = estimate, y = "Proportion")) + geom_vline(xintercept = 0.5, color = "red") + geom_pointrange(aes(xmin = conf.low, xmax = conf.high))

But then it requires {broom} for this kind of plotting (while all other htest objects like t.test(), etc. can be extracted and plotted with CIs with model_parameters()—it's just prop.test() that has the character-only version of the estimate)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions