[R] Help with permutation function from Turner et al. 2010 (Ecology)

Uwe Ligges ligges at statistik.tu-dortmund.de
Sat Jun 9 17:27:57 CEST 2012


On 09.06.2012 03:57, Ashley Stasko wrote:
> Hello,
>
>
> I'm using R code that includes a residual permutation that was written as a supplement to the paper:
>
>
> Turner et al. 2010. A general hypothesis-testing framework for stable isotopes ratios in ecological studies. Ecology 91:2227-2233.
> The supplemental code is available at: http://www.esapubs.org/archive/ecol/E091/157/suppl-1.htm
>
>
> When I execute the function, no warnings are given (seems fine). But the resulting output matrix is simply a matrix of repeating rows (i.e., it appears as if the function is selecting the same "random" sample every time). The function is as follows (please keep in mind that many of the internal functions were written by Turner et al. as well and may not be familiar):
>
>
> line.1<-c(0,traj.result(traj.size,traj.dir,traj.shape))
>
>
> permute<-function(y,x2,g,o,p) {
>    p.table<-line.1
>    yhat<-predict(x2)
>    res<-resid(x2)
>    line<-nrow(y)
>
>    for(i in 1:p){
>      line.rand<-sample(line,replace=FALSE)
>      res.temp<-cbind(line.rand,res)
>      z<-(order(line.rand))
>      res.temp2<-as.matrix(res.temp[z,])
>      res.p<-res.temp2[,-1]  # Rows of residuals are now randomized
>      y.rand<-yhat+res.p
>
>      lm.new<-lm(y.rand~o*g,x=T,model=T)
>      ls<-data.frame(expand.grid(o=levels(o),g=levels(g)))
>      ls[]<-lapply(ls, factor)
>      means.r<-predict(lm.new,ls)
>
>      traj.r<-arrayspecs(means.r,g,o)
>      traj.size.r<-trajsize(traj.r)
>      traj.dir.r<-trajorient(traj.r,ang.cor=T)
>      traj.shape.r<-trajshape(traj.r)
>      result<-traj.result(traj.size.r,traj.dir.r,traj.shape.r)
>      p.table<-rbind(p.table,c(i,result))
>    }
>    p.table
> }
>
>
>
> But when I try to execute the function the rows in the output matrix repeat the same numbers:
>
>
>> test3<-permute(Y,lm.2f.red,group,obs,99)
>> test3
>          [,1]     [,2]      [,3]      [,4]
> p.table    0 3.612322 0.9910104 0.3998703
>               1 3.612322 0.9910104 0.3998703
>               2 3.612322 0.9910104 0.3998703
>               3 3.612322 0.9910104 0.3998703
>               4 3.612322 0.9910104 0.3998703
>               5 3.612322 0.9910104 0.3998703
>       ......
>
>
> Can anyone see where there may be a problem with the permutation code? I'm relatively new to functions, and especially to writing permutation functions, so I'm having difficulty finding the problem.



If the results are suspicious (we cannot reproduce easily since you have 
not provided reproducible code as the posting guide asks you), why not 
ask the authors of the code? They hopefully know better what they aimed 
to get than readers of this list.

Best,
Uwe Ligges


>
> Thanks,
>
>
> Ashley
>
>
>
>
>
> ************************************************
> Ashley Stasko
> M.Sc. Candidate
> Cooperative Freshwater Ecology Unit
> Living With Lakes Centre
> Laurentian University
> Sudbury, ON
> Tel: 705-675-1151 ext. 2209
> Email: ax_stasko at laurentian.ca
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list