Skip to content

Problem using GLM in a loop (fwd)

1 message · Brian Ripley

#
This example is caused by R's messing with formula environments.

That's explained in ?formula, but should it not be explained in
?model.frame ?

Simple test:

data <- data.frame(y=rnorm(100), x=1:100)

testit <- function(formula)
{
   weights <- runif(100)
   glm(formula, weights=weights, data=data)
}
testit(y ~ x)

weights is looked for in the environment of the formula, not of the call
to glm.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

---------- Forwarded message ----------
Date: Tue, 21 Aug 2001 15:47:34 +0100 (BST)
From: Prof Brian Ripley <ripley@stats.ox.ac.uk>
To: Isabelle ZABALZA <isabelle.zabalza-mezghani@ifp.fr>
Subject: Re: [R] Problem using GLM in a loop

It's a scope problem: use

data$weights <- 1/sig

and it will work.  It's finding the system function weights, not your
variable.

I don't really understand this one as yet.
On Tue, 21 Aug 2001, Isabelle ZABALZA wrote: