Integer overflow in R

4

I'm working on a population dynamics simulation that involves integer generation. Due to the assumptions of my model, I'm generating random numbers through the rmultinom function of R. However, I'm having overflow problems in my simulation.

The largest integer that R can generate is defined by .Machine$integer.max . On my PC, this number is equal to 2147483648. In scientific notation, this value is approximately 2,147 x 10 ^ 9.

But many of the simulations that I push go beyond this limit. The size parameter of the rmultinom will be greater than 10 ^ (10) or even 10 ^ (12). And if so, I can not generate random numbers with the distribution I want.

What could I do to solve this problem? Any suggestions?

    
asked by anonymous 22.09.2016 / 18:07

1 answer

2

From what I understand, what rmultinom does, is as follows:

  • You have lenght(prob) distinct types of balls, where prob is the prob parameter of the function.

  • Next you will remove% of balls% independently according to these probabilities. (% is the% parameter of the function)

  • This procedure will be repeated size , times. ( size is p parameter size of function)

Given this scenario, I imagine you can use n greater than what fits in n , as follows:

A possible function that takes samples with the maximum size of n

library(bit64)
rmultinom2 <- function(size, prob){

  n <- size %/% 1000000000L
  resto <- size %% 1000000000L

  amostra <- rmultinom(n = as.integer(n), size = 1000000000L, prob = prob)
  amostra_resto <- rmultinom(n = 1, size = resto, prob = prob)

  return(rowSums(cbind(amostra, amostra_resto)))
}

Repeating the experiment 100x

amostra <- plyr::ldply(1:100, function(x, size, prob) {
  rmultinom2(size, prob)
}, size = as.integer64("10000000000"), prob = c(1,2,4,5)
)

I think the big balcony here is to use the package sizes which supports larger integers and take several independent samples to add. It may also be necessary to transform the lines of R (within the function) into large integers so that the sum does not explode either.

Now, if R is greater than the largest integer, I do not know.

    
22.09.2016 / 20:51