[R] variable scope for deltavar function from emdbook

Ben Bolker bbolker at gmail.com
Tue Oct 11 14:09:21 CEST 2011


adad <adad <at> gmx.at> writes:

> Working example:
> ----------------------
> library("emdbook")
> 
> fn <- function()
> {
> browser()
> y <- 2
> print(deltavar(y*b2, meanval=c(b2=3), Sigma=1) )
> }
> 
> x <- 2
> print(deltavar(x*b1, meanval=c(b1=3), Sigma=1) )
> y<-3
> 
> fn()
> ------------------------
> 
> running this returns 4 for the first function call, which is fine.
> 
> For the call of deltavar in fn(), I get 9, i.e. the function uses y<-3 
> instead of the local y<-2. If y<- is commented, deltavar returns an error.
> 
> So why is the function not using the local variable and how do I make it 
> use it?

  The real problem is that I (the author) don't understand scoping in R, and
how to manipulate it, as well as I'd like to. I will work on this (any
tips from the R-helpers appreciated).  In the meantime, you could
try out one of the other available delta-method calculators, such
as the one in the msm package (library("sos"); findFn("{delta method}")).

  More text to try to make gmane happy

  Ben Bolker



More information about the R-help mailing list