Hello all
I have what seems like a simple question but have not been able to find an
answer on the forum. I'm trying to define a function which involves
regression models and a large number of covariates. 

I would like the function to accept any number of covariates and, ideally, I
would like to be able to enter the covariates in a group (e.g. as a list)
rather than individually. Is there any way of doing this? 

Example:

#define function involving regression model with several covariates 
custom <- function(outcome, exposure, covar1, covar2, covar3){
  model <- lm(outcome ~ exposure + covar1 +  covar2 + covar3)
  expected <- predict(model)
  summary(expected)
}

library(MASS)
attach(birthwt)

custom(bwt, lwt, low, age, race) #Works when 3 covariates are specified

custom(bwt,lwt,low,age) # Does not work with < or > 3 covariates

varlist <- list(low,age,race)
custom(bwt,lwt, varlist) #Does not work if covariates are included as a list



Thanks very much for your help

Tim

--
Tim Elwell-Sutton
University of Hong Kong

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to