Wednesday, 10 February 2016

Use R for Fun!

This package is a collection of some classical computer games (e.g. the Mine sweeper and Five in a row) as well as other funny stuff.
Some examples:

## install.packages('fun')
library(fun)
if (.Platform$OS.type == "windows") x11() else x11(type = "Xlib")
mine_sweeper()

library(fun)
gomoku()

You can take a look at the list of functions in this package by reading the HTML help page (go to help.start()), and I also need to mention the demos, e.g. see demo('TurtleGraphics') for a demo of Turtle graphics (how many people know the old Logo programming language?), and demo(package = 'fun') for a list of all demos in this package.
 
demo('RealTurtle', package = 'fun')
 
 
Posted by Wathsala Karunarathna
 
 

Tuesday, 9 February 2016

R plots and graph gallery

R has great graphics and plotting capabilities and can produce a wide range of plots very easily. The following is an R plot gallery with a selection of different R plot types and graphs that were all generated with R. In each case you can click on the graph to see the commented code that produced the plot in R. Note that the R code produces pdf files, which I have converted in gimp to png format for displaying on the web.
 
Posted by Nathasha Wijesinghe
 
 
 
 

Saturday, 6 February 2016

Getting Started with R

There are hundreds of websites that can help you learn the language. Here's how you can use some of the best to become a productive R programmer.

  • Learn the basics: Visit Try R to learn how to write basic R code. These interactive lessons will get you writing real code in minutes, and they'll tell you immediately when you go wrong.

  • Broaden your skills: Work through The Beginner's Guide to R by Computerworld Magazine. This 30 page guide will show you how to install R, load data, run analyses, make graphs, and more.

  • Practice good habits: Read the R Style Guide for advice on how to write readable, maintainable code. This is how other R users will expect your code to look when you share it.

  • Look up help: When you need to learn more about an R function or package, visit Rdocumentation.org, a searchable database of R documentation. You can search for R packages and functions, look at package download statistics, and leave and read comments about R functions.

  • Ask questions: Seek help at StackOverflow, a searchable forum of questions and answers about computer programming. StackOverflow has answered (and archived) over 40,000 questions related to R programming. You can browse StackOverflow's archives and see which answers have been up voted by users, or you can ask your own R related questions and wait for a response.
    • If you a have question that is more about statistical methodology there are also plenty of R users active on the the  CrossValidated Q&A community.

  • Keep tabs on the R community: Read R bloggers, a blog aggregator that reposts R related articles from across the web. A good place to find R tutorials, announcements, and other random happenings.

  • Deepen your expertise: Once you've gained some familiarity with R, Advanced R provides an entertaining roadmap to some of the deeper subtleties of the language and how to work with it most effectively.
https://support.rstudio.com/hc/en-us/articles/201141096-Getting-Started-with-R



Posted by Shenali Wijesinghe

Monday, 1 February 2016

R Markdown — Dynamic Documents for R

R Markdown is an authoring format that enables easy creation of dynamic documents, presentations, and reports from R. It combines the core syntax of markdown (an easy-to-write plain text format) with embedded R code chunks that are run so their output can be included in the final document. R Markdown documents are fully reproducible (they can be automatically regenerated whenever underlying R code or data changes).

For more information

http://rmarkdown.rstudio.com/

Posted by Mohamed Abraj

Friday, 29 January 2016

The R User Conference 2016: June 27 - June 30, 2016 Stanford University, Stanford, California

Registration and abstract submission is ongoing.

The annual useR! conference is the main meeting of the international R user and developer community. Its program consists of both invited and user-contributed presentations.
  • The invited keynote lectures cover a broad spectrum of topics ranging from technical and R-related computing issues to general statistical topics of current interest.
  • The user-contributed presentations and posters are submitted as abstracts prior to the conference and may be related to (virtually) any R-related topic. The presentations are typically organized in sessions of either broad or special interest, which also comprise a "free" discussion format. Such a discussion format not only provides a forum for software demonstrations and detailed discussions but also supports the self-organization of the respective communities. The duration is between 15 or 20 minutes for the focus and kaleidoscope sessions, and 5 minutes for lightning talks.
This year the conference will be held at the campus of Stanford University in Stanford, CA. The conference is being organized with support from the Department of Statistics, Stanford University and the Stanford Libraries.
The program will cover topics such as
  • History of R and computing with data
  • Bayesian Statistics
  • Bioinformatics
  • Economics, Finance and Insurance
  • High Performance Computing
  • Reproducible Research
  • Industrial Applications
  • Statistical Learning with Big Data
  • Spatial Statistics
  • Teaching
  • Visualization & Graphics
  • and many more.
The year 2016 will be special in at least two ways: it marks the 40th anniversary of the S language (the language underlying R), and is also the year that John Chambers, an inventor of S, turns 75. Holding the conference at Stanford, the epicenter of Silicon Valley, is especially timely given the prominent position of R in the rapidly expanding fields of big data and data science.
Posted by Thilina Thilakarathna

