给你写了一个,你看看是不是你想要的——
pca
head(iris)
library(psych)
cortest.bartlett(cor(iris[,-5]), n=150)
kmo = function( data ){
library(MASS)
X <- cor(as.matrix(data))
iX <- ginv(X)
S2 <- diag(diag((iX^-1)))
AIS <- S2%*%iX%*%S2 #anti-image covariance matrix
IS <- X+AIS-2*S2 # imagecovariance matrix
Dai <-sqrt(diag(diag(AIS)))
IR <-ginv(Dai)%*%IS%*%ginv(Dai) # image correlation matrix
AIR <-ginv(Dai)%*%AIS%*%ginv(Dai) # anti-image correlation matrix
a <- apply((AIR -diag(diag(AIR)))^2, 2, sum)
AA <- sum(a)
b <- apply((X -diag(nrow(X)))^2, 2, sum)
BB <- sum(b)
MSA <- b/(b+a) # indiv.measures of sampling adequacy
AIR <-AIR-diag(nrow(AIR))+diag(MSA) # Examine the anti-image of the
# correlation matrix. That isthe
# negative of the partialcorrelations,
# partialling out all othervariables.
kmo <- BB/(AA+BB) # overallKMO statistic
# Reporting the conclusion
if (kmo >= 0.00 &&kmo < 0.50){
test <- 'The KMO testyields a degree of common variance
unacceptable for FA.'
} else if (kmo >= 0.50&& kmo < 0.60){
test <- 'The KMO testyields a degree of common variance miserable.'
} else if (kmo >= 0.60&& kmo < 0.70){
test <- 'The KMO testyields a degree of common variance mediocre.'
} else if (kmo >= 0.70&& kmo < 0.80){
test <- 'The KMO testyields a degree of common variance middling.'
} else if (kmo >= 0.80&& kmo < 0.90){
test <- 'The KMO testyields a degree of common variance meritorious.'
} else {
test <- 'The KMO testyields a degree of common variance marvelous.'
}
ans <- list( overall = kmo,
report = test,
individual = MSA,
AIS = AIS,
AIR = AIR )
return(ans)
} # end of kmo()
kmo(iris[,-5])
iris.pr<-princomp(iris[,-5],cor=TRUE)
summary(iris.pr,loadings=TRUE)
plot(iris.pr,type="lines")
iris.pr$scores
scale(iris.pr$scores)
啊啊啊,居然写了这么多,必须给我点赞呀~~~