[R] sapply() related query

Marc Schwartz marc_schwartz at me.com
Wed Jun 17 18:01:07 CEST 2009


On Jun 17, 2009, at 10:06 AM, Girish A.R. wrote:

> Hi folks,
>
> I'm trying to consolidate the outputs (of anova() and lrm()) from
> multiple runs of single-variable logistic regression. Here's how the
> output looks:
> ------------------------------------------------------------
>                          y ~ x1      y ~ x2       y ~ x3      y ~
> x4
> Chi-Square 0.1342152  1.573538  1.267291  1.518200
> d.f.                           2                 2
> 2              1
> P                0.9350946  0.4553136 0.5306538  0.2178921
> R2            0.01003342   0.1272791 0.0954126 0.1184302
> -------------------------------------------------------------------
> The problem I have is when there are a lot more variables (15+) --- It
> would be nice if this output is transposed.
>
> A reproducible code is included below. I tried the transpose function,
> but it didn't seem to work. If there is a neater way of getting the
> desired output, I'd appreciate that as well.
>
> ===========================================
> Lines <- "y   x1  x2  x3  x4
> 0   m   1   0   7
> 1   t   2   1   13
> 0   f   1   2   18
> 1   t   1   2   16
> 1   f   3   0   16
> 0   t   3   1   16
> 0   t   1   1   16
> 0   t   2   1   16
> 1   t   3   2   14
> 0   t   1   0   9
> 0   t   1   0   10
> 1   m   1   0   4
> 0   f   2   2   18
> 1   f   1   1   12
> 0   t   2   0   13
> 0   t   1   1  16
> 1   t   1   2   7
> 0   f   2   1   18"
>
> my.data <- read.table(textConnection(Lines), header = TRUE)
> my.data$x1 <- as.factor(my.data$x1)
> my.data$x2 <- as.factor(my.data$x2)
> my.data$x3 <- as.factor(my.data$x3)
> my.data$y <- as.logical(my.data$y)
>
> sapply(paste("y ~", names(my.data)[2:dim(my.data)[2]]),
> function(f){tab <- cbind(as.data.frame(t(anova(lrm(as.formula(f),data
> = my.data,x=T,y=T))[1,])),
> as.data.frame(t(lrm(as.formula(f),data = my.data,x=T,y=T)$stats[10])))
> })
> =================================
>
> Thanks,
>
> - Girish


You can try something like this:

library(Design)

my.func <- function(x)
{
   mod <- lrm(my.data$y ~ x)
   data.frame(t(anova(mod)[1, ]), R2 = mod$stats[10])
}

 > t(sapply(my.data[, -1], my.func))
    Chi.Square d.f. P         R2
x1 0.1342152  2    0.9350946 0.01003342
x2 1.573538   2    0.4553136 0.1272791
x3 1.267291   2    0.5306538 0.0954126
x4 1.518200   1    0.2178921 0.1184302


I am not sure what your end game might be, but would simply express  
the appropriate caution if this is a step in any approach to variable  
selection for subsequent model development...

HTH,

Marc Schwartz




More information about the R-help mailing list