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")
Este ejercicio forma parte del curso
Machine Learning with caret in R
Instrucciones del ejercicio
- Fit a logistic regression called
model
to predictClass
using all other variables as predictors. Use the training set forSonar
. - Predict on the
test
set using that model. Call the resultp
like you've done before.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
# Fit glm model: model
# Predict on test: p