Re: [R] For Loops please help!!
Yes, the cause is memory use patterns, but the price is steep nonetheless. E.g.: rate<-log(400*1.1^(1:30)) # runs about 27x times as fast as the following (test via 'microbenchmark') rate<-numeric(30) for (i in 1:30){ rate[i]<-log(400*1.1^i) } When manipulating large arrays, the difference can easily be a few seconds vs. an hour or more. And if many such arrays need to be run, the difference is between "difficult" and "not feasible". -Dan -- View this message in context: http://r.789695.n4.nabble.com/For-Loops-please-help-tp4711882p4711887.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] For Loops please help!!
This is not true. The steep price has to do with memory use patterns like result <- c( result, new value ). Vectorization is cleaner, easier to read, and somewhat faster, but for loops are not the monster that they have a reputation for being if the memory is allocated before the loop and elements are updated in the loop. In particular the more complicated the algorithm inside the loop, the less incremental overhead the for loop will introduce. --- Jeff NewmillerThe . . Go Live... DCN:Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. On September 5, 2015 8:31:15 AM PDT, Dan D wrote: >Also, any time you write "for" in R, you pay a steep price in >performance. In >a short, simple loop it may not be noticeable, but in a more >challenging >problem it can be a huge issue. > >A more efficient way to write your loop would be: >infectrate = 400*1.1^(1:30) # calculation >cbind(1:30,log(infectrate))# display > >-Dan > > > >-- >View this message in context: >http://r.789695.n4.nabble.com/For-Loops-please-help-tp4711882p4711885.html >Sent from the R help mailing list archive at Nabble.com. > >__ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] For Loops please help!!
Also, any time you write "for" in R, you pay a steep price in performance. In a short, simple loop it may not be noticeable, but in a more challenging problem it can be a huge issue. A more efficient way to write your loop would be: infectrate = 400*1.1^(1:30) # calculation cbind(1:30,log(infectrate))# display -Dan -- View this message in context: http://r.789695.n4.nabble.com/For-Loops-please-help-tp4711882p4711885.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] For Loops please help!!
The code has an error so it won't run as written. Instead of: infectrate[n]= (400)(1.1)^(n); try: infectrate[n]= 400*1.1^n; What I get after making this change looks right. -- View this message in context: http://r.789695.n4.nabble.com/For-Loops-please-help-tp4711882p4711884.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] For loops
This has nothing to do with your question, but instead of using >class=c(rep(1,3),rep(2,3),rep(3,3)) > It's probably easier to use class = rep(1:3, each =3) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] For loops
In addition to what Jeff and David have said... If you really want to create a separate data frame for each subgroup then you need use the assign function, and also data.split[[i]] instead of data.split[1]. for (i in 1:3) { assign( paste('sub',i,sep='') , data.split[[i]] } Jeff showed you how to skip the splitting, by subsetting within the loop. 'class' is also a R built-in function name, and thus not a good choice for one's on use. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 4/9/12 2:33 PM, "Christopher Desjardins" wrote: >Hi, >I am having trouble with syntax for a for loop. Here is what I am trying >to >do. > >class=c(rep(1,3),rep(2,3),rep(3,3)) >out1=rnorm(length(class)) >out2=rnorm(length(class)) >out3=rnorm(length(class)) >data=data.frame(class,out1,out2,out3) > >dat.split=split(data,data$class) > for(i in 1:3){ > sub[i]=dat.split[i] > } > >However, the for loop doesn't work. I want to assign each split to a >different data object. Better yet, how I could assign each class to a >separate object and skip the splitting? > >Thanks, >Chris > > [[alternative HTML version deleted]] > >__ >R-help@r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] For loops
On Apr 9, 2012, at 5:33 PM, Christopher Desjardins wrote: Hi, I am having trouble with syntax for a for loop. Here is what I am trying to do. class=c(rep(1,3),rep(2,3),rep(3,3)) out1=rnorm(length(class)) out2=rnorm(length(class)) out3=rnorm(length(class)) data=data.frame(class,out1,out2,out3) dat.split=split(data,data$class) for(i in 1:3){ sub[i]=dat.split[i] } However, the for loop doesn't work. I want to assign each split to a different data object. Why? What's wrong with leaving them in a list that split() provides. In that form you can easily use lapply() and start your 12 step program away from for-loop addiction. Better yet, how I could assign each class to a separate object and skip the splitting? -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] For loops
class1 <- data[1==data$class,] gets you a subset of the data into a dedicated object, but if you want to handle arbitrarily large amounts of data or class values then the list output of split is really much better to stay with. Also, "data" is a predefined function, so it is not a good idea to define objects with that name due to confusing code review. --- Jeff NewmillerThe . . Go Live... DCN:Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Christopher Desjardins wrote: >Hi, >I am having trouble with syntax for a for loop. Here is what I am >trying to >do. > >class=c(rep(1,3),rep(2,3),rep(3,3)) >out1=rnorm(length(class)) >out2=rnorm(length(class)) >out3=rnorm(length(class)) >data=data.frame(class,out1,out2,out3) > >dat.split=split(data,data$class) > for(i in 1:3){ > sub[i]=dat.split[i] > } > >However, the for loop doesn't work. I want to assign each split to a >different data object. Better yet, how I could assign each class to a >separate object and skip the splitting? > >Thanks, >Chris > > [[alternative HTML version deleted]] > >__ >R-help@r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] For loops
Hi, I am having trouble with syntax for a for loop. Here is what I am trying to do. class=c(rep(1,3),rep(2,3),rep(3,3)) out1=rnorm(length(class)) out2=rnorm(length(class)) out3=rnorm(length(class)) data=data.frame(class,out1,out2,out3) dat.split=split(data,data$class) for(i in 1:3){ sub[i]=dat.split[i] } However, the for loop doesn't work. I want to assign each split to a different data object. Better yet, how I could assign each class to a separate object and skip the splitting? Thanks, Chris [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] For loops in R
Hello. I've just started using R and am trying to figure out if the two codes snippets below have the same output gBest<-floor(runif(popsize,min=1,max=top)) velocity[i,j]<<-.4* velocity[i,j] + 1 * runif(1) * (pbestsVar[i,j] - popVar[i,j]) + 1 * runif(1) * (archiveVar[gBest,j] - popVar[i,j]) and for (i in 1:popsize) { for (j in 1:maxvar) { gBest<-sample(top,size=1) velocity[i,j]<<-.4* velocity[i,j] + 1 * runif(1) * (pbestsVar[i,j] - popVar[i,j]) + 1 * runif(1) * (archiveVar[gBest,j] - popVar[i,j]) #velocity[i,j]<-.4* velocity[i,j] + 1 * .3722 * (pbestsVar[i,j] - popVar[i,j]) + 1 * .3722 * (archiveVar[3,j] - popVar[i,j]) } many thanks -- View this message in context: http://n4.nabble.com/For-loops-in-R-tp1015933p1015933.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] help on R for loops
Hi, all. I need help on improving the efficiency of my R simulations. Below is a function that simulates a change point model. It first generates a sequence of three dimensional ARMA(1,1) observations, then calculates the one step ahead prediction errors, some statistic is calcualted and compared with threshold values in the end. As you can see in the function, there are 5 for loops, which makes the simulation long and inefficient. Can anyone help me improve it? simfun<-function(b){ for(l in 1:20) { qq<-matrix(0,2,40) for (w in 1:40) { x2 <- matrix(rep(0,6000),nr=3) epsilonzero2<-(mvrnorm(n=1,mu=c(0,0,0),Sigma=residsigma)) x2[,1:2]<-c(0,0,0) for (i in 3:550) { epsilonone<-(mvrnorm(n=1,mu=c(0,0,0),Sigma=residsigma)) x2[,i] <- phi%*%x2[,i-1]+epsilonone-theta%*%epsilonzero2 epsilonzero2<-epsilonone } residsigma3<-matrix(c(0.789,0.2143,0.171,0.2143,1.4394,-0.229,0.171,-0.229,0.6649),nrow=3,byrow=T) epsilonzero3<-epsilonzero2 for (i in 551:2000) { epsilonone<-(mvrnorm(n=1,mu=c(0,0,0),Sigma=residsigma3)) x2[,i] <- phi%*%x2[,(i-1)]+epsilonone-theta%*%epsilonzero3 epsilonzero3<-epsilonone } inno2<-matrix(0,3,2000) inno2[,1]<-c(0,0,1) for ( i in 2:2000) { inno2[,i]<-x2[,i]-phi%*%x2[,(i-1)]+ theta%*%inno2[,(i-1)] } sampeigen4<-matrix(0,3,(2000-nnumber[l])) for ( i in (nnumber[l]+1):2000) { var1<-matrix(0,3,3) for ( j in 1:(nnumber[l])) { var1<-var1+j^{b}*inno2[,i-nnumber[l]-1+j]%*%t(inno2[,i-nnumber[l]-1+j])/(sum(c(1:nnumber[l])^{b})) var1<-var1 } sampeigen4[,(i-nnumber[l])]<-(eigen(var1)$values-eigen(residsigma)$values) } chisqstat<-rep(0,(2000-nnumber[l])) for(i in 1:(2000-nnumber[l])) { chisqstat[i]<-((nnumber[l]-1)/2)*t(sampeigen4[,i])%*%(diag(eigen(residsigma)$values)^2)%*%(sampeigen4[,i]) } normstat<-apply((sampeigen4+eigen(residsigma)$values),2,prod) qq[1,w]<-vp(chisqstat[(550-nnumber[l]):(2000-nnumber[l])]) qq[2,w]<-vp1(normstat[(550-nnumber[l]):(2000-nnumber[l])]) } inarl2[,l]<-apply(qq,1,mean) } inarl2 } Thanks XS Looking for last minute shopping deals? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.