Skip to contents

[Questioning]

Takes in a a n x 3 data frame and calculates the chi squared statistic, the p-value of this and the odds ratio.

First column must be the category names. Second column must be observed values (a count) Third column must be the expected values (also a count, but can be weights or probabilities)

Usage

chi_and_odds(m)

Arguments

m

A n x 3 data frame or tibble. Should have variables in a particular order: category_name, observed_count, expected_count

Examples

df <- tibble::tibble(field = c('Yes', 'No') , observed = c(34, 88), expected = c(707, 1021))
chi_and_odds(df)
#> # A tibble: 2 × 3
#>   field observed expected
#>   <chr>    <dbl>    <dbl>
#> 1 Yes         34      707
#> 2 No          88     1021
#> 
#> $chisq.test.result
#> 
#> 	Chi-squared test for given probabilities
#> 
#> data:  observed
#> X-squared = 8.5886, df = 1, p-value = 0.003383
#> 
#> 
#> $odds_ratio
#>    observed
#> 1 0.5579594
#> 
#> $odds_interpretation
#> [1] "Yes is 1.79224520366423 times less likely to happen in observed than in expected"
#> 

# or from clipboard
df <- clipr::read_clip_tbl()
#> Error in error_interactive(): To run write_clip() in non-interactive mode, either call write_clip() with allow_non_interactive = TRUE, or set the environment variable CLIPR_ALLOW=TRUE
chi_and_odds(df)
#> # A tibble: 2 × 3
#>   field observed expected
#>   <chr>    <dbl>    <dbl>
#> 1 Yes         34      707
#> 2 No          88     1021
#> 
#> $chisq.test.result
#> 
#> 	Chi-squared test for given probabilities
#> 
#> data:  observed
#> X-squared = 8.5886, df = 1, p-value = 0.003383
#> 
#> 
#> $odds_ratio
#>    observed
#> 1 0.5579594
#> 
#> $odds_interpretation
#> [1] "Yes is 1.79224520366423 times less likely to happen in observed than in expected"
#>