#### analysis for trade data #### normal scores transform Z<-array(qnorm( rank(c(Y),na.last="keep")/(sum(!is.na(Y))+1)) , dim(Y) ) dimnames(Z)<-dimnames(Y) Y<-Z Y[is.na(Y)]<- 0 #### #### get resids from additive model a1<-apply(Y,1,mean,na.rm=TRUE)-mean(Y,na.rm=TRUE) a2<-apply(Y,2,mean,na.rm=TRUE)-mean(Y,na.rm=TRUE) a3<-apply(Y,3,mean,na.rm=TRUE)-mean(Y,na.rm=TRUE) a4<-apply(Y,4,mean,na.rm=TRUE)-mean(Y,na.rm=TRUE) Y<-Y- mean(Y,na.rm=TRUE) - outer(a1,outer(a2,outer(a3,a4,"+"),"+"),"+") #### m<-dim(Y) K<-length(m) par(mfrow=c(2,2)) for(k in 1:K) { d<-svd(mat(Y,k))$d plot(d,type="h",ylim=c(0,max(d))) } r<-rep(2,K) hYa<-US.als(Y,r) plotwtxt(hYa$U[[1]],labels=dimnames(Y)[[1]]) plotwtxt(hYa$U[[2]],labels=dimnames(Y)[[2]]) plotwtxt(hYa$U[[3]],labels=dimnames(Y)[[3]]) plotwtxt(hYa$U[[4]],labels=dimnames(Y)[[4]]) ## does changing the rank for one mode affect the SVs of another? ## (a) truncated svd ## (b) ols/als