2

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)
Nightingale
  • 233
  • 1
  • 10

1 Answers1

1

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.

Jari Oksanen
  • 3,287
  • 1
  • 11
  • 15
  • It also fails using nset=10 – Nightingale Jun 30 '16 at 11:16
  • I cannot confirm this: it works OK with nset=10 when I try. Further, `shuffleSet` works in all cases, also with `nset=1`. The thing that fails is showing its result on the screen. The error message comes from `print.permutationMatrix()`, and if you save the result without display using `p <- shuffleSet(60,nset=1,control=ctrl)` you get no error. – Jari Oksanen Jul 01 '16 at 05:11
  • Just to be clear: I never got the error message that you reported (`number of items to replace is not a multiple of replacement length`) which sounds like a user error. The message that I got was `invalid to set the class to matrix unless the dimension attribute is of length 2 (was 0)` which comes from `print`ing the permutations. The reason for this is obvious: with `nset=1` the permutations are dropped to a vector instead of being a one-row matrix. The second case you reported with `AnimalDay` factor works in `print`, because it does not drop to a vector. In both cases `shuffleSet` works. – Jari Oksanen Jul 02 '16 at 04:40
  • Do I understand correctly that the code runs when you try it? – Nightingale Jul 02 '16 at 12:06
  • When I cut and paste the code and run it in R it fails. I tried it using R 3.3 (both directly and via Rstudio) and in R 3.1. All resulting in the same error-message. I'm using permute version 0.8-4A. Any suggestions on what to try? – Nightingale Jul 02 '16 at 12:17
  • @Nightingale Yes, the code runs but we get an error when printing. The issue you report may be related to several bugs fixed in or before version 0.9-0, which is on CRAN. – Gavin Simpson Jul 02 '16 at 21:42
  • There is a discrepancy between behaviour and documentation; `?shuffleSet` claims it returns a matrix and it clearly isn't in the `nset = 1` case. I would prefer that functions like this always return the same type of object. In this case, I would suggest `?shuffleSet` should return a matrix with `nset` rows and `n` columns. Rather than change the `print` method I propose to change the function itself to always return a matrix. – Gavin Simpson Jul 02 '16 at 21:47
  • And this is related to `allPerms` being invoked with the current example but not `shuffleSet(10, nset = 1)` – Gavin Simpson Jul 02 '16 at 21:55
  • This is fixed on github https://github.com/gavinsimpson/permute/issues/19 and will be on CRAN as 0.9-1 some time next week when I get chance to package this up and review CRAN's policies. – Gavin Simpson Jul 02 '16 at 22:30
  • Thanks! Upgrading to version 0.9 helped. I was under the assumption that I was using the last version because the "update packages" button in Rstudio did not find any available updates. Could you post is as an answer so I can accept? And cool that I helped you discover another issue – Nightingale Jul 03 '16 at 14:21