MyNixOS website logo
Description

Transforms Contingency Tables to Data Frames, and Analyses Them.

Contingency Tables are a pain to work with when you want to run regressions. This package takes them, flattens them into a long data frame, so you can more easily analyse them! As well, you can calculate other related statistics. All of this is done so in a 'tidy' manner, so it should tie in nicely with 'tidyverse' series of packages.

flatr

Overview

flatr is a package designed to make the analysis of contingency tables easier.

Contingency tables are a popular means of presenting categorical data in textbooks, as they take up very little space, while still allowing to present all the data. However, this means makes it tough to run analysis on them. flatr helps ease this pain by turning i × j × k contingency tables into "tidy" data.

Functions

  • flatten_ct() takes an i × j × k contingency table, and turns it into a tibble.

  • goodness_of_fit() takes a logistic or probit regression model, and does a χ2 Goodness of Fit Test. The test statistic is one of:

  • Chi Squared Equation

  • G Squared Equation

Tidy Data

flatr is designed to work with the tidyverse series of packages. Tidy data is data in a "long" format, where each variable has its own column.

Usage

lung_cancer
#> , , City = Beijing
#> 
#>        Lung
#> Smoking   Y   N
#>       Y 126 100
#>       N  35  61
#> 
#> , , City = Shanghai
#> 
#>        Lung
#> Smoking   Y   N
#>       Y 908 688
#>       N 497 807
#> 
#> , , City = Shenyang
#> 
#>        Lung
#> Smoking   Y   N
#>       Y 913 747
#>       N 336 598
#> 
#> , , City = Nanjing
#> 
#>        Lung
#> Smoking   Y   N
#>       Y 235 172
#>       N  58 121
#> 
#> , , City = Harbin
#> 
#>        Lung
#> Smoking   Y   N
#>       Y 402 308
#>       N 121 215
#> 
#> , , City = Zhengzhou
#> 
#>        Lung
#> Smoking   Y   N
#>       Y 182 156
#>       N  72  98
#> 
#> , , City = Taiyuan
#> 
#>        Lung
#> Smoking  Y  N
#>       Y 60 99
#>       N 11 43
#> 
#> , , City = Nanchang
#> 
#>        Lung
#> Smoking   Y  N
#>       Y 104 89
#>       N  21 36

lung_tidy <- flatten_ct(lung_cancer)
lung_tidy
#> # A tibble: 8,419 x 3
#>    Smoking   Lung    City
#>     <fctr> <fctr>  <fctr>
#>  1       Y      Y Beijing
#>  2       Y      Y Beijing
#>  3       Y      Y Beijing
#>  4       Y      Y Beijing
#>  5       Y      Y Beijing
#>  6       Y      Y Beijing
#>  7       Y      Y Beijing
#>  8       Y      Y Beijing
#>  9       Y      Y Beijing
#> 10       Y      Y Beijing
#> # ... with 8,409 more rows

lung_logit <- glm(Lung ~ Smoking + City, family = binomial, data = lung_tidy)
goodness_of_fit(model = lung_logit, response = "Lung", type = "Chisq")
#> 
#> Chi-squared Goodness of Fit Test 
#> 
#> model: lung_logit 
#> Chi-squared = 5.19987, df = 7, p-value = 0.63559

lung_tidy %>% 
  glm(
    Lung ~ Smoking + City
    ,family = binomial(link = "probit")
    ,data = .
  ) %>% 
  goodness_of_fit(response = "Lung", type = "Gsq")
#> 
#> G-squared Goodness of Fit Test 
#> 
#> model: . 
#> G-squared = 5.15871, df = 7, p-value = 0.6406
Metadata

Version

0.1.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows