(This article was first published on

**The power of R**, and kindly contributed to R-bloggers)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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...