Francisco Mauro Gutiérrez
2013-Jan-18 00:15 UTC
[R-es] optimización de código en R (Carlos J
Hola Tania Que tal estas soluciones, creo que el tema va por la 4 sim4 <- function(nreps) { nb1 <- 10 # 10 blue marbles in Urn 1 n1 <- 18 # number of marbles in Urn 1 at 1st pick n2 <- 13 # number of marbles in Urn 2 at 2nd pick nb2 <- 6 draw1<-ifelse(runif(nreps)<nb1/n1,1,0) sum(draw2<-ifelse(runif(nreps)<(nb2+draw1)/(n2+draw1),1,0))/nreps } sim4b <- function(nreps) { nb1 <- 10 # 10 blue marbles in Urn 1 n1 <- 18 # number of marbles in Urn 1 at 1st pick n2 <- 13 # number of marbles in Urn 2 at 2nd pick nb2 <- 6 draw1<-ifelse(runif(nreps)<nb1/n1,1,0) sum(rbinom(nreps,1,(nb2+draw1)/(n2+draw1)))/nreps } system.time(sim1(10000)) # user system elapsed # 0.12 0.00 0.12 system.time(sim4(10000)) # user system elapsed # 0.02 0.00 0.01 system.time(sim4b(10000)) # user system elapsed # 0.02 0.00 0.02 Paco [[alternative HTML version deleted]]