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")
本练习是课程的一部分
Machine Learning with caret in R
练习说明
- Fit a logistic regression called
modelto predictClassusing all other variables as predictors. Use the training set forSonar. - Predict on the
testset using that model. Call the resultplike you've done before.
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Fit glm model: model
# Predict on test: p