Skip to content

Instantly share code, notes, and snippets.

@agabrielsen
Created June 19, 2016 22:06
Show Gist options
  • Save agabrielsen/e029a1833b79580832735d0bf58c4bb3 to your computer and use it in GitHub Desktop.
Save agabrielsen/e029a1833b79580832735d0bf58c4bb3 to your computer and use it in GitHub Desktop.
Jarque-Bera Test for Normality
jarquebera <- function(x){
#' Test for normality Jarque-Bera, Bowman-Shenton(1977))
#'
#' Calculates Jarque-Bera Normality test & corresponding p-value
#' @param x a Nx1 vector
#' @return Jarque-Bera statistic
#' @export
#' @author Alexandros Gabrielsen
T=length(x)
mu=mean(x)
mu3=sum((x-mu)^3)/T
mu4=sum((x-mu)^4)/T
sig2=((T-1)/T) * var(x) # Population Variance
b1=(mu3/(sig2^(3/2)))^2
b2=mu4/(sig2)^2
W=T*( b1/6 + ((b2-3)^2)/24)
chi2_pval=1-chis_cdf(W,2) # 1-pchisq(W,2)
jb = as.matrix(c(W, chi2_pval))
colnames(jb) = "Jarque-Bera"
rownames(jb) = c("JBStat", "Probability")
return(jb)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment