Skip to content

Instantly share code, notes, and snippets.

@sTeamTraen
Last active February 17, 2022 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sTeamTraen/1f80c575216a4e1efc29432ede5aa6ac to your computer and use it in GitHub Desktop.
Save sTeamTraen/1f80c575216a4e1efc29432ede5aa6ac to your computer and use it in GitHub Desktop.
Demonstrates the chi-square distribution under the null for DFs 1 through 6
# Demonstration of chi-square distribution from chi-square tests under the null
# By Nick Brown, February 2022.
# Licence: CC-0
set.seed(2)
df.range <- 1:6 # range of DFs for which to generate plots
loops <- 10000 # number of cases to generate (more gives better precision)
N <- 5000 # range of numbers in tables
cst <- vector(mode="list", length=length(df.range))
sizes <- (df.range * 2) + 2
for (s in sizes) {
df <- (s / 2) - 1
cst[[df]] <- rep(NA, loops)
for (l in 1:loops) {
res <- matrix(as.integer(runif(s) * N), ncol=2)
cst[[df]][l] <- chisq.test(res)$statistic
}
}
for (cs in 1:length(cst)) {
hist(cst[[cs]]
, main=paste0("Chi-square distribution, DF=", cs)
, xlab=paste0("Chi-square statistic with N=", N)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment