Skip to content

Double FOR

3 messages · Ginestet, Cedric, Ales Ziberna, Hadley Wickham

#
Maybe you whould try:

One of your problems is that you are rewriting the resoults so that the 
resoult you get is only for the final combination of m, s, y.

Secondly, I am not sure if you want to use in the equation elements of m, s, 
y, or the whole vectors.

function (m,s,y)

{
DIC.hat<-NULL
for (j in m){

for (k in s){

for (i in y){



DIC.hat<-cbind(DIC.hat,c(m=j,s=k,y=i,DIC.hat=sum(-2*((log(1/sqrt(2*pi*k^2))*exp((((i-j)/k)^2)/-2)))))

}

}

}

DIC.hat

}


Best,
Ales Ziberna
----- Original Message ----- 
From: "Ginestet, Cedric" <c.ginestet at imperial.ac.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Saturday, November 26, 2005 4:09 PM
Subject: [R] Double FOR
#
One way would be explicitly create your data first:
df <- expand.grid(a = 1:5, b= 1:5, c=1:5)

And then take advantage of vectorisation to compute DIC.hat:
with(df, sum(-2*((log(1/sqrt(2*pi*a^2))*exp((((b-c)/a)^2)/-2))))

which looks awfully like differences of two normal densities, so you
might be able to use dnorm (which might be faster as it is written
purely in C (but not noticeably so unless you have a lot of data), but
will make your algorithm more clear).

By breaking it down into multiple steps, hopefully you can get a
better idea of what's going on.  Putting browser() in the middle of
your loop would be another way for you to check out what is really
happening.

Hadley
On 11/26/05, Ginestet, Cedric <c.ginestet at imperial.ac.uk> wrote: