https://github.com/cran/nFactors
Tip revision: 875465dbb701152a2de23d9377cbe4c2604c4ad0 authored by Gilles Raiche on 14 October 2009, 00:00:00 UTC
version 2.3.1
version 2.3.1
Tip revision: 875465d
nCng.r
nCng <-
function(x, cor=TRUE, model="components", details=TRUE, ...) {
x <- eigenComputes(x, cor=cor, model=model, ...)
detail <- NULL
nlength <- 2
n <- length(x)
if (n < 6) stop("The number of variables must be at least 6.")
i <- 1
cng <- numeric(n-5)
while ((i+2*nlength+1) <= n) {
xa <- c(i:(i+nlength))
ya <- x[i:(i+nlength)]
compa <- lm(ya ~ xa)$coef[2]
xb <- c((i+1+nlength):(i+2*nlength+1))
yb <- x[(i+1+nlength):(i+1+2*nlength)]
compb <- lm(yb ~ xb)$coef[2]
cng[i] <- compb - compa
i <- i + 1
}
if (details == TRUE) detail <- data.frame(v=(1:(n-5)),values=x[1:(n-5)], cng)
cng <- as.numeric(which(cng==max(cng, na.rm=TRUE))+nlength)
res <- list(detail=detail, nFactors=c(cng))
class(res) <- c("nFactors","list")
return(res)
}