Site icon R-bloggers

Least squares as springs, the Shiny app

[This article was first published on R – Enchufa2, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Three weeks ago, I saw a nice mechanical recreation of a PCA (or total least squares) on Twitter: just by pulling some strings attached to a straw.

When you encode your vectors as strings. https://t.co/xkmdV8TeMc

— Sean J. Taylor (@seanjtaylor) November 15, 2020

Some days ago, Joshua Loftus published Least squares as springs, where the author presents some nice visualisations, and explains that the cost function is the same (except for a constant) as the potential energy of springs attaching the data points to the regression line. As a result, if we take any line attached in this way to a point cloud (with the required constraints in place for the strings: vertical movement for regular regression; no constraints for PCA, the springs slide freely), then the system will oscillate until it reaches the state of minimum energy (i.e., meets the regression line).

Inspired by this and based on the Matlab code for the animation in this excellent StackExchange answer, I created a Shiny app that allows us to play with different parameteres:

The resulting (and oddly satisfying) movement is simulated as a composition of a translation and a rotation. You can play with a plotly-powered JavaScript animation or download it as a gganimate-powered GIF:

Article originally published in Enchufa2.es: Least squares as springs, the Shiny app.

To leave a comment for the author, please follow the link and comment on their blog: R – Enchufa2.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.