In this exercise, we use the Auto
data set.
There seems to be a nice non-linear relationships between displacement
(the cylinder volume) and the dependent variable mpg
.
We fit a series of non-linear models to investigate the relationship.
Some of the exercises are not tested by Dodona (for example the plots), but it is still useful to try them.
i
, fit a step function of displacement
to predict mpg
with i
cuts.i
-th element of the vector deltas.cut
.
Initialize the vector
deltas.cut
as a vector of length 10 with valuesNA
.
d.min.cut
.
i
, fit a natural spline of displacement
to predict mpg
with i
degrees of freedom.i
-th element of the vector deltas.ns
.
Initialize the vector
deltas.ns
as a vector of length 10 with valuesNA
.
df.min.ns
.
Try to fit the best natural spline model on the plot above:
mpg
vs displacement
using all the data.displacement.grid
of values ranging from the lowest displacement
value in the data to the highest displacement
value observed, in steps of 0.1
.fit.ns
using the optimal degrees of freedom df.min.ns
,mpg
for the entire sequence. Store the result in preds
.preds
on the plot.Assume that:
ISLR2
library has been loadedAuto
dataset has been loaded and attachedboot
library has been loadedsplines
library has been loaded