[R] Formula syntax question

Frank E Harrell Jr f.harrell at vanderbilt.edu
Tue Jul 3 16:49:46 CEST 2007

Isaac Kohane wrote:
> Forgive me if this is obvious:
> 	I have a frame of data with the variables in each column (e.g.  
> Discrete_Variable1, ContinuousVariable_1, ContinuousVariable_2,  ...   
> ContinuousVariable_n)
> 	and I want to create a model using lrm i.e.
> 	model <- lrm(Discrete_Variable1 ~ ContinuousVariable_1,  
> data=lotsofdata)
> 	Is there a syntax for having all the continuous variables referenced  
> in the formula without having to enumerate them all?
> 	I've seen the ~ . notation but when I try
> 	model <- lrm(Discrete_Variable1 ~  ., data=lotsofdata)
> 	I get this error:
> 	Error in terms.formula(formula, specials = "strat") :
> 	'.' in formula and no 'data' argument
> 	Any help is appreciated.
> -Zak

It may be best to write a function to determine what is continuous (>= 
10 unique values for example, and numeric) and to run sapply on that 
function, over your data frame.  Then you could use lrm(y ~ ., 
data=mydata[continuous]) if it were not for a problem with lrm which 
Charles Thomas Dupont (the Design package maintainer) and I will work 
on.  Until then you can write a command to compose a formula, e.g.,

form <- as.formula(paste('y', paste(names(mydata)[continuous], 
collapse='+'), sep='~'))
lrm(form, data=mydata)

Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University

More information about the R-help mailing list