[R] No visible binding for global variable

Naser Jamil jamilnaser79 at gmail.com
Sat Jan 28 10:03:21 CET 2017


Dear Prof John,
I have got the points now. Many thanks for your suggestions.

Regards,
Jamil.

On 28 January 2017 at 00:27, Fox, John <jfox at mcmaster.ca> wrote:

> Dear Jamil,
>
> Presumably, the variables alloc.dose, r0, r1, and r2 will exist in the
> global environment when you call the compiled function lf1.c(), but didn’t
> exist when you compiled the function. If so, then lf1.c() should work
> properly despite the notes (not errors, or even warnings) that was printed.
>
> On the other hand, relying on global data in a function is poor
> programming style — it would be better to make alloc.dose, etc., arguments
> to the function. As well, if you’re compiling the function to improve
> efficiency, then you probably also want to compile psi0(), etc.
>
> I hope that this helps,
>  John
>
> John Fox
> Sen. William McMaster Professor of Social Statistics
> McMaster University
> Hamilton, Ontario, Canada
> Web: http::/socserv.mcmaster.ca/jfox
>
> > On Jan 27, 2017, at 10:43 AM, Naser Jamil <jamilnaser79 at gmail.com>
> wrote:
> >
> > Dear R-users,
> > I would like to seek your suggestion. I have the following code which
> runs
> > smoothly. But when I compile the function (lf1.c), it shows "no visible
> > binding for global variable" for some of the arguments.
> >
> > ###################################
> >
> > library(compiler)
> >
> >
> > psi0<-function(theta1,theta2,theta3,theta4,x){
> > z1<-exp(theta1+theta2*x)
> > z2<-exp(theta3+theta4*x)
> > 1/((1+z1)*(1+z2))
> >                                             }
> >
> > psi1<-function(theta1,theta2,theta3,theta4,x){
> > z1<-exp(theta1+theta2*x)
> > z2<-exp(theta3+theta4*x)
> > z1/((1+z1)*(1+z2))
> >                                             }
> > psi2<-function(theta3,theta4,x) {
> > z2<-exp(theta3+theta4*x)
> > z2/(1+z2)
> >                                }
> >
> >
> >
> > lf1<-function(w) {
> >   v<-1
> >   w1<-w[1]
> >   w2<-w[2]
> >   w3<-w[3]
> >   w4<-w[4]
> >   for (i in 1:length(alloc.dose)) {
> >       dose.i<-alloc.dose[i]
> >       r0.i<-r0[i]
> >       r1.i<-r1[i]
> >       r2.i<-r2[i]
> >       z1<-exp(w1+w2*dose.i)
> >       z2<-exp(w3+w4*dose.i)
> >       psi0<-1/((1+z1)*(1+z2))
> >       psi1<-z1*psi0
> >       v<-v*(psi0^r0.i)*(psi1^r1.i)*((1-psi0-psi1)^r2.i)
> >                                   }
> >   return(v)
> >                  }
> >
> >
> > lf1.c<-cmpfun(lf1)
> >
> > ###############################
> >
> > May I know how to avoid this message? If I leave the code as it is, will
> > that affect the result anyway?
> >
> > Thanks in advance.
> >
> >
> > Regards,
> > Jamil.
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at 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.
>
>
>
>
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list