[R] setting options when using eval

Dominic Barraclough dominicb at cvs.rochester.edu
Fri Feb 20 20:48:37 CET 2004

Hi All,

I'm using a call to eval to evaluate a linear model, however, I have found 
that despite calling
options (contrasts=c("contr.sum", "contr.poly"))

prior  to evaluation, my model factors are coded using  the indicator 
coding associated with the "contr.treatment" contrast option

As an inelegant work around I am setting the contrast option explicitly in 
the model, but I'm hoping that somebody could be me some help with how to 
set  options (contrasts=c("contr.sum", "contr.poly"))
in the correct environment so that I don't need my hack. Some insights to 
what in the enviroments are in the context of eval would also be most helpful.

here is the version of the lm parameters that does what I want.

next.choice ~ Lag0 , data = the.matrix,   contrasts =list( Lag0 =contr.sum )

I don't want to have to add the last parameter
contrasts =list( Lag0 =contr.sum )
if possible.

Lag0 is a factor with 4 levels 0,1,2,3

the following is the function that I call to evaluate models
the.model <- function(the.matrix, model)
     options(contrasts=c("contr.sum", "contr.poly"))  # this does not cause 
factors to be code using contr.sum is subsequent
     expr <- paste("lm(", model,")")
     rc <-    eval(parse(text=expr))
     return (rc)

platform i386-pc-mingw32
arch     i386
os       mingw32
system   i386, mingw32
major    1
minor    8.1
year     2003
month    11
day      21
language R


More information about the R-help mailing list