Skip to contents

Test input data using Chi-squared or Fisher's exact test as appropriate.

Usage

chsqfish(...)

Arguments

...

A vector, matrix, data frame or any valid input to chisq.test

Value

A list containing the observed and expected values and the result of either chisq.test() or fisher.test(), as appropriate.

Details

Uses chisq.test() to calculate expected values and then applies Chi-squared test if all expected values are 5 or greater, otherwise applies fisher.test().

Examples

(t <- bernoulli_data(2, 50, c(0.6, 0.4)) |>
  contingency_table(dv, iv, .rownames = TRUE))
#>    0  1
#> a 13 37
#> b 32 18

t |> chsqfish()
#> $observed
#>    0  1
#> a 13 37
#> b 32 18
#> 
#> $expected
#>      0    1
#> a 22.5 27.5
#> b 22.5 27.5
#> 
#> $test
#> 
#> 	Pearson's Chi-squared test with Yates' continuity correction
#> 
#> data:  t
#> X-squared = 13.091, df = 1, p-value = 0.0002967
#> 
#> 
#> attr(,"class")
#> [1] "chsqfish"

(t <- bernoulli_data(3, 10, c(0.8, 0.5, 0.2)) |>
  contingency_table(dv, iv, .rownames = TRUE))
#>   1 0
#> a 9 1
#> b 7 3
#> c 1 9

t |> chsqfish()
#> Warning: Chi-squared approximation may be incorrect
#> $observed
#>   1 0
#> a 9 1
#> b 7 3
#> c 1 9
#> 
#> $expected
#>          1        0
#> a 5.666667 4.333333
#> b 5.666667 4.333333
#> c 5.666667 4.333333
#> 
#> $test
#> 
#> 	Fisher's Exact Test for Count Data with simulated p-value (based on
#> 	2000 replicates)
#> 
#> data:  chsq$observed
#> p-value = 0.001999
#> alternative hypothesis: two.sided
#> 
#> 
#> attr(,"class")
#> [1] "chsqfish"

rm(t)