[R] question about update()

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Thu May 4 10:49:06 CEST 2023


On 04/05/2023 4:34 a.m., Adelchi Azzalini wrote:
> 
> 
>> On 4 May 2023, at 10:26, Duncan Murdoch <murdoch.duncan using gmail.com> wrote:
>>
>> On 04/05/2023 4:05 a.m., Adelchi Azzalini via R-help wrote:
>>> Hi. There must be something about the use of update() which I do not grasp,
>>> as the next exercise indicates.
>>> Suppose that obj is an object returned by a call to lm() or glm().
>>> Next, a new variable xf is constructed using the same dataframe used
>>> for producing obj. Then
>>> obj$data <- cbind(obj$data, xf=xf)
>>> new.obj <- update(obj, . ~ . + xf)
>>> generates
>>> Error in eval(predvars, data, env) : object 'xf' not found
>>> Could somebody explain what I got wrong, and how to fix it?
>>
>> I don't think you should be modifying the obj$data element:  as far as I can see, it's not used during the update, which will just re-evaluate the original call to glm().  So you should modify the dataframe that you passed in when creating obj.
>>
> 
> 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.

Sorry, that doesn't make sense.

You didn't show us complete code, but presumably it's preceded by 
something like this:

   obj <- glm( ..., data = somedata)

So change your modification to this:

   somedata$xf <- xf

That can be done hundreds of times.  This will need to be more elaborate 
if the function doing the iteration has a copy of obj but doesn't have a 
copy of somedata, but there are lots of ways to resolve that.  Without 
seeing complete code, I can't recommend which one to use.

Duncan Murdoch



More information about the R-help mailing list