Skip to content

nested "for" loops

8 messages · nick_pan, R. Michael Weylandt, michael.weylandt at gmail.com (R. Michael Weylandt +1 more

#
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:
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:

            
#
I found the way out - it was because the borders of the vectors was close
enough thats why I had the same result while I was adding points to the
sequence. The example I gave was irrelevant but I made in order to find out
that the problem was.
Thank you all for your answers.


--
View this message in context: http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3993917.html
Sent from the R help mailing list archive at Nabble.com.
#
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:

            
#
No idea how this relates to what you said originally but glad you got
it all worked out.

And let us all reiterate: really, don't use nested for loops...there's
a better way: promise!

Michael
On Sat, Nov 5, 2011 at 2:20 PM, nick_pan <nick_pan88 at yahoo.gr> wrote: