[R] Speed up code with for() loop

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun May 1 18:02:50 CEST 2011



On 29.04.2011 22:20, hck wrote:
> Barth sent me a very good code and I modified it a bit. Have a look:
>
> Error<-rnorm(10000000, mean=0, sd=0.05)
> estimate<-(log(1+0.10)+Error)
>
> DCF_korrigiert<-(1/(exp(1/(exp(0.5*(-estimate)^2/(0.05^2))*sqrt(2*pi/(0.05^2
> ))*(1-pnorm(0,((-estimate)/(0.05^2)),sqrt(1/(0.05^2))))))-1))
> DCF_verzerrt<-(1/(exp(estimate)-1))
>
> S<- 10000000                                   # total sample size
> D<- 10000                                      # number of subsamples
> Subset<- 10000                                  # number in each subsample
> Select<- matrix(sample(S,D*Subset,replace=TRUE),nrow=Subset,ncol=D)
>
> DCF_korrigiert_select<- matrix(DCF_korrigiert[Select],nrow=Subset,ncol=D)
> Delta_ln<-(log(colMeans(DCF_korrigiert_select, na.rm=T)/(1/0.10)))
>
>
>
> The only problem I discovered is that R cannot handle more than
> 2.147.483.647 integers, thus the cells in the matrix are bounded by this
> condition. (R shows the max by typing: .Machine$integer.max). And if you
> want to safe the workspace, the file with 10.000 times 10.000 becomes round
> 2 GB. Compared to the original of "just" 300 MB.
>
> So I cannot perform my previous bootstrap with 1.000.000 times 100.000. But
> nevertheless 10.000 times 10.000 seems to be sufficiently; I have to say its
> amazing, how fast the idea works.
>
> Has anybody a suggestion how to make it work for the 1.000.000 times 100.000
> bootstrap???

Run it in several blocks of matrices with appropriate dimensions? This 
allows easy parallelization as well.

Uwe Ligges





>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Speed-up-code-with-for-loop-tp3481680p3484548.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



More information about the R-help mailing list