Hi: I have some problems with editing the plot of the splines model. I write the code.
Thanks in advance for your help
Jesús
modspline <- lm(horsepower~ bs(mpg,knots=c(10,20,30),degree=3),data=Auto)
summary(modspline
library(splines)
spline_fit <- lm(Auto$horsepower ~ bs(Auto$mpg,knots=c(10,20,30
)))
rango <- range(Auto$mpg)
npuntos <- seq(from = rango[1], to = rango[2], by = 0.5)
npuntos <- data.frame(mpg=npuntos)
npredic <- predict(spline_fit,newdata = npuntos, se.fit = TRUE, level = 0.95)
head(npredic)
intconf <- data.frame(
inferior = npredic$fit - 1.96npredic$se.fit,
superior = npredic$fit + 1.96npredic$se.fit)
plot(x = Auto$mpg, y =Auto$horsepower, pch = 20, col = "red")
title("Spline cúbica, nodos: 10, 20, 30")
lines(x = npuntos$mpg, npredic$fit, col = "green", lwd = 2)
lines(x = npuntos$mpg, intconf$inferior, col = "blue",
lwd = 2, lty = 2)
lines(x = npuntos$mpg,intconf$superior, col = "blue",
lwd = 2, lty = 2)
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
(the length or mpg and horsepower are equal)