[R] when vectorising does not work: silent function fail?

jim holtman jholtman at gmail.com
Tue Nov 10 16:17:01 CET 2009


Have you tried something like this:

my.results = apply(chr, 2, function(x){
    result <- try(anova(lrm( cpstc.f ~ x + time.cpstc + age + sex + mri))[1,3])
    if (inherits(result, "try-error")) return(NULL)
    result
})

This should catch the error and have NULL in that list element.

On Tue, Nov 10, 2009 at 10:04 AM, Federico Calboli
<f.calboli at imperial.ac.uk> wrote:
> Dear All,
>
> I'm using apply to do some genetic association analysis along a chromosome,
> with many thousands markers. For each marker the analysis is the same, so I
> was planning to use apply(chrom, 2, somefunction)
>
> In the specific case I do:
>
> my.results = apply(chr, 2, function(x){anova(lrm( cpstc.f ~ x + time.cpstc +
> age + sex + mri))[1,3]})
>
> This is all good and well in theory, but in practice the lrm() model will
> fail for some markers and wreck the whole process. Failure for some markers
> is no problem for me, but *predicting* which markers will fail can be hugely
> problematic.
>
> I then though of creating some fucntion to catch the error messages that
> would otherwise scr*w things over:
>
> my.lrm = function(x){
> pol = NULL
> pol = lrm( cpstc.f ~ x + time.cpstc + age + sex + mri)
> if(length(pol) > 0)
> rez = anova(pol)[1,3]
> if(length(pol)  == 0)
> rez = 1
> rez}
>
> my.results = apply(chr, 2, my.lrm)
>
> Still no joy, even adding try() in the evaluation and
> options(show.error.messages = F)
>
> I am at loss on how to get the darn function to bail out *silently* if needs
> be so I can just smack a replacement value in --which would also have the
> benefit of keeping the order of the markers.
>
> Any idea will be gratefully acknowledged.
>
> Best,
>
> Federico
>
>
> --
> Federico C. F. Calboli
> Department of Epidemiology and Public Health
> Imperial College, St Mary's Campus
> Norfolk Place, London W2 1PG
>
> Tel  +44 (0)20 7594 1602     Fax (+44) 020 7594 3193
>
> f.calboli [.a.t] imperial.ac.uk
> f.calboli [.a.t] gmail.com
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list