[R-sig-eco] Randomizing matrices

Peter Solymos solymos at ualberta.ca
Sun Aug 19 18:13:57 CEST 2012


Allan, here is a toy example. Cc-ing back to SIG-ECO:

library(fields)
library(vegan)
x <- matrix(1:12,3,4)
rf <- function(x)
    array(sample(x), dim=dim(x))
sf <- function(x, y) {
    d <- rdist(x, y)
    sum(d %*% d)
}
z <- oecosimu(x, sf, rf)
z
hist(c(z$statistic, z$oecosimu$simulated))
abline(v=z$statistic, col=2)

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 Sun, Aug 19, 2012 at 5:50 AM, Allan Edelsparre <aedelspa at uoguelph.ca> wrote:
> Hi Peter,
>
> I am still having trouble with matrices, especially the oecosimus, where I got completely lost. However, as you also pointed out, all I am trying to do is to shuffle the cells. Do you have any suggestions as to how I can shuffle the cells a 1000 times, and then go on to make the SSD value 1000 times?
>
> Thanks again for helping out.
>
> 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
>>
>



More information about the R-sig-ecology mailing list