IniziaInizia gratis

Fit a logistic regression model

Once you have your random training and test sets you can fit a logistic regression model to your training set using the glm() function. glm() is a more advanced version of lm() that allows for more varied types of regression models, aside from plain vanilla ordinary least squares regression.

Be sure to pass the argument family = "binomial" to glm() to specify that you want to do logistic (rather than linear) regression. For example:

glm(Target ~ ., family = "binomial", dataset)

Don't worry about warnings like glm.fit: algorithm did not converge or glm.fit: fitted probabilities numerically 0 or 1 occurred. These are common on smaller datasets and usually don't cause any issues. They typically mean your dataset is perfectly separable, which can cause problems for the math behind the model, but R's glm() function is almost always robust enough to handle this case with no problems.

Once you have a glm() model fit to your dataset, you can predict the outcome (e.g. rock or mine) on the test set using the predict() function with the argument type = "response":

predict(my_model, test, type = "response")

Questo esercizio fa parte del corso

Machine Learning with caret in R

Visualizza il corso

Istruzioni dell'esercizio

  • Fit a logistic regression called model to predict Class using all other variables as predictors. Use the training set for Sonar.
  • Predict on the test set using that model. Call the result p like you've done before.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# Fit glm model: model


# Predict on test: p
Modifica ed esegui il codice