 x=read.csv("winequality-white.csv",sep=";", header=TRUE)
 x
 getwd()
 colMeans(x)
 X=x[,1:11] ##set of all predictors variables
 X
 X <-x[, !(names(x) %in% c("quality"))]
cov_matrix<-cov(X) 
print(cov_matrix) 
correlation<-cor(X)
fit<-prcomp(cov_matrix)
fit
summary(fit)
screeplot(fit,npcs=11, type="lines")
library(psych)
KMO(cor(X))
cortest.bartlett(cor(X), n=nrow(X))
pca_model<-prcomp(X,scale.=TRUE)
n<-nrow(X)
eigenvals<-(pca_model$sdev)^2
alpha<-0.05
z<-qnorm(1-alpha/2)
lambda1<-eigenvals[1]
lower<-lambda1/(1+z*sqrt(2/n))
upper<-lambda1/(1-z*sqrt(2/n))
c(lower,upper)

R<-cor(X)
p<-ncol(R)
 n<-nrow(X)
r_k<-numeric(p) 
for (k in 1:p){
  vals<-R[-k,k]
  r_k[k]<-mean(vals)
}
r_bar<-(sum(R)-p)/(p*(p-1))
gamma_hat<-((p-1)^2*(1-(1-r_bar)^2))/(p-(p-2)*(1-r_bar)^2)
term1<-0
for(i in 1:(p-1)){
  for(k in (i+1):p){
    term1<-term1+(R[i,k]-r_bar^2)
  }
}
 term2<-gamma_hat*sum((r_k-r_bar)^2)
 T<-(n-1)/(1-r_bar)^2 *(term1-term2)
 T
 
df_test<-(p+1)*(p-2)/2
alpha<-0.05
chi<-qchisq(1-alpha,df=df_test)
chi
