[R] Using eval() more efficiently?

Kevin Wang Kevin.Wang at maths.anu.edu.au
Mon Sep 20 02:45:45 CEST 2004


Hi,

Suppose I have a vector:
  > names.select
  [1] "Idd13"   "Idd14"   "Idd8.12" "Idd7"
automatically generated by some selection criteria.

Now, if I have a data frame with many variables, of which the variables in
"names.select" are also variables from the data frame.  e.g.
  > all.df[1:5,]
    Mouse Idd5 Idd6.19.20 Idd13 Idd14 Idd8.12 Idd3.10.17.18 Idd9
  1   904   F1        NOD   NOD    F1     NOD            F1  NOD
  2   934  NOD         F1    F1    F1      F1           NOD  NOD
  3   950  NOD        NOD    F1   NOD      F1            F1  NOD
  4   977   F1        NOD   NOD    F1      F1            F1   F1
  5  1050   F1         F1   NOD   NOD     NOD           NOD   F1
    Idd15 Idd7 Idd2 Aire Idd4 Idd21 Cross
  1    F1  NOD  NOD  NOD  NOD   NOD     1
  2   NOD  NOD   F1   F1  NOD    F1     1
  3   NOD  NOD   F1   F1  NOD   NOD     1
  4   NOD   F1  NOD  NOD  NOD   NOD     1
  5   NOD   F1   F1   F1  NOD    F1     1

If I want to use the information from names.select to fit a glm() on
Cross, I can do something like:
  >  one.glm2 <- glm(eval(substitute(Cross ~ x1 + x2 + x3 + x4,
  +                                   list(x1 = as.name(names.select[1]),
  +                                        x2 = as.name(names.select[2]),
  +                                        x3 = as.name(names.select[3]),
  +                                        x4 =
as.name(names.select[4])))),
  +                    data = all.df, family = binomial)
which does exactly what I want.

However, this is kind of inefficient as if my selection criteria change,
the variables being selected in names.select may change and it will make
my eval() from one.glm2 invalid.

Is there a way to solve this?  e.g. if names.select has got 5 elements
then I'd want to fit something like:
  one.glm2 <- glm(eval(substitute(Cross ~ x1 + x2 + x3 + x4 + x5,
                                  list(x1 = as.name(names.select[1]),
                                       x2 = as.name(names.select[2]),
                                       x3 = as.name(names.select[3]),
                                       x4 = as.name(names.select[4]),
                                       x5 = as.name(names.select[5])))),
                  data = all.df, family = binomial)

(What I'm doing is writing a function which let's the user determine a
selection criteria, hence names.select will be unknown -- and so far I'm
very puzzled about how I can then use the information in names.select into
my one.glm2...*_*.

Cheers,

Kevin


--------------------------------
Ko-Kang Kevin Wang
PhD Student
Centre for Mathematics and its Applications
Building 27, Room 1004
Mathematical Sciences Institute (MSI)
Australian National University
Canberra, ACT 0200
Australia

Homepage: http://wwwmaths.anu.edu.au/~wangk/
Ph (W): +61-2-6125-2431
Ph (H): +61-2-6125-7407
Ph (M): +61-40-451-8301




More information about the R-help mailing list