Skip to content

Instantly share code, notes, and snippets.

@bschneidr
Created February 18, 2019 21:38
Show Gist options
  • Save bschneidr/554979b061bdb7e68df870343ca5cc81 to your computer and use it in GitHub Desktop.
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.
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))
}
@bschneidr
Copy link
Author

Check and make sure this is using the appropriate tail for the kinds of tests this will be used for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment