[R] passing formula arguments cv.glm

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jul 26 14:06:33 CEST 2005


On Tue, 26 Jul 2005, Adaikalavan Ramasamy wrote:

> Dear Prof. Ripley,
>
> Thank you for your response. See below for my comments.
>
>
> On Tue, 2005-07-26 at 10:57 +0100, Prof Brian Ripley wrote:
>> Adai,
>
>> using traceback() helps, as does giving a reproducible example when
>> reporting a problem.
>
> You are right. The traceback below indicates that your guess is correct.
>
> 10: model.frame(formula = formu, data = data[j.in, , drop = FALSE],
>     drop.unused.levels = TRUE)
>
>
> The example is actually reproducible but you need the load the boot
> package first (which I failed to mention).
>
>> However, the problem is I think the line
>>
>>          d.glm <- update(glmfit, data = data[j.in, , drop = FALSE])
>>
>> in cv.glm.  I think that should be
>>
>>          d.glm <- eval.parent(update(glmfit,
>>                      data = data[j.in, , drop = FALSE], evaluate = FALSE))
>>
>> as in add.default and many other places.
>>
>> Could you please confirm that is the cause?
>
> I believe this is the cause but the fix may need bit more tweaking. I
> get the following error message with your fix.
>
> Error in "[.data.frame"(data, j.in, , drop = FALSE) :
>  	Object "j.in" not found
>
> The traceback gives the final same output as original one but this is
> numbered at 12 instead. Could it be because "j.in" is created within the
> for() loop environment ? I do not know how to fix this and would
> appreciate any help.

Ah, that needs a different fix, namely

         Call <- glmfit$call
         Call$data <- data[j.in, , drop=FALSE]
         d.glm <- eval.parent(Call)

which works for me when I modify your script accordingly.  (You can take 
the first line outside the loop, for tidiness.)


> For your convenience, you can find the script with your current fix at
> the following URL www.cbrg.ox.ac.uk/~ramasamy/cv.glm2.R
>
> Thank you very much. Much appreciated.
>
> Regards, Adai
>
>
>> Brian
>>
>> On Mon, 25 Jul 2005, Adaikalavan Ramasamy wrote:
>>
>>> I am trying to write a wrapper for the last example in help(cv.glm) that
>>> deals with leave-one-out-cross-validation (LOOCV) for a logistic model.
>>> This wrapper will be used as part of a bigger program.
>>>
>>> Here is my wrapper funtion :
>>>
>>>   logistic.LOOCV.err <- function( formu=NULL, data=NULL ){
>>>
>>>     cost.fn <- function(cl, pred) mean( abs(cl-pred) > 0.5 )
>>>
>>>     glmfit <- glm( formula=formu, data=data, family="binomial" )
>>>     print("glmfit is OK")
>>>
>>>     err    <- cv.glm( data=data, glmfit=glmfit,
>>>                       cost=cost.fn, K=nrow(data) )$delta[2]
>>>     print("cv.glm OK")
>>>   }
>>>
>>>
>>> When I run the above function line by line with the arguments from
>>> below, it works fine. But when I call it as function, I get this :
>>>
>>>   rm( glmfit, formu, cv.err ) # cleanup if required
>>>   logistic.LOOCV.err( formu=as.formula(r~stage+xray+acid), data=nodal )
>>>
>>> logistic.LOOCV.err( formu=as.formula(r~stage+xray+acid), data=nodal )
>>> [1] "glmfit is OK"
>>> Error in model.frame(formula = formu, data = data[j.in, , drop =
>>> FALSE],  :
>>> 	Object "formu" not found
>>>
>>>
>>> I think this has something to do with formula and environments but I do
>>> not know enough to solve it myself. I searched the archive without much
>>> help (perhaps I was using the wrong keywords).
>>>
>>> Any help would be very much appreciated. Thank you.
>>>
>>> Regards,
>>> --
>>> Adaikalavan Ramasamy                    ramasamy at cancer.org.uk
>>> Centre for Statistics in Medicine       http://www.ihs.ox.ac.uk/csm/
>>> Wolfson College Annexe                  Tel : 01865 284 408
>>> Linton Road, Oxford OX2 6UD             Fax : 01865 284 424
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>>
>>
>
>

-- 
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list