Hi all , I have written a code with nested "for" loops . The aim is to estimate the maximum likelihood by creating 3 vectors with the same length( sequence ) and then to utilize 3 "for" loops to make combinations among the 3 vectors , which are (length)^3 in number , and find the one that maximize the likelihood ( maximum likelihood estimator). The code I created, runs but I think something goes wrong...because when I change the length of the vectors but not the bounds the result is the same!!! I will give a simple example(irrelevant but proportional to the above) to make it more clear... Lets say we want to find the combination that maximize the multiplication of the entries of some vectors. V1<-c(1,2,3) V2<-c(5, 2 , 4) V3<-c( 4, 3, 6) The combination we look for is ( 3 , 5 , 6) that give us 3*5*6 = 90 If I apply the following in R , I won't take this result V1<-c(1,2,3) V2<-c(5, 2 , 4) V3<-c( 4, 3, 6) for( i in V1){ for( j in V2) { for( k in V3){ l<- i*j*k } } } l Then " l<- i*j*k " is number and not vector(of all multiplications of all the combinations) , and is 3*4*6 = 72. How can I fix the code? -- View this message in context: http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992089.html Sent from the R help mailing list archive at Nabble.com.
Your problem is that you redefine l each time through the loops and don't record old values; you could do so by using c() for concatenation, but perhaps this is what you are looking for: exp(rowSums(log(expand.grid(V1, V2, V3)))) Hope this helps, Michael On Fri, Nov 4, 2011 at 7:49 PM, nick_pan <nick_pan88 at yahoo.gr> wrote:> Hi all , I have written a code with nested "for" loops . > The aim is to estimate the maximum likelihood by creating 3 vectors with the > same length( sequence ) > and then to utilize 3 "for" loops to make combinations among the 3 vectors , > which are (length)^3 in number , and find the one that maximize the > likelihood ( maximum likelihood estimator). > > > The code I created, runs but I think something goes wrong...because when I > change the length of the vectors but not the bounds the result is the > same!!! > > I will give a simple example(irrelevant but proportional to the above) to > make it more clear... > > Lets say we want to find the combination that maximize the multiplication of > the entries of some vectors. > > V1<-c(1,2,3) > V2<-c(5, 2 , 4) > V3<-c( 4, 3, 6) > > The combination we look for is ( 3 , 5 , 6) that give us 3*5*6 = 90 > > If I apply the following in R , I won't take this result > > V1<-c(1,2,3) > V2<-c(5, 2 , 4) > V3<-c( 4, 3, 6) > > for( i in V1){ > ?for( j in V2) { > ? ? for( k in V3){ > > l<- i*j*k > > } > } > } > l > > Then " l<- i*j*k " is ?number and not vector(of all multiplications of all > the combinations) , and is 3*4*6 = 72. > > How can I fix the code? > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992089.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >
Thank you , this works but I have to do it with nested for loops... Could you suggest me a way ? -- View this message in context: http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992324.html Sent from the R help mailing list archive at Nabble.com.
If in fact this is homework, you will do yourself, your classmates, and possibly your teacher if you let them know that, at least in R, almost anything you can do in a for() loop can be done easier and faster with vectorization. If you teacher can't comprehend this, get him fired. a<-c(4,6,3) b<- c( 9,4,1) d <- c(4,7,2,8) winning.value <- max(outer(a,outer(b,d,"*"),"*")) <quote> From: R. Michael Weylandt <michael.weylandt_at_gmail.com> Date: Sat, 05 Nov 2011 10:21:05 -0400 Why do you need to do it with nested for loops? It is of course possible - and I hinted how to do it in my first email - but there's no reason as far as I can see to do so, particularly as a means of MLE. Sounds suspiciously like homework... Michael On Nov 4, 2011, at 10:14 PM, nick_pan <nick_pan88_at_yahoo.gr> wrote: > Thank you , this works but I have to do it with nested for loops... > > Could you suggest me a way ? > -- Sent from my Cray XK6 "Pendeo-navem mei anguillae plena est."
You need to define "l" as a dimensioned object , either a vector or an array, and then assign the value of your calculation to the correctly indexed "location" in that object. Otherwise you are just overwriting the value each time through the loop. Use these help pages (and review "Introduction to R" ?array ?vector ?"[" On Nov 4, 2011, at 7:49 PM, nick_pan <nick_pan88 at yahoo.gr> wrote:> Hi all , I have written a code with nested "for" loops . > The aim is to estimate the maximum likelihood by creating 3 vectors with the > same length( sequence ) > and then to utilize 3 "for" loops to make combinations among the 3 vectors , > which are (length)^3 in number , and find the one that maximize the > likelihood ( maximum likelihood estimator). > > > The code I created, runs but I think something goes wrong...because when I > change the length of the vectors but not the bounds the result is the > same!!! > > I will give a simple example(irrelevant but proportional to the above) to > make it more clear... > > Lets say we want to find the combination that maximize the multiplication of > the entries of some vectors. > > V1<-c(1,2,3) > V2<-c(5, 2 , 4) > V3<-c( 4, 3, 6) > > The combination we look for is ( 3 , 5 , 6) that give us 3*5*6 = 90 > > If I apply the following in R , I won't take this result > > V1<-c(1,2,3) > V2<-c(5, 2 , 4) > V3<-c( 4, 3, 6) > > for( i in V1){ > for( j in V2) { > for( k in V3){ > > l<- i*j*k > > } > } > } > l > > Then " l<- i*j*k " is number and not vector(of all multiplications of all > the combinations) , and is 3*4*6 = 72. > > How can I fix the code? > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992089.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.