celestialgod

parallel bootstrapping with RcppEigen and RcppParallel

Dec 31st, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.66 KB | None | 0 0
  1. Rcpp::sourceCpp("parallel_boot_lm.cpp")  # could be found at http://pastebin.com/Xir4LYei
  2.  
  3. set.seed(1234)
  4. N <- 1e3L
  5. p <- 50L
  6. R <- 500L
  7. dmat <- cbind(1, matrix(rnorm(N * p), ncol = p))
  8. beta <- c(2, runif(p))
  9. yvec <- as.vector(dmat %*% beta + rnorm(N, sd = 3))
  10. dall <- cbind(y = yvec, as.data.frame(dmat[,-1]))
  11. myindex <- matrix(sample(0:(N - 1), N * R, TRUE), ncol = R)
  12.  
  13. system.time(res1 <- parallelFit(dmat, yvec, myindex))
  14. #  user  system elapsed
  15. #  1.00    0.00    0.14
  16. system.time(res2 <- apply(myindex, 2, function(i) coef(lm(y ~ ., data = dall[i+1, ]))))
  17. #  user  system elapsed
  18. #  4.59    0.00    4.59
  19.  
  20. stopifnot(all(abs(res1 - res2) < 1e-8))
Advertisement
Add Comment
Please, Sign In to add comment