# problem 1 tests.mds.2 <- cmdscale(tests.diss, eig=T) par(pty="s") # Creates a square plotting region plot(tests.mds.2$points[,1], tests.mds.2$points[,2], type='n', xlab="Coordinate 1", ylab="Coordinate 2", xlim=c(-50,50), ylim=c(-50,50) ) text(tests.mds.2$points[,1], tests.mds.2$points[,2] , labels=row.names(tests.diss)) ## Problem 2, HW 4b recreation <- matrix(c( 61,49,25,21, 60,40,26,41, 60,37,33,52, 59,29,33,55, 48,16,28,57, 78,20,50,105), byrow=T, ncol=4, nrow=6, dimnames = list(c('18-24','25-34','35-44','45-54','55-64','65+'), c('exercise','sports','charity', 'gardening'))) # Enter name of data matrix / contingency table: mymat <- recreation rr <- nrow(mymat); cc <- ncol(mymat) row.sums <- apply(mymat,1,sum) col.sums <- apply(mymat,2,sum) N<-sum(row.sums) pijrow <- matrix(0,nrow=rr,ncol=cc); pijcol <- matrix(0,nrow=rr,ncol=cc) distmat.row <- matrix(0,nrow=rr,ncol=rr); distmat.col <- matrix(0,nrow=cc,ncol=cc) for (i in 1:rr){ pijcol[i,] <- mymat[i,]/row.sums[i] } for (j in 1:cc){ pijrow[,j] <- mymat[,j]/col.sums[j] } for (i in 1:rr){ for (ii in 1:(i-1)) { my.hold <- sqrt( sum( (N/col.sums)*(pijcol[i,]-pijcol[ii,])^2 ) ) distmat.row[i,ii] <- my.hold; distmat.row[ii,i] <- my.hold } } for (j in 1:cc){ for (jj in 1:(j-1)) { my.hold <- sqrt( sum( (N/row.sums)*(pijrow[,j]-pijrow[,jj])^2 ) ) distmat.col[j,jj] <- my.hold; distmat.col[jj,j] <- my.hold } } round(distmat.col,digits=2) round(distmat.row,digits=2) ################### END CODE ########################### ######################################################### # A straightforward chi-square test for independence of rows and columns: chisq.test(recreation) ############################################################################ # A quick way to do correspondence analysis using the "corresp" function # in the MASS library: library(MASS) # loading the MASS package corresp(recreation,nf=2) # The two-dimensional solution. biplot(corresp(recreation,nf=2)); abline(h=0); abline(v=0) corresp(recreation,nf=1)