[R] Addition to: lm weights

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jul 16 18:09:32 CEST 2001


On Mon, 16 Jul 2001, Prof Brian Ripley wrote:

> On Mon, 16 Jul 2001, Christian Hoffmann wrote:
>
> > Hi all,
> >
> > Sorry for the omission in the first post.
> >
> > I have difficulties in specifying a weight vector inside lm which is called
> > inside a function.
> >
> > ++++Also stepAIC will not behave as expected.
> >
> > I have had the hope that this problem would go away by switching to R from
> > S+, as a variable generated inside a function should be visible to lm
> > inside it, but the evaluation of formulae seems to get in the way.
> >
> > regrbest(Una,"vr3c1",ind,"W")
> > with names(Una) containing the elements of ind:
> > ....    "vr91"  "vr92" ..  and additionally.... "W"
> >
> > regrbest <- function(x,y,ind,wt=NULL)
> >   form <- as.formula(paste(y,paste(ind, collapse = "+"), sep = "~"),
> > env=parent.frame())   #  is env WRONG here???
> > # form = vr3c1 ~ vr91 + vr92   with the actual call
> >   nna <- apply(x[,c(ind,wt)],1,function(x) all(!is.na(x)))
> >   xx  <- x[nna,]
> >   wtt  <- !is.null(wt) && any(wt!=1)
> >   if (wtt) {  #  is TRUE here
> >     w <- xx[,wt]
> >     lm1 <- lm(form,data=xx,weights=w)
> >   else {
> >     lm1 <- lm(form,data=xx,na.action=na.omit)
> >     stw <- stepAIC(lm1,direction="forward",trace=2, k=2)
> >
> > Error in eval(expr, envir, enclos) : Object "w" not found
> > The weights w are not found ????
>
> Right, as they are not in the scope of re-fitting the model.
> Why are you not using
>
> lm1 <- lm(form,data=xx,weights=wt)
>
> anyway?

Sorry, I see now. This was meant to be a function definition, but there
are no braces, so I read what was actually in the message ....

Use

xx$wt <- xx[,wt]
lm1 <- lm(form,data=xx,weights=wt)

-- 
Brian D. Ripley,                  ripley at 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

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list