bdeonovic
bdeonovic

Reputation: 4220

Best way to make a formula with indeterminate number of variables

I have lots of data. I would like to make a formula to use in glm

Data is in a vector like this:

{a1,a2,b1,b2,...}

and I would like to make a formula like this:

Identity ~ a1:a2 + b1:b2 + ...

any suggestions for something quick and easy?

Upvotes: 1

Views: 1259

Answers (1)

jthetzel
jthetzel

Reputation: 3623

I'm not sure if I understand your question. Providing example data would make it easier. Is the following similar to what you are after?

## Create example data
variables <- c("a1", "a2", "b1", "b2", "c1", "c2")
dat <- data.frame(matrix(rnorm(70), ncol = 7))
names(dat) <- c("Identity", variables)

## Create formula
formula <- paste(variables[grep("1$", variables)], variables[grep("2$", variables)], sep = ":", collapse = " + ")
formula <- paste("Identity ~ ", formula, sep = "")

## Run model
glm1 <- glm(formula = formula, data = dat)
summary(glm1)

Above, the formula is created with paste(), and the sep and collapse arguments are used to make everything come together with the appropriate : and +.

Upvotes: 7

Related Questions