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
- A
matrixwith dimnames holds the observed counts. chisq.testreturns the statistic, p-value, and expecteds.prop.tableconverts counts to row proportions.
Keywords and builtins used here
clistmatrix
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.
Step 2 of 2 in Hypothesis tests, step 5 of 11 in Statistics.