library("tidyverse")
library("psych")
library("rstatix")
library("gt")Lab 8.1 Chi Square
Intro
For our Chi Square Lab 8.2 we are going to use the cpr.csv data set (found under the Files tab) to analysis the results of a simulated study involving the treatment of cardiac arrest. Our data set has two variables group and outcome. First responder crews were randomly assigned to a control group or a treatment group, the control group continued to provide care as they normally would for sudden witnessed cardiac arrest and the treatment group was issued and trained on CPR devices to improve the quality of their CPR (hypothetically). Outcome data was grouped into patients that survived or died. As all of the provided data is categorical, we will use a Chi Square test to help us analyze the data.
Conditions for conducting a Chi-Square test
Variables are categorical
Observations are independent
Groups of the categorical variables are mutually exclusive
Expected frequency of each group is at least 5
Conducting the test
cpr.data <- read_csv("cpr.csv")
library(ggplot2)
ggplot(cpr.data, aes(x = group, fill = outcome)) +
geom_bar(position = "dodge") +
labs(
title = "Effectiveness of CPR",
x = "Group Variable",
y = "Count",
fill = "Outcome Variable"
) +
theme_minimal()chi2 <- cpr.data %>% table() %>% chisq_test() %>% gt()
chi2| n | statistic | p | df | method | p.signif |
|---|---|---|---|---|---|
| 150 | 6.240755 | 0.0125 | 1 | Chi-square test | * |
cpr.data %>% table() %>% chisq_test() %>% chisq_descriptives() %>% gt()| group | outcome | observed | prop | row.prop | col.prop | expected | resid | std.resid |
|---|---|---|---|---|---|---|---|---|
| control | died | 57 | 0.3800000 | 0.7125 | 0.6195652 | 49.06667 | 1.132563 | 2.666187 |
| treatment | died | 35 | 0.2333333 | 0.5000 | 0.3804348 | 42.93333 | -1.210761 | -2.666187 |
| control | survived | 23 | 0.1533333 | 0.2875 | 0.3965517 | 30.93333 | -1.426403 | -2.666187 |
| treatment | survived | 35 | 0.2333333 | 0.5000 | 0.6034483 | 27.06667 | 1.524889 | 2.666187 |
cpr.data %>% table() %>% pairwise_chisq_gof_test() %>% gt()| n | group1 | group2 | statistic | p | df | p.adj | p.adj.signif |
|---|---|---|---|---|---|---|---|
| 2 | grp1 | grp2 | 5.260870 | 0.021800 | 1 | 0.109000 | ns |
| 2 | grp1 | grp3 | 14.450000 | 0.000144 | 1 | 0.000864 | *** |
| 2 | grp1 | grp4 | 5.260870 | 0.021800 | 1 | 0.109000 | ns |
| 2 | grp2 | grp3 | 2.482759 | 0.115000 | 1 | 0.345000 | ns |
| 2 | grp2 | grp4 | 0.000000 | 1.000000 | 1 | 1.000000 | ns |
| 2 | grp3 | grp4 | 2.482759 | 0.115000 | 1 | 0.345000 | ns |