# 2 Interesting animations…

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**The power of R**, 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.

So I haven’t had success YET in finding a way to post here the animations, but I thought it would be interesting to show you at least a couple of examples using this software, and I chose 2 pretty interesting ones by Yihui Xie and Xiaoyue Cheng.

The first one is “The Gradient Descent Algorithm”, it follows the gradient to the optimum. The arrows will take you to the optimum step by step. By the end of the animation, you get something like the image above.

The code to generate such animation is:

*library(animation)*

*# gradient descent works*

*oopt = ani.options(ani.height = 500, ani.width = 500, outdir = getwd(), interval = 0.3,*

*nmax = 50, title = “Demonstration of the Gradient Descent Algorithm”,*

*description = “The arrows will take you to the optimum step by step.”)*

*ani.start()*

*grad.desc()*

*ani.stop()*

*ani.options(oopt)*

For the second example I chose an animation called “The k-Nearest Neighbour Algorithm”,where, for each row of the test set, the nearest (in Euclidean distance) training set vectors are found, and the classification is decided by majority vote, with ties broken at random.

By the end of the animation, you will get something like this:

The code to generate such animation is:

*library(animation)*

*oopt = ani.options(ani.height = 500, ani.width = 600, outdir = getwd(), nmax = 10,*

*interval = 2, title = “Demonstration for kNN Classification”,*

*description = “For each row of the test set, the k nearest (in Euclidean*

*distance) training set vectors are found, and the classification is*

*decided by majority vote, with ties broken at random.”)*

*ani.start()*

*par(mar = c(3, 3, 1, 0.5), mgp = c(1.5, 0.5, 0))*

*knn.ani()*

*ani.stop()*

*ani.options(oopt)*

I’ll keep trying to find the way to upload the whole animations and not just the final result these days, wish me luck!

To

**leave a comment**for the author, please follow the link and comment on their blog:**The power of R**.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.