[19235] in s-news-athena

home help back first fref pref prev next nref lref last post

Re: [S] using apply() with update() of an lm()?

daemon@ATHENA.MIT.EDU (Bill Venables)
Wed Sep 15 19:48:48 1999

Message-Id: <199909152343.JAA29245@snowy.nsw.cmis.CSIRO.AU>
To: Doug Moog <dbm3@po.cwru.edu>
Cc: S-news@wubios.wustl.edu
In-Reply-To: Your message of "Wed, 15 Sep 1999 15:56:42 -0400."
             <4.1.19990915154346.009400e0@pop.cwru.edu> 
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Date: Thu, 16 Sep 1999 09:41:01 +1000
From: Bill Venables <William.Venables@cmis.CSIRO.AU>


> 
> I'm totally perplexed.   The following (where my.df is a numeric data frame):
> 
> x.null <- lm(my.df[,1]~1, data=my.df)
> apply(my.df[,-1], 2, function(x,y) {x.new_update(y,.~.+x);
> sum(residuals(x.new)^2)}, y=x.null)
> 
> gives:  Error in FUN(X, y = ..1): Object "x" not found

Yes, it's perplexing territory.  This is a visibility issue.  I am strongly of
the view that the modelling software was really designed with direct,
interactive use in mind.  The convenience of the data.frame argument comes at a
cost when you start to bury the modelling operations inside functions, even
like this inside apply().  It is possible, but messy, to fix it.  This is a
flaw with S-PLUS in my view, but one we probably need to live with.
> 
> But I don't get this error when 
> 
> x.null <- lm(runif(n)~runif(n))
> 
> where n is the number of rows in my.df (and no other n).

yep, visibility issues can indeed be very tricky and puzzling...

> (The objective is to get a vector of the sums of squares of the residuals
> of all variables in the data frame when modeled on the first one.)
> 

The saving grace is that if it is a common sort of thing to do there is usually
a pretty direct and simple solution.  In this case it is to use add1(), which
does almost precisely what you want, (although when you first meet add1() it
can be sort of difficult getting your head around as well, especially the
scope= argument...  :-)

Bill Venables.
-- 
-----------------------------------------------------------------
Bill Venables, Statistician, CMIS Environmetrics Project.

Physical address:                            Postal address:
CSIRO Marine Laboratories,                   PO Box 120,       
233 Middle St, Cleveland, Queensland         Cleveland, Qld, 4163
AUSTRALIA                                    AUSTRALIA

Telephone: +61 7 3826 7251     Email: Bill.Venables@cmis.csiro.au     
      Fax: +61 7 3826 7304


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

home help back first fref pref prev next nref lref last post