On Aug 26, 2012, at 5:06 PM, Jinsong Zhao wrote:
> Hi there,
>
> In my code, there is a for loop like the following:
>
> pmatrix <- matrix(NA, nrow = 99, ncol = 10000)
> qmatrix <- matrix(NA, nrow = 99, ncol = 3)
> paf <- seq(0.01, 0.99, 0.01)
> for (i in 1:10000) {
> p.r.1 <- rnorm(1000, 1, 0.5)
> p.r.2 <- rnorm(1000, 2, 1.5)
> p.r.3 <- rnorm(1000, 3, 1)
> pmatrix[,i] <- quantile(c(p.r.1, p.r.2, p.r.3), paf)
> }
> for (i in 1:99) {
> qmatrix[i,] <- quantile(pmatrix[i,], c(0.05, 0.5, 0.95))
> }
>
> Because of the number of loop is very large, e.g., 10000 here, the
> code is very slow.
I would think that picking the seq(0.01, 0.99, 0.01) items in the
first case and the 500th, 5000th and the 9500th in the second case,
rather than asking for what `quantile` would calculate, would surely
be more "statistical", in the sense of choose order statistics anyway.
Likely much faster.
--
David.
>
> Is it possible to optimize the code?
>
> Any suggestion will be greatly appreciated.
>
> Regards,
> Jinsong
>
> ______________________________________________
> R-help at r-project.org 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.
David Winsemius, MD
Alameda, CA, USA