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

library("tidyverse")
library("psych")
library("rstatix")
library("gt")

Conducting the test

NoteExercise 1

Create a new object with the cpr.csv data set and provide a brief explanation of the data (you can graph and/or create a frequency table of the data to provide support for your explanation)

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()

NoteAnswer:

Individuals who receive CPR are more likely to survive than individuals who do not.

NoteExercise 2

Create a new object called chi2 with the data object that was created in Exercise 1. Run a goodness of fit and test of independence [can be done together with the following syntax: chi2 <- [data] %>% table() %>% chisq_test() %>% gt(). Explain your results.

chi2 <- cpr.data %>% table() %>% chisq_test() %>% gt()

chi2
n statistic p df method p.signif
150 6.240755 0.0125 1 Chi-square test *
NoteAnswer:

There is a statistically significant association between the two categorical variables (e.g., receiving CPR and survival). Individuals who received CPR are more likely to survive than would be expected if there were no association. 0.0125 shows difference between the groups.

Exercise 3

Use the chisq_descriptives() function to get the expected values for each group. How does the expected values differ from the observed values?

Example: [data] %>% table() %>% chisq_test() %>% chisq_descriptives() %>% gt()

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
NoteAnswer:

In the control group, more people died and fewer survived than expected if CPR had no effect. However, in the treatment group, more people survived and fewer died than expected. This supports the conclusion that receiving CPR (treatment) is associated with higher survival, and not receiving it (control) is associated with higher mortality.

NoteExercise 4

For our post hoc assessment we will use the pairwise_chisq_gof_test() function to help us identify which groups are different. Conduct your post hoc assessment. Does it appear that the new treatment for cardiac arrest was helpful? Explain your answer.

Example: [object] %>% table() %>% pairwise_chisq_gof_test()

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
NoteAnswer: Group 1 and three are significant

Only grp1 vs grp3 shows a significant difference in outcome. The post hoc test confirms that the new treatment had a significant effect, but it is most pronounced in specific group comparisons (grp1 vs grp3). The lack of significance in other comparisons could be due to smaller effect sizes or limited sample sizes. This shows that patients who receive the new treatment for cardiac arrest is in fact helpful.