[R] question about update()

Berwin A Turlach berw|n@tur|@ch @end|ng |rom gm@||@com
Thu May 4 11:44:10 CEST 2023

G'day Adelchi,

hope all is well with you.

On Thu, 4 May 2023 10:34:00 +0200
Adelchi Azzalini via R-help <r-help using r-project.org> wrote:

> Thanks, Duncan. What you indicate is surely the ideal route.
> Unfortunately, in my case this is not feasible, because the
> construction of xf and the update call are within an iterative
> procedure where xf is changed at each iteration, so that the steps 
> obj$data <- cbind(obj$data, xf=xf)
> new.obj <- update(obj, . ~ . + xf)
> must be repeated hundreds of times, each with a different xf.

If memory serves correctly, update() takes the object that is passed to
it, looks at what the call was that created that object, modifies that
call according to the additional arguments, and finally executes the
modified call.

So there is a lot of manipulations going on in update().  In particular
it would result each time in a call to lm(), glm() or whatever call was
used to create the object.  Inside any of these modelling functions a
lot of symbolic manipulations/calculations are needed too (parsing the
formula, creating the design matrix and response vector from the parsed
formula and data frame, checking if weights are used &c).

If you do the same calculation essentially over and over again, just
with minor modification, all these symbolic manipulations are just time

IMHO, you will be better off to bypass update() and just use lm.fit()
(for which lm() is a nice front-end) and glm.fit() (for which glm() is a
nice front-end), or whatever routine does the grunt work of fitting the
model to the data in your application (hopefully, the package creator
used a set up of XXX.fit() to fit the model, called by XXX() that does
all the fancy formula handling).



More information about the R-help mailing list