Category Archives: R

Data visualisation talk: Presentation using reports package

Why did I use html5 for my today’s talk?   My last presentation was prepared using html5. This time I wanted  some innovation while making the slides.  I prepared  first few slides in Jessyink. Then I got to know that my friend (Trinker) developed an R package, reports,  for creating an amazing presentation including  beamer and reveal. js template.  This is cool!!! Create a presentation in R for R talk. I could write everything in R markdown and convert into beautiful html5 slides.

You could find my presentation just below this blog.  If any of you wanted to recreate the same, here is the R Markdown file Rmd.  I did bit modification in the style file and some tags. Otherwise, you could generate the same. Ensure you have all required packages (check Rmd file) before running the file. The options required for slide creation very well described in the help (reports package).

 Short version of slide creation is as follows:

  1. Install reports package (install.packages(“reports”) or take it from github).
  2. Call the presentation(“your_folder”) function. This creates Rnw and Rmd files inside your_folder/PRESENTATION.
  3. Add contents to Rmd file. Then click knit HTML if you are using RStudio or call knit2html. This would create the html file.
  4. Finally, run reveal. j’s () function. This would create the html5 presentation slides inside your_folder/PRESENTATION/reveal.js(). You could customize the path or theme or transition… I have used the theme as beige and transition as a cube.

This process wouldn’t be smoother for an R beginner who uses Windows. One should make sure all dependencies are set.  Github page provides more help resources, and it is helpful to complete successful creation of your presentation.

Apart from reports one can try slidify  package for creating html5 slides. This got multiple  html5 presentation templates including shower, io2012, etc.

One thing I am a bit unhappy is  that Emacs+ess doesn’t have a Rmd mode. I am sure someone is working on that.  Now it is time for my research work.

Note:Use firefox or chrome browser to view my presentation. left, right, up*, and down* arrows for transition.


PS:  ggplot2 is liked by almost all. As per the feedback, I should spend more time on ggplot2.

Fun with R graphics: A raptor and a cake

R graphics are something I always  explore or experiment with.  Base graphics, ggplot2 are my favorites. On  other graphics packages, I haven’t used plotrix much. I am currently exploring grid package.


First one is using ggplot2. (You could also do this using base graphics).

It would be more fun if we can do animation on Raptors. You can find an animated version of Raptor here. Rcode.


Here is the easy one. This one is a cake using plotrix package.


I have already discussed this graph in my favorite forum talks tats.  I dedicated both the graphs to my friends in the forum. You can find more fun graphs in this forum.


A heuristic enhancement on an optimisation algorithm

Directly or indirectly we heavily use optimization in our life.  Resource or utility optimization is something we hear every day.  Here I will discuss a standard optimization problem in statistics, maximum likelihood estimate. This blog is a small episode of my recent work. I used R language to implement the optimization exercise.

There are packages available for optimization in R. The mostly used are: optim() and  optimize() are in stats package. A dedicated function mle() available in stats4 package. This function is very useful most of the mean part modelling (eg: glm). What we need for this optimization is to prepare a function for the (log) likelihood and gradient (or hessian). We can also specify the algorithm (methods) as any of the following: “Nelder-Mead”, “BFGS”, “CG”, “L-BFGS-B”, “SANN”, and   “Brent”. This optimization output gives the necessary information for the model estimation.

I was working on an extension of bivariate garch model. It includes more than 20 parameters. Let me focus pure garch model, garch(1,1),  as a prototype to explain the algorithm. Here the model equation is in the variance part. Also, the parameters have constraint to ensure the positive variance. The   model equation is given below.

[math]h_t = \omega + \alpha x_{t-1}^2 + \beta h_{t-1}; \omega >0 , \alpha,\beta \geq 0 [/math]

The  optimization function may work for the garch (1,1) estimation. But as the number of parameters increases, there is not much use with optimal functions in R. The main reason for failing the algorithm is the nonconvex surface with sensitive boundaries. So the solution is to write the codes for newton raphson algorithm or BHHH or other modified algorithms.

I wrote BHHH algorithm for my model. I was not able solve it fully, it was not giving the optimum. The reason is the direction for each iteration when it reaches sensitive area is not properly scaled. ie. either it overshoot in some dimension or there is no significant improvement in some direction. So to get appropriate multiplier on optimal direction fails here.

Now I will discuss about the heuristic part of the modification in the algorithm. In this heuristic, certain number of dimensions are randomly selected and perturbed. Remaining dimensions are left unchanged. This way we can overcome the scaling issue. This given me optimal solution but it is not optimised in the execution perspective. It take slightly more time ( I compared few simple models with my heuristic and existing garch estimation).

This approach looks very silly for regression type problems. But in garch like process tweaking like this is very much required. I am still looking for the better way. Anyway the blogging helped me to revise  my approach again. Please let me know if you have any suggestions.