## Chapter 12: Multivariate Data Exploration and Discrimination 

## Sec 12.1: Multivariate Exploratory Data Analysis 
## ss 12.1.1: Scatterplot matrices 
## Scatterplot matrix, columns 9-11 of possum (DAAG).  
## Colors distinguish sexes; symbols distinguish sites
library(DAAG)
## Loading required package: lattice
library(lattice) 
colr <- c("red", "blue") 
pchr <- c(3,4,0,8,2,10,1) 
ss <- expand.grid(site=1:7, sex=1:2)           # Site varies fastest 
parset <- with(ss, simpleTheme(pch=pchr[site], col=colr[sex])) 
sitenames <- c("Cambarville","Bellbird","Whian Whian", "Byrangery", 
               "Conondale ","Allyn River", "Bulburin") 
## Add column sexsite to possum; will be used again below 
possum$sexsite <- paste(possum$sex, possum$site, sep="-") 
splom(possum[, c(9:11)], groups = possum$sexsite,  
      col = colr[ss$sex], par.settings=parset, 
      varnames=c("tail\nlength","foot\nlength","ear conch\nlength"), 
      key = list(text=list(sitenames), points=list(pch=pchr), columns=3)) 

plot of chunk unnamed-chunk-1

## Cloud plot of earconch, taill and footlgth 
cloud(earconch~taill+footlgth, data=possum, pch=pchr, groups=site,  
      par.settings=simpleTheme(pch=c(3,4,0,8,2,10,1)), 
      auto.key = list(space="top", corner=c(0,1), columns=3, between=1, 
                 text=sitenames, between.columns=2)) 

plot of chunk unnamed-chunk-1

 # auto.key takes its symbols (pch) from par.settings 

## ss 12.1.2: Principal components analysis 
##                     Preliminary data scrutiny 
## Principal components calculations: possum[, 6:14] (DAAG) 
possum.prc <- princomp(na.omit(possum[, 6:14])) 

## Footnote Code
## Plot of principal components: possum[, 6:14] 
here<- complete.cases(possum[, 6:14]) 
colr <- c("red", "blue") 
pchr <- c(3,4,0,8,2,10,1) 
ss <- expand.grid(site=1:7, sex=1:2)            # Site varies fastest 
xyplot(possum.prc$scores[, 2] ~ possum.prc$scores[, 1], aspect="iso", 
       groups = possum$sexsite[here], col = colr[ss$sex], pch = pchr[ss$site], 
       xlab="1st Principal Component", ylab="2nd Principal Component", 
       key=list(points = list(pch=pchr), 
                text=list(c("Cambarville", "Bellbird", "Whian Whian",  
                            "Byrangery", "Conondale", "Allyn River", 
                            "Bulburin" )), columns=4))