Common problems with complex modeling analysis with R is that model results are often complex objects and getting to values, like model coefficients, demand a lot of manipulations; others vary from one model to another. Fortunately, the broom
package provides nice and easy to use solutions to the problem.
Answers to the exercises are available here.
Please do all exercises using the tidiverse
packages (mostly broom
and ggplot2
.) If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.
Exercise 1
The plot shows displacement vs. miles per gallon for the mtcars
data set. Fit the data with the linear model and display the model’s coefficients as a tidy data.frame
.
Exercise 2
Reconstruct the plot from exercise 1, adding a prediction line.
Exercise 3
Fit the data with inverse relations instead of a linear one. Like before, display the coefficients and add a prediction line to the plot. (Hint: use nls
function)
Exercise 4
Compare the performance of both models.
Exercise 5
To evaluate the model stability, re-fit the better of the models 100 times with the bootstrap procedure and plot histograms of coefficients.
Exercise 6
Plot the predictions from bootstrapped models.
Exercise 7
Fit the model once again, but this time, separately for a different number of cylinders (cyl
variable). Plot the prediction for each group separately, plus the prediction from the previous model.
Exercise 8
Display the model performance summaries. Compare the sum of deviance with a model not grouped by the number of cylinders.
Exercise 9
Apply k-means on the iris
data set testing k
from 2 to 5. Display the performance summary for each number of clusters.
Exercise 10
Plot the Petal.Length
vs. Petal.Width
showing assignment to clusters and centers of the cluster.
Leave a Reply