title: How to make this R code (for loop) more efficient?
body: <p>I am doing a simulation study and I wrote the following R code. Is there anyway to write this code without using two <code>for</code> loop, or make it more efficient (run faster)?</p>
  
  <pre><code>S = 10000
  n = 100
  v = c(5,10,50,100)
  beta0.mle = matrix(NA,S,length(v))  #creating 4 S by n NA matrix 
  beta1.mle = matrix(NA,S,length(v))
  beta0.lse = matrix(NA,S,length(v))
  beta1.lse = matrix(NA,S,length(v))
  for (j in 1:length(v)){
    for (i in 1:S){
      set.seed(i)
      beta0 = 50
      beta1 = 10
      x = rnorm(n)
      e.t = rt(n,v[j])
      y.t = e.t + beta0 + beta1*x
      func1 = function(betas){
        beta0 = betas[1]
        beta1 = betas[2]
        sum = sum(log(1+1/v[j]*(y.t-beta0-beta1*x)^2))
        return((v[j]+1)/2*sum)
      }
      beta0.mle[i,j] = nlm(func1,c(1,1),iterlim = 1000)$estimate[1]
      beta1.mle[i,j] = nlm(func1,c(1,1),iterlim = 1000)$estimate[2]
      beta0.lse[i,j] = lm(y.t~x)$coef[1]
      beta1.lse[i,j] = lm(y.t~x)$coef[2]
    }
  }
  </code></pre>
  
  <p>The function <code>func1</code> inside the second <code>for</code> loop is used for <code>nlm</code> function (to find mle when errors are t distributed).
  I wanted to use <code>parallel</code> package in R but I didn't find any useful functions.</p>