Skip to content

Instantly share code, notes, and snippets.

@RikFerreira
Last active July 21, 2021 16:52
Show Gist options
  • Save RikFerreira/7719dd6ac015f046f3a996ad5d4a3bc1 to your computer and use it in GitHub Desktop.
Save RikFerreira/7719dd6ac015f046f3a996ad5d4a3bc1 to your computer and use it in GitHub Desktop.
# Source: https://minato.sip21c.org/swtips/factor-in-R.pdf
bartlett.sphericity.test <- function(x) {
method <- "Bartlett’s test of sphericity"
data.name <- deparse(substitute(x))
x <- subset(x, complete.cases(x)) # Omit missing values
n <- nrow(x)
p <- ncol(x)
chisq <- (1-n+(2*p+5)/6)*log(det(cor(x)))
df <- p*(p-1)/2
p.value <- pchisq(chisq, df, lower.tail=FALSE)
names(chisq) <- "X-squared"
names(df) <- "df"
return(
structure(
list(
statistic = chisq,
parameter = df,
p.value = p.value,
method = method,
data.name = data.name
),
class = "htest"
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment