typestar

Chi-squared tests in R

Testing independence in a contingency table.

observed <- matrix(c(30, 20, 15, 35), nrow = 2,
                   dimnames = list(c("young", "old"),
                                   c("yes", "no")))

test <- chisq.test(observed)
statistic <- test$statistic
p_value <- test$p.value
expected <- test$expected

proportions <- prop.table(observed, margin = 1)
totals <- margin.table(observed, margin = 2)

How it works

  1. A matrix with dimnames holds the observed counts.
  2. chisq.test returns the statistic, p-value, and expecteds.
  3. prop.table converts counts to row proportions.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
301
Tokens
83
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 52 seconds.

Type this snippet

Step 2 of 2 in Hypothesis tests, step 5 of 11 in Statistics.

← Previous Next →