Dear all,
I want to use the deltavar() function from emdbook. I can use it
directly from the command terminal but within a function it behaves weird.
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?
Many thanks
variable scope for deltavar function from emdbook
3 messages · adad, Ben Bolker, Uwe Ligges
1 day later
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
2 days later
This is R, not S-Plus.
In the first two lines you have
expr <- as.expression(substitute(fun))
nvals <- length(eval(expr, envir = as.list(meanval)))
Simplified example:
y <- 0
fn1 <- function(){
y <- 1
fn1sub <- function() print(y)
fn1sub()
}
fn2sub <- function() print(y)
fn2 <- function(){
y <- 2
fn2sub()
}
fn1() # 1
fn2() # 0
So, if you do not want to pass the objects, but just use the symbols,
you have to make sure the objects go into the environment you are
evaluating the stuff in, or you have to ask the user to pass these
objects to deltavar in another way.
This is a similar region of the hell as evaluating glm() within a
function and using its formula interface.
Best,
Uwe Ligges
On 11.10.2011 14:09, Ben Bolker wrote:
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
______________________________________________ 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.