Search code examples
rfunctionerror-code

Errors when trying to create and use a random permutation test function in R


I am trying to complete a random permutation test in RStudio and keep getting the following two errors: Error: evaluation nested too deeply: infinite recursion / options(expressions=)? Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?

#create groups/data vectors
drinks = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
mosquito = c(27, 20, 21, 26, 27, 3, 24, 21, 20, 19, 23, 24, 28, 19, 24, 29, 18, 20, 17, 31, 20, 25, 28, 21, 27, 21, 22, 15, 12, 21, 19, 15, 22, 24, 19, 23, 13, 22, 20, 24, 18, 20)

#create function
rpermut = function(group, outcome, permutation){
diff = rep(NA, permutation)
for(i in 1:permutation){
outcome = sample(outcome)
diff[i] = mean(outcome[group==levels(group)[1]]) - mean(outcome[group==levels(group)[2]])}
diff
}

#adding values to function
mosrep = rpermut(group=drinks, outcome=mosquito, permutation=1000)

I am not sure what the error codes mean nor how to fix things so that the function will run. I would greatly appreciate any assistance you may be able to provide on where I am going wrong here!


Solution

  • So it seems to work for me with a few changes. Firstly I assume that both drinks and mosquito should be the same length which in your question is not the case.

    > length(drinks)
    [1] 43
    > length(mosquito)
    [1] 42
    

    Secondly, levels() works on factors whereas those objects drinks and mosquito are numeric vectors.

    > class(drinks)
    [1] "numeric"
    > class(mosquito)
    [1] "numeric"
    

    to therefore make this function work on my machine i had to then adjust the function to be this:

    rpermut = function(group, outcome, permutation){
        diff = c()
        group = as.factor(group)
        for(i in 1:permutation){
            outcome = sample(outcome)
            diff[i] = mean(outcome[group==levels(group)[1]]) - mean(outcome[group==levels(group)[2]])
        }
        return(diff)
    }
    

    This is just changing the group to a factor with as.factor()

    I also changed diff = rep(NA, permutation) to just diff = c() which is creating an empty vector. There is no need to assign NA to all the values as you can simply fill an entry with diff[i] the same way.

    So the vectors need to be the same length and then this should work, a check could simply be added as well.

    if(length(group) != length(outcome)){
            stop("input vector lengths does not match!")
        }
    

    All together:

    rpermut = function(group, outcome, permutation){
        if(length(group) != length(outcome)){
            stop("input vector lengths does not match!")
        }
        diff = c()
        group = as.factor(group)
        for(i in 1:permutation){
            outcome = sample(outcome)
            diff[i] = mean(outcome[group==levels(group)[1]]) - mean(outcome[group==levels(group)[2]])
        }
        return(diff)
    }