0

I am trying to build a Logistic Regression model that is able to run regardless of the nature of the predictor variables as long as the response variable stays the same. Here's a sample of what I have in mind:

#Take in the data
newdata = read.csv("book1.csv", head = TRUE) 

#Store the response variable whose column heading you know beforehand
y = "response.variable"

#Identify the predictor variables (this is where I am stuck)
#Below is the algorithm of what I have in mind though 
1) Take remaining column headings except "response.variable"
2) Store them as x = c(other headings)

#Form of model
model.form = reformulate(x, response = y)

#Build model
logit = glm(model.form, family = "binomial", data = newdata)

Any ideas are welcome.

Edit: When I use the code glm(y ~., data = newdata) as suggested by @laterow, it gives an error statement:

 Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]:
 contrasts can be applied only to factors with 2 or more levels
Mikee
  • 783
  • 1
  • 6
  • 18

1 Answers1

0

If your predictor variables are all of the remaining variables in your data.frame, use the following to select them:

x <- names(newdata)[-which(names(newdata) == "response.variable")]

The names() function returns the names of all variables in a data.frame. The super helpful which() function returns the index of the elements in a vector that match some conditional expression.

lmo
  • 37,904
  • 9
  • 56
  • 69