Kibour
Kibour

Reputation: 151

R - step with dynamic upper component

I have a table with a lot of columns. I would like to make a forward step regression on some columns only. Since there is a lot of columns and that I do not want to type them explicitly by hand, is there a handy method to pass them to the upper component? That is, something like

step(lm(lg ~ 1, data=na.omit(data)),
  scope=list(
    lower=lg ~ 1,
    upper=lg ~ names(data[grep('V[.].+[.]Duration', names(data), ignore.case = T )])
  ),
direction="forward",
)

Upvotes: 0

Views: 92

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226182

You can use reformulate to construct a formula, so something like

upperVars <- reformulate(grep('V[.].+[.]Duration', names(data), 
                    ignore.case = TRUE, value=TRUE ), response="lg")
step(..., upper=upperVars)

should work. (You haven't given a reproducible example ...)

Upvotes: 1

Related Questions