My understanding is that a Chi Square Goodness of Fit test and a one-proportion Z-test should have identical p-values and be equivalent (https://youtu.be/-Vssir6yUNQ?si=4-t9Np9h4bedU9xN&t=423). However, when I test them in R, they return different p-values and test statistics. Should the below code be written differently or am I misunderstanding the statistics? I am absolutely stumped why these two examples are not identical!
Reproducible example:
# Simulated data: 100 observations of a categorical variable with two levels
observed <- c(45, 55) # Frequencies for the two categories
expected_prop <- 0.5 # Expected proportion for the first category (null hypothesis)
# Perform one proportion Z-test
prop_test <- prop.test(observed[1], n = sum(observed), p = expected_prop, alternative = "two.sided")
cat("One Proportion Z-Test:\n")
print(prop_test)
# Perform chi-square goodness of fit test
expected <- rep(sum(observed) * expected_prop, length(observed))
chi_square_test <- chisq.test(observed, p = expected, rescale.p = TRUE)
cat("\nChi-Square Goodness of Fit Test:\n")
print(chi_square_test)
Correct answer from Demetri (change correction to False):
# Load required library
library(stats)
# Simulated data: 100 observations of a categorical variable with two levels
observed <- c(45, 55) # Frequencies for the two categories
expected_prop <- 0.5 # Expected proportion for the first category (null hypothesis)
# Perform one proportion Z-test
prop_test <- prop.test(observed[1], n = sum(observed), p = expected_prop, alternative = "two.sided", correct = F)
cat("One Proportion Z-Test:\n")
print(prop_test)
# Perform chi-square goodness of fit test
expected <- rep(sum(observed) * expected_prop, length(observed))
chi_square_test <- chisq.test(observed, p = expected, rescale.p = F)
cat("\nChi-Square Goodness of Fit Test:\n")
print(chi_square_test)