[R-sig-eco] Randomizing matrices

Allan Edelsparre aedelspa at uoguelph.ca
Sat Aug 18 15:05:38 CEST 2012


Dear R ecologists,

I'm trying to figure out a way to calculate the sum of squared
distances (SSD) between two matrices, where one matrix is held
constant and the other is randomized. So far I have been able to get
the syntax together to obtain my observed SSD, but the problem for me
is obtain SSD from 1000 randomizations to obtain a distribution. Below
is my syntax, all data were standardized a priori, each matrix is
6columns x 363rows.

For now the issue seems to be that the 'sample' command destroys the
matrix and turns it into a vector. I've tried to remedy this but now
it seems I may have one huge matrix from my randomizations rather than
1000 of the original size.

data1<-read.table("StandDataTime1and2.txt", header=TRUE)
data1<-data.frame(data1)

#split data into two matrices 6 X 363
#####trial1
trial1<-cbind(data1$fe1, data1$fr1, data1$ar1, data1$mid1, data$bot1,
data$surf1)
trial1<-as.matrix(trial1)

###trial2
trial2<-cbind(data1$fe2, data1$fr2, data1$ar2, data1$mid2, data1$bot2,
standardizedData$surf2)
trial2<-as.matrix(trial2)

##calculate euclidean distance between matrices
library(fields)
distances<-rdist(trial1, trial2)

###getting the observed value SSD
squared<-distances %*% distances
ssd<-sum(squared)
ssd
###observed value is 14301499

###########################################################################################
Below is where the problem is occuring


########permuting one block (trial2) and calculating a 95%
distribution from 1000 randomizations
v <- rep(sample(trial2 <- as.matrix(trial2)),1000)
## Now run those thousand values through the equation to calculate the ssd2

for (i in v) {
  dist2 <-rdist(trial1,[i])
  squared2<-dist2^2
  ssd2<-sum(squared2)
  print(ssd2)
}

Working with matrices seems to add to the level of difficulty. Any
help here would be greatly appreciated. Thanks in advance for any
help.

Allan



More information about the R-sig-ecology mailing list