Randomizing matrices
Thanks Peter, I think that might actually work. I Will let you know. Allan ----- Original Message ----- From: "Peter Solymos" <solymos at ualberta.ca> To: "Allan Edelsparre" <aedelspa at uoguelph.ca> Cc: r-sig-ecology at r-project.org Sent: Saturday, August 18, 2012 10:50:46 AM Subject: Re: [R-sig-eco] Randomizing matrices Allan, Simply defining the dimension might work: dim(v) <- dim(trial2) but it is not clear what you are trying to achieve with the rep(..., 1000) part. It won't permute the matrix 1000 times but repeat same values. You might want to have a look at oecosimu in vegan which calculates the distribution given you SSD statistics, and you can use many different matrix permutation algorithms although it seems to me that you are just shuffling the cells. Cheers, Peter -- P?ter S?lymos, Dept Biol Sci, Univ Alberta, T6G 2E9, Canada AB solymos at ualberta.ca, Ph 780.492.8534, http://psolymos.github.com Alberta Biodiversity Monitoring Institute, http://www.abmi.ca Boreal Avian Modelling Project, http://www.borealbirds.ca
On Sat, Aug 18, 2012 at 7:05 AM, Allan Edelsparre <aedelspa at uoguelph.ca> wrote:
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
_______________________________________________ R-sig-ecology mailing list R-sig-ecology at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology