Search code examples
rshuffleveganpermute

Restricted permutation (permute) fails using shuffleSet and runs using shuffle


I'm doing PRC using the vegan-package but run into trouble when I attempt to perform an Anova on the results. I get the following error-message:

Error in doShuffleSet(spln[[i]], nset = nset, control) : 
  number of items to replace is not a multiple of replacement length

The problem originates in the shuffleSet-function of the permute-package. I created a reproducible example below. The weird thing is that the shuffle-function does not cause trouble, but the shuffleSet-function does.

In my experiment 3 treatments were given to 4 animals. The animals received the treatments in different orders. On every day, 5 samples were collected over time.

I would like to permute my observations within animals and not between them. Therefore I use AnimalID as a block.

I would like to permute days (in my actual experiments animals received the same treatment multiple times) but keep the measurements within a day intact. Hence I chose to permute Days freely and have no permutations within Days.

require(permute)    
TreatmentLevels=3
Animals=4
TimeSteps=5
AnimalID=rep(letters[1:Animals],each=TreatmentLevels*TimeSteps)
Time=rep(1:TimeSteps,Animals=TreatmentLevels)
#treatments were given in different order per animal. 
Day=rep(c(1,2,3,2,3,1,3,2,1,2,3,1),each=TimeSteps) 
Treatment=rep(rep(LETTERS[1:TreatmentLevels],each=TimeSteps),Animals)
dataset=as.data.frame(cbind(AnimalID,Treatment,Day,Time))

ctrl=how(blocks = dataset$AnimalID,plots = Plots(strata=dataset$Day,type = "free"),
          within=Within(type="none"), nperm = 999)

#this works
shuffle(60,control=ctrl)
#this giveas an error
shuffleSet(60,nset=1,control=ctrl)
shuffleSet(60,nset=10,control=ctrl)

The problem seems to be in the block. Because this works

dataset$AnimalDay=factor(paste0(dataset$AnimalID,dataset$Day))
ctrl=how(plots = Plots(strata=dataset$AnimalDay,type = "free"),
          within=Within(type="none"), nperm = 999)

#this works
shuffle(60,control=ctrl)
shuffleSet(60,nset=1,control=ctrl)
shuffleSet(60,nset=10,control=ctrl)

Solution

  • The key problem seems to be nset = 1: the permutation is generated and shuffleSet works, but printing the result fails because one set is dropped to a vector and print expects a matrix. You can get the permutation, you can use the permutation, but you cannot print it.

    We got to fix this.