I'm trying to perform ridge regression on the following dataset in Julia, Can anyone share resources to perform ridge regression in code?

Viewed 119

x={10.9, 12.4, 13.5, 14.6, 14.8, 15.6, 16.2, 17.5, 18.3, 18.6} and y={24.8, 30.0, 31.0, 29.3, 35.9, 36.9, 42.5, 37.9, 38.9, 40.5}.

I already performed linear regression using the code:

using DataFrames, CSV
using PyPlot
using Plots
using GLM

x = [10.9, 12.4, 13.5, 14.6, 14.8, 15.6, 16.2, 17.5, 18.3, 18.6] #x-values
y = [24.8, 30.0, 31.0, 29.3, 35.9, 36.9, 42.5, 37.9, 38.9, 40.5]  #Y-values

data = DataFrame(X=x, Y=y)

linearRegressor = lm(@formula(Y ~ X), data)

println(linearRegressor)

linearFit = predict(linearRegressor)
(plot!(x, linearFit,))
display(scatter!(x, y))
1 Answers

Using MLJLinearModels as suggested by Bogumil in the comments

julia> x = [10.9, 12.4, 13.5, 14.6, 14.8, 15.6, 16.2, 17.5, 18.3, 18.6];

julia> y = [24.8, 30.0, 31.0, 29.3, 35.9, 36.9, 42.5, 37.9, 38.9, 40.5];

julia> xₘ = reshape(x, length(x), 1); # since MLJ will expect x to be a Matrix (2d array)

julia> using MLJLinearModels

julia> fit(RidgeRegression(), xₘ, y)
2-element Vector{Float64}:
 2.2488474986814606
 0.24878206004726794
Related