LoginSignup
0
0

More than 5 years have passed since last update.

correspondence analysis

Last updated at Posted at 2019-02-28

以下サイトを参考にしました。二つ目のコードです。(URL:https://ncss-wpengine.netdna-ssl.com/wp-content/themes/ncss/pdf/Procedures/NCSS/Correspondence_Analysis.pdf


library(dplyr)

n=10

data=data.frame(n1=c(rpois(n/2,lambda=5),rpois(n/2,lambda=10)),n2=rpois(n,5),n3=rpois(n,8),n4=rpois(n,12))

ave_col=apply(data,2,sum);ave_row=apply(data,1,sum)

c_ij=(as.matrix(data)-mean(ave_col)[1])/sqrt(mean(ave_col)/sum(ave_col))

mat=array(0,dim=c(nrow(data),ncol(data)))

for(i in 1:nrow(data)){
for(j in 1:ncol(data)){

mat[i,j]=(ifelse(i==j,1,0)-mean(ave_col)[1]/sum(ave_col))/sqrt(mean(ave_col)/sum(ave_col))

}
}  

eigen(cor(c_ij))

eigen(cor(mat))


#correspondence analysis

library(dplyr)

n=10

data=data.frame(n1=c(rpois(n/2,lambda=5),rpois(n/2,lambda=10)),n2=rpois(n,5),n3=rpois(n,8),n4=rpois(n,12))

K=as.matrix(data)

P=K/sum(K);c=apply(P,2,sum);r=apply(P,1,sum)

D_r=sqrt(solve(diag(r)))

D_c=sqrt(solve(diag(c)))

A=D_r%*%P%*%D_c

mat=svd(A)

B=mat$u;W=diag(mat$d);C=mat$v

f=D_r%*%B%*%W

g=D_c%*%C%*%W

V=(W%*%W)

eigen_values=diag(V)

row_d=c();col_d=c()

for(i in 1:nrow(P)){

row_d=c(row_d,sum((P[i,]/apply(P,1,sum)[i]-apply(P,2,sum))^2/apply(P,2,sum)) ) 

}

for(j in 1:ncol(P)){

col_d=c(col_d,sum((P[,j]/apply(P,2,sum)[j]-apply(P,1,sum))^2/apply(P,1,sum)))  

}

Inertia_r=r*row_d^2/sum((r*row_d)^2)

Inertia_c=c*col_d^2/sum((c*col_d)^2)

row_d^2

col_d^2

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0