Saturday, 23 January 2016

Stochastic Processes and Monte Carlo with R

The use of probability and statistics is ubiquitous in quantitative finance. All of the observable prices, volumes, order arrival rates, etc, are due to supply and demand imbalances. However, keeping track of all the supply and demand imbalances becomes cumbersome as the number of variables increases. Statistical tools are vital in explaining and modeling these effects. Stochastic processes and Monte Carlo analysis are some of the tools that are used in the fields of finance, economics and marketing.

http://www.rfortraders.com/lecture-6-stochastic-processes-and-monte-carlo/

Posted by  Prabhani Jayasinghe

Friday, 22 January 2016

GUI Programming Using R

Good news for R Users.
GUI programming now available with R!


A few days back the RStudio blog announced Shiny, a new product for easily creating interactive web applications (http://www.rstudio.com/shiny/). I wanted to compare this new framework to one I’ve worked on, gWidgetsWWW2.rapache – a version of the gWidgets API for use with Jeffrey Horner’s rapache module for the Apache web server (available at GitHub). The gWidgets API has a similar aim to make it easy for R users to create interactive applications.
I don’t want to worry here about deployment of apps, just the writing side. The shiny package uses websockets to transfer data back and forth from browser to server. Though this may cause issues with wider deployment, the industrious RStudio folks have a hosting program in beta for internet-wide deployment. For local deployment, no problems as far as I know – as long as you avoid older versions of internet explorer.
Now, Shiny seems well suited for applications where the user can parameterize a resulting graphic, so that was the point of comparison. Peter Dalgaard’s tcltk package ships with a classic demo tkdensity.R. I use that for inspiration below. That GUI allows the user a few selections to modify a density plot of a random sample.

gWidgetsWWW2

We’ll start with a gWidgets approach and work our way to the shiny code. A basic GUI can be generated with the following code:
 
width <- 600; height <- 480
 
## The plot commands. Uses GUI objects defined below
make_plot <- function(...) {
  y <- get(svalue(distribution))(svalue(size))
  plot(density(y, bw=svalue(bandwidth)/100, kernel=svalue(kernel)))
  points(y, rep(0, svalue(size) ))
}
 
update_plot <- function(...) {
  f <- tempfile()
  require(canvas)
  canvas(f, width=width, height=height)
  make_plot()
  dev.off()
  svalue(cnv) <- f ## update canvas widget
}
 
## The layout
w <- gwindow("tkdensity example")
gstatusbar("Powered by gWidgetsWWW2.rapache and rapache", cont=w)
 
## containers
bl <- gborderlayout(cont=w)
fl <- gformlayout(cont=bl, where="west")
 
## controls
distribution <- gcombobox(data.frame(value=c("rnorm", "rexp"),
                                     labels=c("Normal", "Exponential"),
                                     stringsAsFactors=FALSE),
                          cont=fl, label="distribution")
kernel <- gcombobox(c("gaussian", "epanechnikov", "rectangular",
             "triangular", "cosine"),
                    cont=fl, label="kernel")
size <- gcombobox(c(5,50, 100, 200, 300),
                  coerce.with="as.numeric",
                  cont=fl, label="size")
bandwidth <- gslider(0.05*100, 2*100, by=0.05*100, value=1*100,
                     width=250,
                     coerce.with="as.numeric",
                     cont=fl, label="bandwidth")
refresh <- gbutton("refresh",
                   cont=fl, label="")
 
cnv <- gcanvas(cont=bl, where="center", width=width, height=height)
 
## connect controls
for(i in list(kernel, size, bandwidth, refresh))
  addHandlerChanged(i, update_plot)
All right, that may look like a lot, but lots of people have used gWidgets to write such GUIs. They aren’t that hard. basically there are three parts:
  • The definition of the controls and their layout that comprise the
    user interface.
  • The definition of some callback to create the graphic when a control
    is updated.
  • Some means to specify when to invoke this call back.
The gWidgets API is meant to be cross-toolkit, but this is only mostly true. In the above we use a few web-specific features, including the canvas package to provide a JavaScript canvas for drawing R graphics.

Manipulate

This particular example would be really easy were we able to use RStudio’s manipulate package. That is only for RStudio users though. Well, not really. There is a simple map available in thegWidgetsWWW2.rapache package that allows us to easily use that specification. Here is the code:
 
## load in map for manipulate -> gWidgets
source(system.file("demo", "manipulate.R", package="gWidgetsWWW2.rapache"), local=TRUE)
 
w <- gwindow("Manipulate example")
gstatusbar("Powered by gWidgetsWWW2.rapache and rapache", cont=w)
 
manipulate(
           {
             y <- get(distribution)(size)
             plot(density(y, bw=bandwidth/100, kernel=kernel))
             points(y, rep(0, size))
           },
           ##
           distribution=picker("Normal"="rnorm", "Exponential"="rexp"),
           kernel=picker("gaussian", "epanechnikov", "rectangular",
             "triangular", "cosine"),
           size=picker(5, 50, 100, 200, 300),
           bandwidth=slider(0.05 * 100, 2.00 * 100, step=0.05 * 100, initial=1* 100), # integers needed
           button=button("Refresh"),
           ## gWidgetsWWW2.rapache extras
           container=w,
           device="svg",                # svg or canvas or png
           delay=1000                   # delay to let data load for combo boxes
           )
Okay, I admit. I’m a fan of manipulate and it really is a perfect way to specify an interface as straightforward as this. This one is an example in the gWidgetsWWW2.rapache package and you can see it in action at http://www.math.csi.cuny.edu/gw/ex-manipulate.R (though this redirects to a private server which is often not online). The manipulateinterface handles the layout of the controls and the invocation of the callback, all you add is an expression to plot and a specification of
the controls.

Shiny

Now for Shiny. We noted above there are really three pieces to writing this app, with Shiny we only need to worry about two: the definition and layout of the user interface, and the specification of how the resulting graphic is made. For this, we write two functions: ui.R and server.R.
The ui.R files is used to layout the interface. Here is ours:
library(shiny)
 
shinyUI(pageWithSidebar(
 
  # Application title
  headerPanel("tkdensity"),
 
  # Sidebar with a slider input for number of observations
  sidebarPanel(
               selectInput("distribution", "Distribution:",
                           choices = c("Normal"="rnorm", "Exponential"="rexp")),
               selectInput("kernel", "Kernel:",
                           choices = c("gaussian", "epanechnikov", "rectangular",
                             "triangular", "cosine")),
               selectInput("size", "Size:",
                           choices = c(5,50, 100, 200, 300)),
               sliderInput("bandwidth", "bandwidth:",
                           min = 0.05,
                           max = 2,
                           value = 1)
  ),
 
  # Show a plot of the generated distribution
  mainPanel(
    plotOutput("distPlot")
  )
))
There are a few containers (“panels”) and a few controls specified (“inputs”). The basic usage of shiny involves just a handful of each and they are easy to learn. The above code simply makes a header, puts four controls into a side panel and in the main panel readies a place to display our plots. The name distPlot is odd because I stole this basic set up from one of shiny‘s examples and made just minor changes.
The server.R file is not difficult either. We need to make the graphic so that it displays. Here it is:
library(shiny)
 
shinyServer(function(input, output) {
 
  make_plot <- function(distribution, size, bandwidth, kernel) {
    y <- get(distribution)(size)
    plot(density(y, bw=bandwidth, kernel=kernel))
    points(y, rep(0, size))
  }
 
  output$distPlot <- reactivePlot(function() {
    make_plot(input$distribution, input$size, input$bandwidth, input$kernel)
  })
})
Again, this is only slightly modified from an example. The input object is not a list or an environment, but we can extract the values associated with the user interface as though it were. In the above we see the distributionsizebandwidth and kernel values are passed along to the make_plot call. The only new thing here, besides that, is the magic way we create a plot (passing a function as a call back to reactivePlot and assigning it to distPlot in the output object). The shiny package takes care of the third part of our GUI, using this “reactive” programming style to synchronize various parts of
the interface.
Want to see it at work? Well fire up your copy of shiny and run it from a GitHub gist:
require(shiny)
shiny:::runGist("4009017")
Before you gloss over the above lines, think how neat this is. Just as you can install packages from GitHub within an R session through the devtools package, you can run shiny apps within R from GitHub with the runGist function. This is an awesome feature for deploying interactive web apps for local usage.
I think you’ll see, if you compare, for this task the user experience is nicer with shiny than that of gWidgetsWWW2.rapache. The specification of the GUI is easiest with the manipulate style, but using shiny wasn’t difficult. I personally like very much the use of the “bootstrap” CSS libraries (http://twitter.github.com/bootstrap/), as opposed to the ExtJS CSS (http://www.sencha.com/products/extjs/). The interface looks very modern. (Not only looks, the slider is much better than that ingWidgetsWWW2.rapache, where the underlying control only wants integer values.) Though the gWidgetsWWW2.rapache package might have more controls and more layout options than the basic use of shiny, I can see that there will be a huge number of great shiny apps written. Within its range of use, it is a great choice for easily making R-driven web applications.

 
To Read this :
 
Posted by Thiloka Bogahalanda 
 

Thursday, 21 January 2016

Time Series and Forecasting

R has extensive facilities for analyzing time series data. This describes the creation of a time series, seasonal decomposition, modeling with exponential and ARIMA models, and forecasting with the forecast package.

http://www.statmethods.net/advstats/timeseries.html

Posted by Irosha De Silva