[R] Has For bucle be impooved in R

Jesús Para Fernández j.para.fernandez at hotmail.com
Mon Aug 7 16:29:58 CEST 2017


Hi!

I am doing a lapply and for comparaison and I get that for is faster than lapply.


What I have done:



n<-100000
set.seed(123)
x<-rnorm(n)
y<-x+rnorm(n)
rand.data<-data.frame(x,y)
k<-100
samples<-split(sample(1:n),rep(1:k,length=n))

res<-list()
t<-Sys.time()
for(i in 1:100){
  modelo<-lm(y~x,rand.data[-samples[[i]]])
  prediccion<-predict(modelo,rand.data[samples[[i]],])
  res[[i]] <- (prediccion - rand.data$y[samples[[i]]])

}
print(Sys.time()-t)

Which takes 8.042 seconds

and using Lapply

cv.fold.fun <- function(index){
   fit <- lm(y~x, data = rand.data[-samples[[index]],])
   pred <- predict(fit, newdata = rand.data[samples[[index]],])
   return((pred - rand.data$y[samples[[index]]])^2)
  }


t<-Sys.time()

nuevo<-lapply(seq(along = samples),cv.fold.fun)
print(Sys.time()-t)


Which takes 9.56 seconds.

So... has been improved the FOR loop on R???

Thanks!





	[[alternative HTML version deleted]]



More information about the R-help mailing list