Given the following, one of the things I am trying to see is what % of draws are below a certain number: lambda <- 3 rate <- 5 n <- 5 set.seed(123) v <- replicate(n, rexp(rpois(1,lambda), rate)) vv <- unlist(v) cat("% of draws below 0.1:", round(length(subset(vv, vv < 0.1))/length(vv)*100,0), "%\n") In actuality, my lambda, rate, and n are 26, 10, 1000000, respectively; which in effect makes the length of vv roughly equal 26'000'000. When I run cat(...), I get the following: Error: cannot allocate vector of size 101540 Kb In addition: Warning messages: 1: Reached total allocation of 1015Mb: see help(memory.size) 2: Reached total allocation of 1015Mb: see help(memory.size) Rather than keep the code as is and resort to memory.limit(), I would like to see how the code can be modified (i.e., alternatives to unlist()) such that I could still see what % of draws are below a certain number. I'd appreciate any suggestions. Regards, platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 2.1 year 2005 month 12 day 20 svn rev 36812 language R
Jacques Wagnor said the following on 5/4/2007 8:53 AM:> Given the following, one of the things I am trying to see is what % of > draws are below a certain number: > > lambda <- 3 > rate <- 5 > n <- 5 > > set.seed(123) > v <- replicate(n, rexp(rpois(1,lambda), rate)) > vv <- unlist(v) > cat("% of draws below 0.1:", round(length(subset(vv, vv < > 0.1))/length(vv)*100,0), "%\n") > > In actuality, my lambda, rate, and n are 26, 10, 1000000, > respectively; which in effect makes the length of vv roughly equal > 26'000'000. When I run cat(...), I get the following: > > Error: cannot allocate vector of size 101540 Kb > In addition: Warning messages: > 1: Reached total allocation of 1015Mb: see help(memory.size) > 2: Reached total allocation of 1015Mb: see help(memory.size) > > Rather than keep the code as is and resort to memory.limit(), I would > like to see how the code can be modified (i.e., alternatives to > unlist()) such that I could still see what % of draws are below a > certain number. > > I'd appreciate any suggestions. > > Regards, > > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 2.1 > year 2005 > month 12 > day 20 > svn rev 36812 > language R > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.This works for me on R-2.5.0 on Win XP with 1GB RAM. system.time({ set.seed(1) lambda <- 26 rate <- 10 n <- 1000000 v <- replicate(n, rexp(rpois(1, lambda), rate)) vbar <- sapply(v, function(x) mean(x < 0.1)) vlen <- sapply(v, length) }) # user system elapsed # 75.67 1.13 96.85 (m <- weighted.mean(vbar, vlen)) # [1] 0.6320496 cat("% of draws below 0.1: ", round(m * 100), "%\n", sep = "") Note your version R is almost a year and half old. Please upgrade. It's easy and free. --sundar
First, your example works happily in a 1Gb Windows machine under R 2.5.0: your R (2.2.1) is well overdue for an update. What you are generating is a random number of rexp(rate=10) random variables. So all you need is n <- 1000000 N <- sum (rpois(n, 26)) vv <- rexp(N, 10) mean(vv[vv < 0.1]) which runs is ca 500Mb and 6 secs. On Fri, 4 May 2007, Jacques Wagnor wrote:> Given the following, one of the things I am trying to see is what % of > draws are below a certain number: > > lambda <- 3 > rate <- 5 > n <- 5 > > set.seed(123) > v <- replicate(n, rexp(rpois(1,lambda), rate)) > vv <- unlist(v) > cat("% of draws below 0.1:", round(length(subset(vv, vv < > 0.1))/length(vv)*100,0), "%\n") > > In actuality, my lambda, rate, and n are 26, 10, 1000000, > respectively; which in effect makes the length of vv roughly equal > 26'000'000. > When I run cat(...), I get the following: > > Error: cannot allocate vector of size 101540 Kb > In addition: Warning messages: > 1: Reached total allocation of 1015Mb: see help(memory.size) > 2: Reached total allocation of 1015Mb: see help(memory.size) > > Rather than keep the code as is and resort to memory.limit(), I would > like to see how the code can be modified (i.e., alternatives to > unlist()) such that I could still see what % of draws are below a > certain number. > > I'd appreciate any suggestions. > > Regards, > > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 2.1 > year 2005 > month 12 > day 20 > svn rev 36812 > language R > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595