[R] survreg anova: problem with indirect invocation

William Valdar valdar at well.ox.ac.uk
Thu Oct 20 18:13:58 CEST 2005


Dear R help,

I've encountered a problem with survreg's anova(). I am currently 
writing general code to fit a variety of models using different fitting 
functions. Here's a simple example of what I'm trying to do:

---begin code---

# general function to analyse data
analyse.data <- function(formula, FUN, data, ...)
{
     fit <- FUN(formula, data=data, ...)
     anova(fit)
}

---end code---

In theory I should be able to call analyse.data() specifying FUN as I 
please. In practice FUN=lm and FUN=glm work but FUN=survreg does not. 
For example:

---begin code---

library(survival)

data <- data.frame(
         y = rexp(100, rate=1),
         status = rbinom(100,1,prob=0.8),
         x = rnorm(100))

# analyse as lm
formula <- as.formula("y ~ x")
analyse.data(formula, FUN=lm, data=data)
# works!

# analyse as glm
formula <- as.formula("y ~ x")
analyse.data(formula, FUN=glm, data=data, family="Gamma")
# works!

# analyse as survival
formula <- as.formula("Surv(y, status) ~ x")
analyse.data(formula, survreg, data=data)
# Error in eval(expr, envir, enclos) : couldn't find function "FUN"

# check survival works outside function
fit <- survreg(formula, data)
anova(fit)
# works!

---end code---

I suspect this is something to do with parent frames in anova.survreg(). 
However, although R mentions anova.survreg on traceback(), the code for 
this function seems not to be viewable.

I couldn't find any other positings about this. Any suggestions?

Note: I am using R version 2.1.1 and "survival" package version 2.18.

Many thanks,

William

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Dr William Valdar               ++44 (0)1865 287 717
Wellcome Trust Centre           valdar at well.ox.ac.uk
for Human Genetics, Oxford      www.well.ox.ac.uk/~valdar




More information about the R-help mailing list