Skip to content

Speed up for loop

3 messages · Andreas Bregiannis, Eivind K. Dovik

#
Hello everyone,

I would like to ask you how I can speed up this for loop.  In the following code I generate a sample of 200 of the NIG distribution and then I fit to them the NIG distribution. My aim is to estimate 1mln times the mean parameter of this NIG distribution.
Any ideas?

library(GeneralizedHyperbolic)
mu=0;delta = 1;alpha = 1;beta = 0
m=numeric(1000000)
for(i in 1:1000000){
m[i]=coef(nigFit(rnig(200, param = c(mu, delta, alpha, beta))
))[1]
}

Thank you in advance.
Best,
Andreas
#
On Sun, 6 May 2018, Andreas Bregiannis wrote:

            
Hi, Andreas.

How about this?

library(GeneralizedHyperbolic)
mu = 0; delta = 1; alpha = 1; beta = 0

func <- function(){
     random_numbers <- rnig(200, param = c(mu, delta, alpha, beta))
     fit <- nigFit(d)
     return(coef(fit)[1])
}

m <- as.numeric(rep(func(), 1000000)



Eivind K. Dovik
Bergen, NO
#
On Sun, 6 May 2018, Eivind K. Dovik wrote:

            
My apologies. The last line should be:

m <- as.numeric(replicate(1000000, func())