# NOT RUN {
##1.toy example:
##simulate a two sample locational difference normal data:
X1<-matrix(rnorm(250,mean=0,sd=1),10,25)
X2<-matrix(rnorm(250,mean=0.5,sd=1),10,25)
X<-rbind(X1,X2)
## Adjacency matrix:
dmat<-dist(X)
W <-exp(-as.matrix(dmat)^2/(2*quantile(dmat,.5)^2))
## Obtain top 10 approximated nontrivial singular values:
data_sval<-LPSpectral(W, k=10)$sval
## Obtain approximated singular vector corresponding to the top nontrivial singular value:
data_phi1<-LPSpectral(W, k=1)$Phi
## plot the results:
par(mfrow=c(1,2))
plot(data_sval,type='b')
plot(data_phi1)
##2.Senate Data
# }
# NOT RUN {
data(senate)
attach(senate)
##creating W (long computation)
require(psych)
W <- matrix(0,nrow(X),nrow(X))
for(i in 1:(nrow(X)-1)){
for(j in (i+1):nrow(X)) {
W[i,j] <- psych::phi(table(X[i,],X[j,]))
}
}
W = W + t(W)
diag(W)<-0
## Obtain top 10 approximated nontrivial singular values:
senate_sval<-LPSpectral(W, k=10, m=15)$sval
## Obtain approximated singular vector corresponding to the top nontrivial singular value:
senate_phi1<-LPSpectral(W, k=1, m=15)$Phi
## plot the results:
par(mfrow=c(1,2))
plot(senate_sval,type='b')
plot(senate_phi1)
# }
Run the code above in your browser using DataCamp Workspace