[R] foreach loop - rejection method

jim holtman jholtman at gmail.com
Thu Oct 8 13:48:38 CEST 2009


Your function is referencing 'cand2' but where is it defined?  There
are other objects referenced in the function that do not appear to be
passed in as parameters.  Are they defined globally?  What do you
think the following statement in your function is doing:

 cand2[l-1]<-rinvgamma(1,phi,lambda[l-1])

Do you think it is changing the value in a global object 'cand2'?  If
so, you need to understand the scoping in R.

You also need to provide commented, minimal, self-contained,
reproducible code.  Also what packages would be required to run your
code.

On Wed, Oct 7, 2009 at 8:56 PM, dunno87 <benparker1987 at hotmail.com> wrote:
>
> Hi Everybody,
>
> Thanks in advance for your help.
> This is my first time using the foreach statement and I cant get it to work
> properly so here is what i have
>
> test<-function(){
> repeat {
> cand2[l-1]<-rinvgamma(1,phi,lambda[l-1])
> q2<-dinvgamma(cand2[l-1],phi,lambda[l-1])
> p2<-cand2[l-1]^-1.5*exp(-y[l]^2/(2*cand2[l-1]))*exp(-((log(cand2[l-1])-mu_t_cand[l-1])^2)/2*sigmasq)
> ratio<-p2/(c[l-1]*q2)
> if (runif(1)< ratio) {break}
> }
> f2[l-1]<-min(p2,c[l-1]*q2)
> }}
> foreach(l=2:(n-1),.combine=c,.packages='MCMCpack',.options.nws=li
> st(chunkSize=250)) %dopar% test()
>
> even though i've created the array earlier called cand2 when i run this code
> i get the error object "cand2" not found.
>
> What I'm trying to do is the rejection method for my cand2. i need cand2 and
> f2 as vectors as output both of length l-2 in the correct order.
>
> if i do a for loop as normal it works perfectly. Any ideas?
>
> Ben
> --
> View this message in context: http://www.nabble.com/foreach-loop---rejection-method-tp25796927p25796927.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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list