Thanks for the workaround & great explanation!
On Fri, Jun 12, 2009 at 10:30 AM, Olivia Lau <olivia.lau(a)post.harvard.edu>wrote;wrote:
Dear Donald,
I don't think you're doing anything wrong, but there are limitations
to what Zelig can do with mi() data. To get around this limitation, I
suggest:
my.function <- function(z.out = z.out, age.val=2, income.val=2, educ.val=
2) {
# print(c(tm,cv,rt))
age.val <<- age.val
income.val <<- income.val
educ.val <<- educ.val
x.out <- setx(z.out, age = age.val, income = income.val, educ =
educ.val)
s.out <- sim(z.out, x = x.out)
print(summary(s.out))
}
The technical explanation is a bit complicated, but here's an edited
answer. An "environment" in R is a set of objects and functions that
are visible and accessible by other functions that processes. The
"global environment" is your R workspace, which generally consists of
all the objects you see in ls() and all the exported functions in
packages that you can see with search(). (In fact, each of the
packages that you get back via search() is also an environment, and
each function that you define is also its own environment.)
Environments are self-contained -- that is, if you're working in one
environment, you will not have access to objects in another
environment, unless that object is in the search path. In the case of
setx() with MI, the search path jumps to the global environment and
bypasses the environment defined by the function my.function(). Since
age.val, etc are only defined in my.environment(), setx() with MI
can't find these values. In addition, I added z.out to the list of
inputs to your function so that my.function() won't have to search for
it.
Using the <<- operator rather than the <- operator is not generally
recommended because <<- overwrites values in the global environment
from inside another function. If you had age.val in the global
environment, it would be overwritten by the value of age.val given in
the function above. In this case, this is what you want.
Hope this helps. Best,
Olivia
On Thu, Jun 11, 2009 at 5:27 PM, Donald Braman<dbraman(a)law.gwu.edu> wrote:
Hey folks, I have a question about passing values
to Zelig inside a
function. I'm wondering why I can't pass a value the way I normally would
with a function. Take the following example:
# LET'S SAY I HAVE A SIMPLE MODEL
z.out <- zelig(murder.bin ~ age + income + educ ,
model = "logit",
data = mi(miin1, miin2, miin3, miin4, miin5))
summary(z.out)
# THIS WORKS
age.val <- 1
income.val <- 1
educ.val <- 1
x.out <- setx(z.out, age = age.val, income = income.val, educ = educ.val)
s.out <- sim(z.out, x = x.out)
print(summary(s.out))
# BUT THIS DOESN'T WORK
my.function <- function(age.val=2, income.val=2, educ.val= 2) {
print(c(tm,cv,rt))
x.out <- setx(z.out, age = age.val, income = income.val, educ = educ.val)
s.out <- sim(z.out, x = x.out)
print(summary(s.out))
}
my.function()
# AND THIS DOESN'T WORK
my.function(age.val=3, income.val=3, educ.val=3)
What am I doing wrong?
-
Zelig Mailing List, served by Harvard-MIT Data Center
Send messages: zelig(a)lists.gking.harvard.edu
[un]subscribe Options:
http://lists.gking.harvard.edu/?info=zelig
Zelig program information:
http://gking.harvard.edu/zelig/