[R] Addition to: lm weights

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jul 16 17:54:21 CEST 2001


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?

> Calling with
> regrbest(Una,"vr3c1",ind)
>
> will generate lm1 but will stall at stepAIC:
> Error in terms.formula(formula, data = data) :
> 	Object "xx" not found
> > traceback()
> 9: terms.formula(formula, data = data)
> 8: terms(formula, data = data)
> 7: lm(formula = vr3c1 ~ vr91 + vr92 + vr18c1 + vr18c2 + vr191 +
>        vr192 + vr193 + vr211 + vr212 + vr221 + vr222 + vr223 + vr231 +
>        vr232 + F26REG.J + F27SEX + F28ALTER + F31PERS + vr361 +
>        vr362 + F37A + Urban + F6WISSEN + F7B1 + F7B2 + F7B3 + F7B4 +
>        F7B5 + F29CH + F30SCHUL + F33HUND + F35GART, data = xx, na.action =
> na.omit)
> 6: eval(expr, envir, enclos)
> 5: eval(call, parent.frame())
> 4: update.default(fit, paste("~ .", change))
> 3: update(fit, paste("~ .", change))
> 2: stepAIC(lm1, direction = "forward", trace = 2, k = 2)
> 1: regrbest(Una, "vr3c1", ind.9P6)
>
> Is there a general rule on how to treat this kind of problems?
> I very often build linear models inside functions and temporary variables
> seem to be a problem for models.

Put them in the data frame (but they are already there if accessed
sensibly), and in S-PLUS, put the data frame in frame 1.

-- 
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