# # double_center_drive_data.r -- Double-Center Program # # Data Must Be Transformed to Squared Distances Below # # ATLANTA 0000 2340 1084 715 481 826 1519 2252 662 641 2450 # BOISE 2340 0000 2797 1789 2018 1661 891 908 2974 2480 680 # BOSTON 1084 2797 0000 976 853 1868 2008 3130 1547 443 3160 # CHICAGO 715 1789 976 0000 301 936 1017 2189 1386 696 2200 # CINCINNATI 481 2018 853 301 0000 988 1245 2292 1143 498 2330 # DALLAS 826 1661 1868 936 988 0000 797 1431 1394 1414 1720 # DENVER 1519 891 2008 1017 1245 797 0000 1189 2126 1707 1290 # LOS ANGELES 2252 908 3130 2189 2292 1431 1189 0000 2885 2754 370 # MIAMI 662 2974 1547 1386 1143 1394 2126 2885 0000 1096 3110 # WASHINGTON 641 2480 443 696 498 1414 1707 2754 1096 0000 2870 # CASBS 2450 680 3160 2200 2330 1720 1290 370 3110 2870 0000 # # Remove all objects just to be safe # rm(list=ls(all=TRUE)) # library(MASS) library(stats) # # T <- matrix(scan("C:/uga_course_homework_5_2015/drive2.txt",0),ncol=11,byrow=TRUE) # nrow <- length(T[,1]) ncol <- length(T[1,]) # names <- c("Atlanta ","Boise ","Boston ","Chicago ","Cincinnati ","Dallas ","Denver ", "Los Angeles ","Miami ","Washington ","CASBS ") # # pos -- a position specifier for the text. Values of 1, 2, 3 and 4, # respectively indicate positions below, to the left of, above and # to the right of the specified coordinates # namepos <- rep(2,nrow) # TT <- rep(0,nrow*ncol) dim(TT) <- c(nrow,ncol) TTT <- rep(0,nrow*ncol) dim(TTT) <- c(nrow,ncol) # xrow <- NULL xcol <- NULL xcount <- NULL matrixmean <- 0 matrixmean2 <- 0 # # Transform the Matrix # i <- 0 while (i < nrow) { i <- i + 1 xcount[i] <- i j <- 0 while (j < ncol) { j <- j + 1 # # Square the Driving Distances # TT[i,j] <- (T[i,j]/1000)**2 # } } # # Put it Back in T # T <- TT TTT <- sqrt(TT) # # # Call Double Center Routine From R Program # cmdscale(....) in stats library # The Input data are DISTANCES!!! Not Squared Distances!! # Note that the R routine does not divide # by -2.0 # ndim <- 2 # dcenter <- cmdscale(TTT,ndim, eig=FALSE,add=FALSE,x.ret=TRUE) # # returns double-center matrix as dcenter$x if x.ret=TRUE # # Do the Division by -2.0 Here # TTT <- (dcenter$x)/(-2.0) # # # Below is the old Long Method of Double-Centering # # Compute Row and Column Means # xrow <- rowMeans(T, na.rm=TRUE) xcol <- colMeans(T, na.rm=TRUE) matrixmean <- mean(xcol) matrixmean2 <- mean(xrow) # # Double-Center the Matrix Using old Long Method # Compute comparison as safety check # i <- 0 while (i < nrow) { i <- i + 1 j <- 0 while (j < ncol) { j <- j + 1 TT[i,j] <- (T[i,j]-xrow[i]-xcol[j]+matrixmean)/(-2) } } # # # Double-Center the Matrix Using old Long Method # Compute comparison as safety check # i <- 0 while (i < nrow) { i <- i + 1 j <- 0 while (j < ncol) { j <- j + 1 TT[i,j] <- (T[i,j]-xrow[i]-xcol[j]+matrixmean)/(-2) } } # # Run some checks to make sure everything is correct # xcheck <- sum(abs(TT-TTT)) # # # Perform Eigenvalue-Eigenvector Decomposition of Double-Centered Matrix # ev <- eigen(TT) # # Find Point furthest from Center of Space # aaa <- sqrt(max((abs(ev$vec[,1]))**2 + (abs(ev$vec[,2]))**2)) bbb <- sqrt(max(((ev$vec[,1]))**2 + ((ev$vec[,2]))**2)) # # Weight the Eigenvectors to Scale Space to Unit Circle # torgerson1 <- ev$vec[,1]*(1/aaa)*sqrt(ev$val[1]) #torgerson2 <- ev$vec[,2]*(1/aaa) # #torgerson1 <- -ev$vec[,1]*(1/aaa) torgerson2 <- -ev$vec[,2]*(1/aaa)*sqrt(ev$val[2]) # plot(torgerson1,torgerson2,type="n",asp=1, main="", xlab="", ylab="", xlim=c(-3.0,3.0),ylim=c(-3.0,3.0),font=2) points(torgerson1,torgerson2,pch=16,col="red",font=2) text(torgerson1,torgerson2,names,pos=namepos,offset=0.2,col="blue",font=2) # # # Main title mtext("Double-Centered Driving Distance Matrix \n Torgerson Coordinates",side=3,line=1.25,cex=1.5,font=2) # x-axis title mtext("West --- East",side=1,line=2.75,cex=1.2,font=2) # y-axis title mtext("South --- North",side=2,line=2.5,cex=1.2,font=2) #