I'm creating a function that performs cross-validation and ridge regression to select predictors for a model. The inputs of my function are dataframe
and the desired outcome variable outcome
(what is being predicted). I'm using model.matrix() to create an x matrix that I will pass to glmnet(). My function uses outcome
as the object argument in model.matrix(), but it looks like outcome
is the wrong data type to pass through model.matrix(). Using model.matrix() normally, I would write something like model.matrix(Weight~.,dataframe)
. In this case, however, model.matrix won't work as model.matrix(outcome~.,dataframe)
or model.matrix(dataframe$outcome~.,dataframe)
. Any ideas?
If 'outcome' is the object that stores the string "Weight"
, then we can paste
with formula
model.matrix(formula(paste(outcome, "~ .")), dataframe)
A reproducible example with 'iris' dataset
data(iris)
outcome <- "Species"
m1 <- model.matrix(formula(paste(outcome, "~ .")), iris)
m2 <- model.matrix(Species ~ ., iris)
identical(m1, m2)
#[1] TRUE