# # these are functions for multidimensional scaling. they handle weights, # metric and non-metric, constraints on the configaration, # and various types of individual differences models. # # version 0.5.01 2005-10-08 # version 0.6.00 2006-02-22 (unified two versions) # version 0.7.00 2007-05-11 (added spherical constraints) # require("pava") require("mdsutils") # # Chapter1: Basic SMACOF for Sysmmetric and Rectangular Matrices # smacofSym<-function( diss, wgths=initWeights(diss), p=2, init="none", metric=TRUE, ties="primary", verbose=FALSE, relax=1, modulus=1, itmax=100, eps=1e-6) { n<-attr(diss,"Size"); m<-length(diss); dhat<-normDiss(diss,wgths) if (!is.matrix(init)) x<-torgerson(sqrt(delta),p=p) else x<-init if (!is.matrix(w)) w<-matrix(1,n,n)-diag(n) w<-vmat(wgths); v<-myGenInv(w); itel<-1; d<-dist(x); lb<-sum(wgths*d*dhat)/sum(wgths*d^2); x<-lb*x; d<-lb*d sold<-sum(wgths*(dhat-d)^2) repeat { b<-bmat(dhat,wgths,d); y<-v%*%b%*%x y<-x+relax*(y-x); e<-dist(y) ssma<-sum(wgths*(dhat-e)^2) if (!metric) { if ((itel%%modulus) == 0) { if (ties=="primary") daux<-monregP(diss,e,wgths) if (ties=="secondary") daux<-monregS(diss,e,wgths) if (ties=="tertiary") daux<-monregT(diss,e,wgths) dhat<-normDiss(daux,wgths) } } snon<-sum(wgths*(dhat-e)^2) if (verbose) cat("Iteration: ",formatC(itel,width=3, format="d")," Stress: ", formatC(c(sold,ssma,snon),digits=8,width=12,format="f"),"\n") if (((sold-snon)=0,delta,0); delta_min<-ifelse(delta<=0,-delta,0) if (is.list(init)) { x<-init[[1]]; y<-init[[2]] } else { e<-delta_plus^2; e<--0.5*(e-outer(rowSums(e)/m,colSums(e)/n,"+")+(sum(e)/(n*m))) z<-svd(e,nu=p,nv=0); x<-z$u; y<-crossprod(e,x) } d<-distRect(x,y,reg); lold<-sum(w*(delta-d)^2) repeat { ww<-w*(1+(delta_min/d)); wr<-rowSums(ww); wc<-colSums(ww) v<-solve(diag(wc)+(1/m)-crossprod(ww,ww/wr))-(1/m) b<-w*delta_plus/d; br<-rowSums(b); bc<-colSums(b) xraw<-(br*x)-(b%*%y); yraw<-(bc*y)-crossprod(b,x) y<-v%*%(yraw+crossprod(ww,xraw/wr)); x<-(xraw+(ww%*%y))/wr; d<-distRect(x,y,reg) lnew<-sum(w*(delta-d)^2) if (verbose) { cat("Iteration: ",formatC(itel,digits=6,width=6), " Stress: ",formatC(lold,digits=6,width=12,format="f"), " ==>",formatC(lnew,digits=6,width=12,format="f"),"\n") } if (((lold-lnew)