Skip to content

Instantly share code, notes, and snippets.

@benilton
Created June 13, 2017 03:33
Show Gist options
  • Save benilton/19b761a44dc28ccb57c855a6d94ca4d0 to your computer and use it in GitHub Desktop.
Save benilton/19b761a44dc28ccb57c855a6d94ca4d0 to your computer and use it in GitHub Desktop.
data(cars)
fit = lm(dist ~ speed, data=cars)
summary(fit)
system.time({
nBoot = 10000
coefs = rep(NA, nBoot)
for (j in 1:nBoot){
i = sample(nrow(cars), replace = TRUE)
fitBoot = lm(dist ~ speed, data=cars[i,])
coefs[j] = coef(fitBoot)[2]
}
})
hist(coefs)
qs = quantile(coefs, c(.025, .975))
sapply(qs, function(x) abline(v=x, lwd=2, col=2))
## Versao paralela
## Passo 1: utilizar conteudo do for()
## Passo 2: criar uma funcao com o conteudo acima
## Passo 3: funcao retornar o valor de interesse
meuBoot = function(cars){
i = sample(nrow(cars), replace = TRUE)
fitBoot = lm(dist ~ speed, data=cars[i,])
return(coef(fitBoot))
}
library(foreach)
library(doMPI)
## informar numero de processadores
registerDoMC(2)
results = foreach(j=1:10000, .combine=cbind) %dopar% {
meuBoot(cars)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment