Solved – 3D surface plot for least square & ridge regression

rregressionridge regression

I'm very impressed by this plot: Why does ridge estimate become better than OLS by adding a constant to the diagonal?

Does someone has any clue about how to plot this on R? I mean, how to get RSS values for differents parameter estimation? With lm function, I will have only one solution!

Thanks for your help!

Best Answer

It's a plot of the objective function (i.e. the sum of squares, the thing you're trying to minimize with OLS) as a function of the coefficients. One way to compute this is to generate a "grid" of possible coefficients and then compute the sum of squares at each point on the grid.

That is, we fit a regression by finding $\hat\beta \equiv \arg \min_\beta \{ y-x\beta \}$, i.e. the $\hat\beta$ that minimizes $SS_{reg}$. But we can compute $y-x\beta$ for any $\beta$ value. So we can generate a bunch of different $\beta$ values and just plug each one into that $SS_{reg}$ formula. In this case, $\beta$ has two components (i.e. the intercept and slope), so you can visualize all of the possible $SS_{reg}$s as a 3D surface.

As for generating the plot itself, look here for a complete example with R code.