# 3D surface plot for least square & ridge 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!

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.
• PLOTZ - you choose a grid of values for the betas and evaluate $\text{SS}_\text{reg}$ at each grid point. Which values are on your grid depends on which parts of the function you want to look at, but to include the minimum it should include the least squares estimate, probably near the center of the grid. IIRC I used outer for this, but expand.grid would be another good choice. – Glen_b Oct 17 '14 at 9:59
• In order to make the sum of squares have the long valley, I had the data in $(x_1,x_2)$ space lie scattered about a straight line. Because many planes pass through the line, there's not much information in the data about which plane it should be (hence the valley - there's many combinations of $(\hat{\beta_1},\hat{\beta_2})$ with nearly the same fit). – Glen_b Oct 17 '14 at 10:49