I have data (below), and have carried out linear, ridge, and lasso regression. For lasso and ridge regression I have found the optimal lambda using cross validation. I now want to superimpose the fitted models onto a y vs x plot of my original data. I have the linear model on the graph, I just can't figure out how to get the other two to appear. I have attempted it in ggplot, but an answer in base R would be really helpful too! Even if you could point me in the right direction, that would be great.
I have the models all working fine. I have the linear regression line on the plot. However when I try to plot the other two fits in the same way it won't work.
code to create the data
set.seed(1)
x <- rnorm(100)
y <- 1 + .2*x+3*x^2+.6*x^3 + rnorm(100)
d <- data.frame(x=x,y=y)
d$x2 <- d$x^2
d$x3 <- d$x^3
d$x4 <-d$x^4
d$x5 <-d$x^5
linear regression
f <- lm(y ~ ., data=d)
ridge regression
library(glmnet)
x <- model.matrix(y ~ ., data=d)
y <- d$y
grid <- 0.001:50
ridge.fit <- glmnet(x,y,alpha=0, lambda = grid)
cv <- cv.glmnet(x,y)
r.fit.new <- glmnet(x,y,alpha=0, lambda = cv$lambda.min)
lasso
lasso.fit <- glmnet(x,y,alpha=1, lambda = grid)
l.fit.new <- glmnet(x,y,alpha=1, lambda = cv$lambda.min)
graph
ggplot(data=d, aes(x=x, y=y)) + geom_point() + geom_line(aes(y=fitted(f)), colour="blue")