Problem 1

library(splines)

set.seed(123)
n = 12

error = rnorm(n, 0, 0.3)
weight = runif(n, 34, 48)
beta0 = -20.27
beta1 = 1.250
beta2 = -0.01367
height = beta0 + beta1*weight + beta2*(weight)^2 + error
data = data.frame(weight, height)


train = data[1:ceiling(n*.75), ]
test = data[ceiling(n*.75):n, ]

modelLin = lm(height ~ weight, train)
modelSpline = lm(height ~ bs(weight, df = 8), train)

pred.lin = predict(modelLin, test)
pred.spline = predict(modelSpline, test)
## Warning in bs(weight, degree = 3L, knots = c(`16.66667%` =
## 39.2377989967354, : some 'x' values beyond boundary knots may cause ill-
## conditioned bases
lin.meanDelta = mean(pred.lin - test[,2])
spline.meanDelta = mean(pred.spline - test[,2])

abs(spline.meanDelta/lin.meanDelta)
## [1] 27.207

We created two different models, a linear model and a spline df = 8. We calculated the differences between the predicted values and the real values for both models, found their means, and showed the absolute value of the ratio of the spline to linear means. Any value greater than 1 shows that the spline does not predict as well as the linear model.