0

Suppose that I have an m x n data matrix M where the first column is my binary response variable and the remaining columns are predictors.

I created a multiple logistic regression model using

my.model = glm(M[,1] ~ M[,2] + M[,3] + ... M[,n], family=binomial("logit"))

My question is how I can do this without having to write out the M[,2]+ ... + M[,n] terms, e.g. something along the lines of just:

my.model = glm(M[,1] ~M[2:n], family = binomial("logit"))

(I know that the latter won't work, but I'm looking for something where I can do something analogous using a list/block of the matrix).

Max
  • 487
  • 5
  • 19

1 Answers1

1

why don't you just do:

glm(M[,1] ~., family = binomial("logit))

That will call all the columns or if you want specific columns, filter that out of your dataframe beforehand.

nak5120
  • 4,089
  • 4
  • 35
  • 94
  • Wouldn't that also regress M[,1] on itself? – Max Jul 06 '17 at 16:59
  • Nope, here's an example: https://stackoverflow.com/questions/5774813/short-formula-call-for-many-variables-when-building-a-model – nak5120 Jul 06 '17 at 17:03
  • Thanks, I wasn't familiar with that syntax. – Max Jul 06 '17 at 17:06
  • I just attempted to execute glm(M[,1]~.,family=binomial("logit")) and get the error message "Error in terms.formula: "." in formula and no 'data' argument." Any idea of what may be going wrong? – Max Jul 06 '17 at 19:00