grattan / covidReff

Simulating Covid reproduction in a partially vaccinated population
Other
4 stars 1 forks source link

sample.int may be more appropriate than round(runif #17

Closed HughParsonage closed 3 years ago

HughParsonage commented 3 years ago

https://github.com/grattan/covidReff/blob/2ec1a2014a0a36eb6ccf57b12df8e08c72a7311f/R/simulate-covid.R#L171

Partly because that's what you 'really' mean (i.e. a uniform sample of integers), but also because round(runif()) undersamples edges (unless this was intended).

Consider also using dqrng for speed: (not just here but for every random variable).

library(data.table)
DT <- data.table(vaccine_type = c("pf", "az", "none")[sample(3, size = 1e6, replace = TRUE)])
bench::mark(DT[, days_since_first_dose := round(runif(.N, 
                                                      min = 1, 
                                                      max = fifelse(vaccine_type == "pf",
                                                                    21,
                                                                    90)))],
            DT[, days_since_first_dose := sample.int(if (.BY[[1]] == "pf")  21L else 90L, 
                                                     size = .N, 
                                                     replace = TRUE),
               by = "vaccine_type"],
            DT[, days_since_first_dose := dqrng::dqsample.int(if (.BY[[1]] == "pf")  21L else 90L, 
                                                              size = .N, 
                                                              replace = TRUE),
               by = "vaccine_type"],
            check = FALSE)
wfmackey commented 3 years ago

Will take dqrng in the PR!

wfmackey commented 3 years ago

Agree, and fixed in #15