Created
February 18, 2019 21:38
-
-
Save bschneidr/554979b061bdb7e68df870343ca5cc81 to your computer and use it in GitHub Desktop.
Implement a 'G-statistic' Likelihood Ratio Test, similar to the Chi-Squared Independence Test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g_statistic_lr_test <- function(x) { | |
chisq_results <- chisq.test(x) | |
expected_counts <- chisq_results[['expected']] | |
observed_counts <- chisq_results[['observed']] | |
G_Statistic <- 2 * sum(observed_counts * log(observed_counts/expected_counts), | |
na.rm = TRUE) | |
degrees_of_freedom <- chisq_results[['parameter']] | |
p_value <- pchisq(q = G_Statistic, | |
df = degrees_of_freedom, | |
ncp = 0, | |
lower.tail = TRUE) | |
return(list('statistic' = G_Statistic, | |
'degrees_of_freedom' = degrees_of_freedom, | |
'p_value' = p_value)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check and make sure this is using the appropriate tail for the kinds of tests this will be used for.