# R Programming/Print version

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/R_Programming

# Introduction
## What is R ?

R is statistical software which is used for data analysis. It includes a huge number of statistical procedures such as t-test, chi-square tests, standard linear models, instrumental variables estimation, local polynomial regressions, etc. It also provides high-level graphics capabilities.
There are a few minor similarities between R and C programming languages, but they both run in different ways.

## Why use R?

* R is free software. R is an official GNU project and distributed under the Free Software Foundation General Public License (GPL).
* R is a powerful data-analysis package with many standard and cutting-edge statistical functions. See the Comprehensive R Archive Network (CRAN)'s Task Views to get an idea of what you can do with R.
* R is a programming language, so its abilities can easily be extended through the use of user-defined functions. A large collection of user-contributed functions and packages can be found in CRAN's Contributed Packages.
* R is widely used in political science, statistics, econometrics, actuarial sciences, sociology, finance, etc.
* R is available for all major operating systems (Windows, Mac OS, GNU-Linux).
* R is object-oriented. Virtually anything (e.g., complex data structures) can be stored as an R object.
* R is a matrix language.
* R syntax is much more systematic than Stata or SAS syntax.
* R can be installed on your USB stick.

### Alternatives to R

* S-PLUS is a commercial version of the same S programming language that R is a free version of.
* Gretl is free software for econometrics. It has a graphical user interface and is nice for beginners.
* SPSS is proprietary software which is often used in sociology, psychology and marketing. It is known to be easy to use.
* GNU PSPP is a free-software alternative to SPSS.
* SAS is proprietary software that can be used with very large datasets such as census data.
* Stata is proprietary software that is often used in economics and epidemiology.
* Julia is a general programming language, with capabilities similar to MATLAB, R and Python (and speed of C), and can call libraries from all those.
* MATLAB is proprietary software used widely in the mathematical sciences and engineering.
* Octave is free software similar to MATLAB. The syntax is the same and MATLAB code can be used in Octave.
* Python is a general programming language. It includes some specific libraries for data analysis such as Pandas ·.
Beginners can have a look at GNU PSPP or Gretl. Intermediate users can check out Stata. Advanced users who like matrix programming may prefer MATLAB or Octave. Very advanced users may use C or Fortran.

### See also

* "R Relative to Statistical Package" by Patrick Burns

## R programming style

* R is an object oriented programming language. This means that virtually everything can be stored as an R object. Each object has a class. This class describes what the object contains and what each function does with it. For instance, plot(x) produces different outputs depending on whether x is a regression object or a vector.
* The assignment symbol is "<-". Alternatively, the classical "=" symbol can be used.
The two following statements are equivalent :
* Arguments are passed to functions inside round brackets (parentheses).
* One can easily combine functions. For instance you can directly type
* The symbol "#" comments to the end of the line:
* Commands are normally separated by a newline. If you want to put more than one statement on a line, you can use the ";" delimiter.
* You can also have one statement on multiple lines.
* R is case sensitive: a and A are two different objects.
* Traditionally underscores "_" are not used in names. It is often better to use dots ".". One should avoid using an underscore as the first character of an object name.
* You can also use the pipe operator |>.

## How you can help

Here are some things editors do to keep this book internally consistent. If you have something to contribute, go ahead and make your contribution. Other editors can touch up your edits afterwards so that they conform to the guidelines.
The local manual of style WB:LMOS for the R programming book, including a brief explanation of why we do it that way, is:
* Examples use "source" tags : <syntaxhighlight lang="rsplus"> a <- 1:10 ; mean(a) </syntaxhighlight>. That makes them look pretty to our readers.
* The name of packages are in bold : '''Hmisc'''.
* Name of functions are in "code" tags: <code>lm</code>.
* Page titles -- the part after "R Programming/" -- are in sentence case, like "R Programming/Working with data frames". We couldn't decide between sentence case and title case, so I flipped a coin.
* Every page has <noinclude>{{R Programming/Navigation}}</noinclude> at the top and {{R Programming/Navbar|Mathematics|Probability Distributions}} at the bottom. That makes it easier to navigate from one page to another online.

### See Also

* Google's R Style Guide : a set of rules for R programmers

## References

* ↑ Portable R by Andrew Redd http://sourceforge.net/projects/rportable/
* ↑ "Python Data Analysis Library". pandas.pydata.org/. Retrieved February 14, 2013.
* ↑ "Getting started with Pandas". blog.kaggle.com. January 17, 2013. Retrieved February 14, 2013.

# Sample Session

This page is an introduction to the R programming language. It shows how to perform very simple tasks using R. First you need to have R installed (see the Settings page). If you use Windows or Mac OS, the easiest solution is to use the R Graphical User Interface (click on its icon). If you use Linux, open a terminal and type R at the command prompt.
Usually when you open R, you see a message similar to the following in the console:
You can type your code after the angle bracket >.
R can be used as a simple calculator and we can perform any simple computation.
We can also store numeric or string objects using the assignment operator, <-.
We can also store vectors.
We can also describe the vector with length, mean and var.
We can also define a matrix.
We can plot the data using plot.
We can define a dataframe.
You can save an R session (all the objects in memory) and load the session.
We can define a working directory. Note for Windows users : R uses slash ("/") in the directory instead of backslash ("\").
There are some special characters in R
* NA : Not Available (i.e. missing values)
* NaN : Not a Number (e.g. 0/0)
* Inf: Infinity
* -Inf : Minus Infinity.
For instance 0 divided by 0 gives a NaN but 1 divided by 0 gives $+\infty$
We can exit R using q. The no argument specifies that the R session is not saved.

# Manage your workspace

This page explains how to manage your workspace.

## Basic functions

* ls lists the objects in your workspace.
* list.files lists the files located in the folder's workspace
* rm removes objects from your workspace; rm(list = ls) removes them all.
Each object can be saved to the disk using the save function. They can then be loaded into memory using load.
* save.image saves your workspace.

## Informations about the session

* sessionInfo gives information about your session, i.e., loaded packages, R version, etc.
* R.version provides information about the R version.

## Memory usage

Note: According to R version 3.5.1 on Linux and Mac, memory.size and memory.limit are Windows-specific.
memory.size gives the total amount of memory currently used by R.
memory.limit without any argument gives the limit of memory used by R. This can also be used to increase the limit. The maximum amount is limited by the memory of the computer.
object.size returns the size of an R object. You can print the results and choose the unit (byte,kilobytes,megabytes,etc).
memory.profile returns more details.
* gc initiates the garbage collector which causes R to free memory from objects no longer used.

## External links

* Dumping functions from the global environment into an R script file

# Settings

This page show how to install R, customize it and choose a working environment. Once you have installed R, you may want to choose a working environment. This can be a simple text editor (such as Emacs, Vim or Gedit), an integrated development interface (IDE) or graphical user interface (GUI). RStudio is now a popular option.

## Installation
### Linux

Installing R on Debian-based GNU/Linux distributions (e.g. Ubuntu or Debian itself) is as simple as to type in sudo aptitude install r-base or sudo apt-get install r-base (don't forget that this has to be done as root), or installing the package r-base using your favourite package manager, for example Synaptic.
There is also a bunch of packages extending R to different purposes. Their names begin with r-. Take a closer look at the package r-recommended. It is a metapackage that depends on a set of packages that are recommended by the upstream R core team as part of a complete R distribution. It is possible to install R by installing just this package, as it depends on r-base.
Installation with apt-get (Debian, Ubuntu and all linux distributions based on Debian)
Installation with aptitude (Debian, Ubuntu and all linux distributions based on Debian)

### Mac OS

Installation : Visit the R project website (http://r-project.org/), select the "CRAN" page and choose mirror. Download the disk image (dmg file) and install R.
The default graphical user interface for Mac is much better than the one for Windows. It includes
* a dataframe manager,
* a history of all commands,
* a program editor which supports syntax highlighting.

### Windows

(Section source)

#### Download

To install R under Windows operating system you have to download the binaries from the web. First go to r-project.org and click CRAN under download section on the left panel and select a mirror site, from where you could download the required content. The best idea is pick a mirror closest to your actual geographical location, but other ones should work as well. The click Windows and in subdirectories base. The windows binary is the exe file, in form R-x.x.x-win32.exe, where x denotes the actual version of the program. Regardless of the version the setup has the same steps.

#### Setup

As usual in Windows, if you just keep clicking the Next button, you will install the program without any problems. However, there are few things that you can alter.
* On the welcome screen click Next.
* Read or just notice the GNU license, and click Next.
* Select the location, where R should be installed. In case you don't prefer a particular location on your hard disc, the default choice will be OK for you.
* During the next step you can specify which parts of R you want to install. Choices are: User installation, Minimal user installation, Full installation and Custom installation. Notice the required space under the selection panel (varies between 20 and 66 MB). In case you are a beginner in R, choose the default User installation.
* In this step you can choose between 2 ways. If you accept defaults, you skip the 3 "extra" steps during installation (see lower).
* You can specify the Start menu folder.
* In the next step you can choose, between shortcut possibilities (desktop icon and/or quick launch icon) and specify registry entries.
With these steps you can customize the R graphical user interface.
* You can choose if you want an R graphic user interface covering the whole screen (MDI) or a smaller window (SDI).
* You can select the style, how the Help screen is displayed in R. You will use help a lot, so this may be an important decision. It is up to you, which style you prefer. Please note, that the content of help file will be the same regardless of your choice. Here you specify just the appearance of that particular window.
* In the next step you can specify, whether you want to use internet2.dll. If you are a beginner, pick the Standard option here.

#### Update

Updating R on Windows requires several steps:
* Downloading/installing the latest version of R
* Copying your packages from the library folder to the one in the new R installation
Both of these steps can easily be done using the installr package, by running the following command (which would both install the package, and update R):
There is also the possibility of using a "global" package library, see here for more details.

### Portable R for Windows

You have a portable version if you want to install R on your USB stick. This is useful if you don't have admin rights on a computer. The basic installation requires something like 115 mb but you may need more if you want to install add-on packages.

## Working environment

Once you have installed R, you need to choose a working environment. In this section, we review all possible working environment. This include a basic terminal as well as integrated development environment (therefore IDE), text editors or graphical user interface (therefore GUI).
* A graphical user interface provides some menu which makes it possible to run R without writing code. This is a good solution for beginners.
* A text editor makes it easy to write code.
* An integrated development environment provides a text editor and a compiler which makes it easy to write R scripts, to run them and to correct them.
Note that there are some task specific GUIs. For instance speedR provides a GUI to import data into R.

### Terminal

For Linux and Mac OS users it is possible to use R from the terminal.

### R Gui

For Mac OS and Windows users, there is a graphical user interface. In Mac OS, the GUI includes a package manager, a program editor with syntax highlighting and a data browser. In Windows, the GUI is not better than a Terminal.

### Graphical User Interface

This section includes material for beginners (eg people who are not familiar with computing).

#### Poor Man's GUI (pmg)

A simple GUI for learning R. It is recommanded for beginners.

#### Jaguar : Java GUI for R

* Jaguar : Java GUI for R is available for Linux, Mac and Windows (screenshots).
* It is good for beginners.

#### R commander

* Rcommander developed by John Fox provides a menu in the standard Graphical User Interface (screenshots).
* It works on Linux, Mac and Windows.
* It is a good interface for beginners and for people who are not used to script editing.
* Ubuntu users can also install R Commander from the software center.

### Integrated development environment
#### RStudio

RStudio is an integrated development interface for R.
* It works on Mac, Windows and Linux platforms.
* It supports Sweave and LaTeX.
* It includes syntax highlighting for R, LaTeX and Sweave.
* It includes a way to view variables and dataframes.
* It makes it easy to load and install package, to navigate in the help files and to manage your workspace.
* It supports code and file name completion.
* It can be installed on a USB stick.
John Verzani has written a book dedicated to this new interface, Getting Started with RStudio and Jeffrey Racine recommand RStudio for Sweave.

#### RKward

RKward is an IDE and a GUI for Linux (KDE) (Screenshots). RKWard aims to provide an easily extensible, easy to use IDE/GUI for R. RKWard tries to combine the power of the R-language with the (relative) ease of use of commercial statistics tools.

#### Eclipse with StatET

Eclipse with the StatET plugin provides an IDE for R.
* It supports Sweave.

#### Rattle GUI

* Rattle for Linux, Windows and Mac (screenshots).

#### Tinn R

* For Windows only
* Tinn R is a good IDE for Windows users. One can easily define keyboard shortcuts to execute selected R code from Tinn R.

#### Notepad++ and NpptoR

* For Windows only.
Notepad++ and NPPtoR provides syntax highlighting and hotkeys (by default F8) to send lines of code to R. Syntax highlighting can be easily modified using the dialog box to manage user define languages (Menu/View/Use Define Dialog...). NPPtoR provides a method to generate syntax highlighting dynamically (depending on all the available packages in the R environment).

#### Vi, Vim and GVim

* Vim and GVim provides syntax highlighting
* Vim is for advanced users only
* The Vim-R-plugin allows the communication between Vim and R

#### Emacs and ESS

* Emacs with ESS (Emacs Speaks Statistics).
* For Linux users, you just have to install emacs and ESS using your standard package manager (synaptic, aptitude, yum, etc)
* For Mac and Windows user, you can have a look at Vincent Goulet's page which has binary with Emacs and ESS.
* For Mac users, Aquamacs Emacs is a good solution. It is an enhancement of the standard Emacs editor.
* For Windows users, XEmacs is a good solution.
* Once the installation of Emacs and ESS is done, you just have to open Emacs and open or create a file with extension .R (C-x C-f). ESS will be automatically loaded.
** C-c M-j evaluates the current line
** C-c M-r evaluates the current region
** C-c M-b evaluates the current buffer
* See John Fox webpage http://web.archive.org/web/20050516104010/http://socserv.mcmaster.ca/jfox/Books/Companion/ESS/ess-xemacs.pdf to learn about Emacs and ESS

#### WinEdt

* How to use R for Windows with the RWinEdt extension ? by Andy Eggers
* WinEdt is not open source
* WinEdt is for Windows only.
* Install the RWinEdt package.

#### gedit with gedit-r-plugin

* For Linux users only.
* There is also a plugin for gedit called gedit-r-plugin. This can be installed using Synaptic or any other package manager on a linux platform.

## Customizing R
### R profile

R can be customized using the Rprofile file. On Linux, this file is stored in the home directory. You can edit it by running the following command in a terminal :
If you use some packages very often, you can load them systematically using the Rprofile file. You can also change the default options.

### Options

The function options without any argument show all options
The linguistic and encoding options can be modified using Sys.setlocale :
By default, error messages are in the local language. However, it is possible to set them in English using Sys.sentev

## References

* ↑ This section was imported from the Wikiversity project Installation, How to use R course
* ↑ Updating R from R (on Windows) – using the {installr} package
* ↑ Portable R http://sourceforge.net/projects/rportable/
* ↑ http://jgr.markushelbig.org/JGR.html
* ↑ http://socserv.mcmaster.ca/jfox/Misc/Rcmdr/
* ↑ rstudio.org
* ↑ John Verzani "Getting Started with RStudio An Integrated Development Environment for R", O'Reilly Media, September 2011
* ↑ Jeffrey Racine, (forthcoming), "RStudio: A Platform Independent IDE for R and Sweave," Journal of Applied Econometrics.
* ↑ StatET : http://www.walware.de/goto/statet
* ↑ Rattle : http://rattle.togaware.com/
* ↑ Graham J Williams. Rattle: A Data Mining GUI for R. The R Journal, 1(2):45-55, December 2009
* ↑ Tinn stands for Tinn Is Not Notepad http://www.sciviews.org/Tinn-R/
* ↑ Note that Notepad++ can be installed on a USB stick http://sourceforge.net/projects/notepadpluspe/
* ↑ NPPtoR is also a portable software http://sourceforge.net/projects/npptor/
* ↑ ESS : http://ess.r-project.org/
* ↑ Vincent Goulet Emacs page http://vgoulet.act.ulaval.ca/emacs
* ↑ http://www.people.fas.harvard.edu/~aeggers/RWinEdt_installation.pdf

# Documentation
## Obtaining Help

For each package you have a reference manual available as an HTML file from within R or as a PDF on the CRAN website. You also often have Vignettes or comprehensive articles in the R Journal, the Journal of Statistical Software, etc.
You can search for help inside all loaded packages using help or ?. Usually you do not need to add quotes to function names, but sometimes it can be useful. args gives the full syntax of a function.
apropos and find looks for all the functions in the loaded packages containing a keyword or a regular expression.
You can search for help in all installed packages using help.search or its shortcut ??.
RSiteSearch looks for help in all packages and in the R mailing lists. The sos package improves the RSiteSearch function with the findFn function. ??? is a wrapper for findFn.
hints in the hints package suggests what to do with an object.

## Handouts

* An Introduction to R The R Reference Manual
* Robert Kabacoff's Quick R
* Grant Farnsworth's Econometrics in R The best introduction for an economist (about 20 pages)
* UCLA R Computing Resources
* A Handbook of Statistical Analyses Using R by Brian S. Everitt and Torsten Hothorn
* fr+en Arthur Charpentier's R for acturies
* Dan Goldstein's video tutorial
* fr Julien Barnier's introduction to R for sociologists
* Rosetta Code presents solutions to the same task in different programming languages.
* 'R language for programmers', by John Cook
* A Brief Guide to R Beginners in Econometrics
* R Tutorial by Kelly Black

## Teaching Resources

* François Briatte has a nice introduction to data analysis using R
* Simon Jackman Political Methodology Classes
* Jonathan Katz Political Methodology Classes
* A Brief Guide to R for Beginners in Econometrics
* PRISM luncheons
* Statistical Analysis: an Introduction using R - which includes a course on R
* Biostatistics with R aka A R companion to Wayne Daniel 's Biostatistics Book

## Blogs

* Planet R the first R blog aggregator
* R Bloggers The news pulse for the R blogosphere
* "R" you Ready ?
* One R Tip a Day
* Revolution computing blog
* Yu Sung Su's Blog:R
* (fr) Freakonometrics (in French) lots of code chunks
* (fr) Baptiste Coulmont (in French)
* (fr) Quanti Sciences Sociales (in French) R blog for sociologists

## Journals

* The R Journal
* Journal of Statistical Software contains lots of articles on R packages.
* The Political Methodologist contains lots of articles on R for political scientists.

## Books

* Venables and Ripley : Modern Applied Statistics with S
** A very good introduction to R covering numerous topics.
* A Handbook of Statistical Analyses Using R (Brian S. Everitt and Torsten Hothorn, Chapman & Hall/CRC, 2008)
* An Introduction to Data Technologies, by Paul Murrell
** Everything you need to know about data management
* A first course in statistical programming with R, John Braun and Duncan Murdoch.
* Peter Dalgaard (2009). ISwR: Introductory Statistics with R. R package version 2.0-4. http://CRAN.R-project.org/package=ISwR
* Springer Use R Series
* John Fox : An R and S-PLUS Companion to Applied Regression
* Gelman Hill : Data Analysis using Regression and Multilevel Hierarchical Models

## useR and other R conferences

* useR! 2009
* useR! 2010
* London R homepage
* R / Finance conferences in 2009 and 2010

## Search Engine

* R seek
* Google Code Search with keyword "lang:r" gives access to r programs including the request. For instance the following request optim lang:r gives access to all the r programs including optim.

## Q&A / Forums

* Nabble R http://r.789695.n4.nabble.com/
* Stackoverflow
* The #rstats hashtag on Twitter
* IRC: #r@freenode
* r-soc : mailing list for French sociologist

## References

* ↑ If you want to know more about regular expressions, have a look at the Regular expressions section in the Text Processing page.
* ↑ Introduction to Data Analysis

# Control Structures
## Conditional execution

* Help for programming :
if accepts a unidimensional condition.
The unidimensional condition may be one of TRUE or FALSE, T or F, 1 or 0 or a statement using the truth operators:
* x == y "x is equal to y"
* x != y "x is not equal to y"
* x > y "x is greater than y"
* x < y "x is less than y"
* x <= y "x is less than or equal to y"
* x >= y "x is greater than or equal to y"
And may combine these using the & or && operators for AND. | or || are the operators for OR.
The ifelse command takes as first argument the condition, as second argument the treatment if the condition is true and as third argument the treatment if the condition is false. In that case, the condition can be a vector. For instance we generate a sequence from 1 to 10 and we want to display values which are lower than 5 and greater than 8.

### Sets

R has some very useful handlers for sets to select a subset of a vector:
to exclude a subset of a vector:

## Loops
### Implicit loops

R has support for implicit loops, which is called vectorization. This is built-in to many functions and standard operators. for example, the + operator can add two arrays of numbers without the need for an explicit loop.
Implicit Loops are generally slow, and it is better to avoid them when it is possible.
* apply can apply a function to elements of a matrix or an array. This may be the rows of a matrix (1) or the columns (2).
* lapply applies a function to each column of a dataframe and returns a list.
* sapply is similar but the output is simplified. It may be a vector or a matrix depending on the function.
* tapply applies the function for each level of a factor.
* See also aggregate which is similar to tapply but is applied to a dataframe instead of a vector.

### Explicit loops

R provides three ways to write loops: for, repeat and while. The for statement is excessively simple. You simply have to define index (here k) and a vector (in the example below the vector is 1:5) and you specify the action you want between braces.
When it is not possible to use the for statement, you can also use break or while by specifying a breaking rules. One should be careful with this kind of loops since if the breaking rules is misspecified the loop will never end. In the two examples below the standard normal distribution is drawn in as long as the value is lower than 1. The cat function is used to display the present value on screen.
The next statement can be used to discontinue one particular cycle and skip to the “next”.

### Iterators

* Loops in R are generally slow. iterators may be more efficient than loops. See this entry in the Revolution Computing Blogs

## References
# Working with functions
## Looking at the code of a function

* You can type the name of the function in the console without any round brackets after the name. This will print the code of the function in the console.
* You can also use the page function which opens a new editor window and prints the code of the function in this editor.
* You can also use the trCopy function in the TinnR package to copy the code of the function. Then you just have to paste it in a text editor to have a look at it.
Here is an example with the lm function.

## Creating your own function
### Returning an object

By default the value of the last line (*) is returned. In the following example, we have a simple function with two objects. The last one is returned.
The function can return an object explicitly using return (but as it is the last line, you could simply use x instead):
* ) More precisely, it is not the "last line" but rather the value of the last evaluation which is returned from the function.

### Adding arguments

It is possible to add arguments.
Note that the above function would rather be written (and be more efficient) as
(as the last value is returned)
The ... argument means that you can add other arguments which will be passed to functions inside the function.
It is possible to add a dataframe as argument. Here is an example :
For estimation commands it is possible to add formulas as arguments. For instance, we can create our own function for ordinary least square using a formula interface.

## Recursive functions

R supports recursive functions. The function below computes Fibonacci numbers recursively.

## Functions as Objects

R functions can be treated as objects
This can be useful when wanting to make many different kinds of functions

## Higher-order functions

You can use higher-order functions in R. Contrary to common belief, using them instead of loops, is not faster, because the apply function has a for-loop inside its definition. Use them only to improve clarity of your code.

### apply

apply is the most basic of R's map functions. lapply, sapply and mapply are convenient interfaces for apply that work on lists, vectors and multiple vectors respectively.
apply takes as arguments an array, a vector of the dimension to map along and a function. The following example is based on the apply documentation. It uses apply to compute column and row sums of a matrix.

### tapply

tapply is similar to apply, but applies a function to each cell of a ragged array, that is to each (non-empty) group of values given by a unique combination of the levels of certain factors.

### Reduce

This function from the Reduce documentation cumulatively adds

## References

* ↑ http://r.789695.n4.nabble.com/Optional-data-argument-for-a-function-tp850247p850247.html
* ↑ Patrick Burns, The R Inferno, p. 24

# Debugging
## Some basic tips

* Use print statements in your functions to print variable values. Although this technique is considered low-tech or even old fashioned by some, it can still be a quick and easy way to trace an error.
* Place a browser statement in the function just before the crashing line. When the function is called, it will be executed up to the browser line. The command-line interface then switches to the function environment, so that all variables in the function can be inspected or changed. See below for commands available in browser mode.

## Tracing errors with traceback

A standard error message in R will tell you which function threw the error. Consider as an example the following function whose sole purpose is to throw an error.
A call to myFun gives
After an error is raised, the traceback function allows you to show the call stack leading to the error. For example, the function below calls myFun.
Calling myFun2 and traceback gives
The traceback function can be executed automatically each time an error is raised with the option
It may be switched off again with

## Executing code line by line

A function can be executed by setting it to debugging mode with
Then, when the function is called, and a browser in that function's environment is opened so that it can be executed line by line. In the debugging browser, apart from all standard R functionality, the following commands are available.
| Command | Meaning |
| n | Advance to next step. An empty line also works. |
| c, cont | Continue to the end of the current context. E.g. to the end the loop within a loop or to the end of the function. |
| where | Print the stack of function calls (where are you?) |
| Q | Exit the browser and return to the top-level R prompt. |
Debugging can be switched off with
There are a few related functions as well:
* debugonce Switch off debugging after the first call.
* isdebugged Check if a function is in degugging mode.

## Browsing the call stack

This is the most advanced debugging option in R base. By setting options(error=recover) you get the opportunity to browse any environment in the call stack. For example,
By typing '1' or '2' behind Selection: the browser will jump to the selected environment. Once in the browser, all standard R functionality is at your disposal, as well as the commands in the table below.
| Command | Meaning |
| c, cont | Exit the browser and continue at the next statement. An empty line will do the same. |
| n | Enter the step-through debugger (this changes the meaning of c) |
| where | Print a stack trace of active function calls (where are you in the stack?). |
| Q | Exit the browser, do not continue at the next statement but go back to the top-level R browser. |
Recovery mode can be switched off by

# Using C or Fortran

For some tasks, R can be slow. In that case, it is possible to write a program in C or Fortran and to use it from R. This page is for advanced programmers only.
* See wikiversity Connecting Fortran and R
* Link C with R

## References
# Utilities

This page includes material about some utilities. Most of the functions presented here have nothing to do with statistical analysis but may be useful when working on a project. Many functions are just similar to standard unix functions.

## System (Unix/DOS)

system gives access to the system (DOS or unix). The option wait=FALSE means that you don't ask R to wait that the task is finished.
Some examples :
* You can convert an image from to PS to PNG using the unix convert function of your computer. If you want to know more about this function, open a Terminal application and type man convert (This should work on Mac OS and Linux).
* You can open Stata and run a program.
* You can run pdflatex from R and directly open the pdf in a pdf browser.
See also sys in the Hmisc package, shell and shell.exec.

## File Handling

dir lists all the files in a directory. It is similar to the Unix function ls. dir.create creates a new directory. It is similar to mkdir in Unix.
file.info gives information about a file.
Removing files with a specific pattern :
* file.edit opens a file in the text editor.
* file.show opens a file in a new window.
* tempfile creates a temporary file.
* getZip in the Hmisc package.

## Internet

browseURL opens an URL using an internet browser. download.file download a file from the internet.
To see the default browser, use getOption
We can change the default browser using the options command. It is safer to store the options before.
You can download a file from the internet using download.file. Note that very often you don't need to download a file from the internet and you can directly load it into R from the internet using standard functions. For instance, if you want to read a text file from the internet, you can use read.table, scan or readLines.
See also RCurl

## Computing time

If you perform computer intensive task you may want to optimize the computing time. Two functions are available system.time and proc.time. Both returns a vector of values. The first is the standard CPU time.

## Computing process

user.prompt (Zelig) makes a pause in the computation process (useful if you want to do a demo). waitReturn (cwhmisc) does the same job. Sys.sleep stop the computation during a few seconds.
It is possible to stop the computing process if a logical condition is not true using stopifnot.

## Miscellanous

* trCopy (TinnR package) copy an object to the clipboard. It is useful if you want to copy a large object to the clipboard. For instance, if you want to copy the code of a function and paste it in a text editor.
* sessionInfo gives information on the current session info (R version + loaded packages). This function may be useful for reproducible computing. getRversion gives the current R version. R.version gives more details about the computer and R.Version returns the same informations as a list.

## See Also

* See the R.utils package

## References

* ↑ Henrik Bengtsson (2009). R.utils: Various programming utilities. R package version 1.1.7. http://CRAN.R-project.org/package=R.utils

# Estimation utilities

This page deals with methods which are available for most estimation commands. This can be useful for all kind of regression models.

## Formulas

Most estimation commands use a formula interface. The outcome is left of the ~ and the covariates are on the right.
It is easy to include multinomial variable as predictive variables in a model. If the variable is not already a factor, one just need to use the as.factor function. This will create a set of dummy variables.
For instance, we can use the Star data in the Ecdat package :
I takes arguments "as is". For instance, if you want to include in your equation a modified variable such as a squarred term or the addition of two variables, you may use I.
It is easy to include interaction between variables by using : or *. : adds all interaction terms whereas * adds interaction terms and individual terms.
It is also possible to generate polynomials using the poly function with option raw = TRUE.
There is also an advanced formula interface which is useful for instrumental variables models and mixed models. For instance ivreg (AER) uses this advanced formulas interface. The instrumental variables are entered after the |. See the Instrumental Variables section if you want to learn more.

## Output

In addition to the summary and print functions which display the output for most estimation commands, some authors have developed simplified output functions. One of them is the display function in the arm package. Another one is the coefplot in the arm package which displays the coefficients with confidence intervals in a plot. According to the standards defined by Nathaniel Beck, Jeff Gill developped graph.summary. This command does not show useless auxiliary statistics.
| R code | Output |
| source("http://artsci.wustl.edu/~jgill/Models/graph.summary.R") N <- 1000 u <- rnorm(N) x1 <- 1 + rnorm(N) x2 <- 1 + rnorm(N) + x1 y <- 1 + x1 + x2 + u graph.summary(lm(y ~ x1 + x2)) | Family: gaussian Link function: identity Coef Std.Err. 0.95 Lower 0.95 Upper CIs:ZE+RO (Intercept) 0.980 0.056 0.871 1.089 |o| x1 1.040 0.043 0.955 1.125 |o| x2 0.984 0.031 0.923 1.045 |o| N: 1000 Estimate of Sigma: 0.998 |
| library("arm") display(lm(y ~ x1 + x2)) | lm(formula = y ~ x1 + x2) coef.est coef.se (Intercept) 0.89 0.05 x1 1.05 0.04 x2 1.02 0.03 --- n = 1000, k = 3 residual sd = 0.96, R-Squared = 0.86 |

## Delta Method

* If you want to know the standard error of a transformation of one of your parameter, you need to use the delta method
* deltamethod in the msm package.
* delta.method in the alr3 package.
* deltaMethod in the car package.

## Zelig : the pseudo-bootstrap method

Zelig is a postestimation package which simulates in the distribution of the estimated parameters and computes the quantities of interest such as marginal effects or predicted probabilities. This is especially useful for non-linear models. Zelig comes with a set of vignettes which explain how to deal with each kind of model. There are three commands.
* zelig estimates the model and draws from the distribution of estimated parameters.
* setx fixes the values of explanatory variables.
* sim computes the quantities of interest.

## References

* ↑ Nathaniel Beck "Making regression and related output more helpful to users" The Political Methodologist 2010 http://politics.as.nyu.edu/docs/IO/2576/beck_tpm_edited.pdf
* ↑ Jeff Gill graph.summary http://artsci.wustl.edu/~jgill/Models/graph.summary.s
* ↑ See the example on the UCLA Statistics webpage : http://www.ats.ucla.edu/stat/r/faq/deltamethod.htm
* ↑ Kosuke Imai, Gary King and Olivia Lau (2009). Zelig: Everyone's Statistical Software. R package version 3.4-5. http://CRAN.R-project.org/package=Zelig

# Packages

An R package includes a set of functions and datasets. Packages are often developed as supplementary material to books. For instance the MASS package was developed by Venables and Ripley for their book Modern Applied Statistics with S and the car package was developed by John Fox for his book An R and S plus Companion to Applied Regression.

## Load a package

A package is loaded into the current R environment using the library function. A list of functions and datasets included in a package can be obtained by using the h or help argument of the library function.
A package can be detached from the current environment by using the detach function:
Without any arguments the library function lists all of the packages currently available to the user. env (gdata) describe all loaded environments (ie packages). search gives the list of all loaded packages.
current.packages (Zelig) show all the required and suggested packages.
Where are my packages stored?
* The .libPaths function without arguments prints the library directories
* The .libPaths function with a directory as argument defines a new directory where to store new libraries.

## Install new packages

* Each major distribution of R includes a 'base' set of packages which support many basic statistical functions.
* Many R Users also choose to install additional 'Add-on' packages to provide simplified interfaces to R commands or to add specialist functionality i.e. the ggplot Grammar of Graphics package provides an advanced graphical output capability.
* The exhaustive list of all available packages is on the CRAN website.
* The R community has developed a vast resource of Add-on packages, some with unique functionality, some with overlapping functionality. It is therefore common to find multiple R packages capable of completing the same task i.e. reading and writing Excel spreadsheets. Ultimately which package to use is your choice.
* To install a new package, it is usually necessary to specify the name of the package as an argument of install.packages function.
* Sometimes you need to specify more options. For instance, this is the case if you are not an administrator of your computer.
** "lib" specifies the directory where you want to store the package.
** "repos" specifies a list of repositories. Note that you can specify a vector of repositories.
** "dep=T" specifies that all the required packages are also downloaded and installed.
* Stay up to date.
If you want to be aware of the latest packages, type new.packages in R or visit the Revolution Computing Blog which gives each month a list of the new and the updated packages.
We can also install bundles of packages using install.views or update.views (ctv).
We can also remove packages with remove.packages.

## Package Documentation and Help

All R packages install with 'help' documentation, listing their functions and providing syntax and usage examples.
See the Obtaining Help Documentation section for more details on accessing package 'help' documentation.

## Package Dependencies

* Most R packages have dependencies or references to other R packages. You must have all of an R package's 'required' dependencies installed, before you can use the package.
* R package dependencies come in two types, required and suggested.
* Specialist R packages such as the ggplot Grammar of Graphics packages have large package dependency trees.
* The install.packages function will automatically download and install a package and its dependencies, on a computer with an Internet connection.
* The R CMD INSTALL utility will check preinstalled packages for dependencies, but not download missing packages.
* Users must follow separate package download and installation processes when working on a computer with no Internet connection. The miniCRAN package can be used to assist in the offline management of R package dependencies.

## Building R Packages

You can write down your own R packages. But, all packages submitted to CRAN (or Bioconductor) must follow specific guidelines, including the folder structure of the package and the other files like DESCRIPTION, NAMESPACE and so on.
* See Friedrich Leisch's introduction (PDF 20 pages)
* See also Duncan Murdoch's tools for building packages using Windows
* See also Hadley Wickham and Jennifer Bryan's online book on current packaging practices (R Packages)

## References

* ↑ Friedrich Leisch Creating R Packages : A Tutorial http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf
* ↑ http://www.r-project.org/conferences/useR-2008/slides/Murdoch.pdf
* ↑ Hadley Wickham and Jennifer Bryan R Packages : Organize, Test, Docment and Share your code https://r-pkgs.org/

# Data types
## Data types

Vectors are the simplest R objects, an ordered list of primitive R objects of a given type (e.g. real numbers, strings, logicals). Vectors are indexed by integers starting at 1. Factors are similar to vectors but where each element is categorical, i.e. one of a fixed number of possibilities (or levels). A matrix is like a vector but with a specific instruction for the layout such that it looks like a matrix, i.e. the elements are indexed by two integers, each starting at 1. Arrays are similar to matrices but can have more than 2 dimensions. A list is similar to a vector, but the elements need not all be of the same type. The elements of a list can be indexed either by integers or by named strings, i.e. an R list can be used to implement what is known in other languages as an "associative array", "hash table", "map" or "dictionary". A dataframe is like a matrix but does not assume that all columns have the same type. A dataframe is a list of variables/vectors of the same length. Classes define how objects of a certain type look like. Classes are attached to object as an attribute. All R objects have a class, a type and a dimension.

## Vectors

You can create a vector using the c function which concatenates some elements. You can create a sequence using the : symbol or the seq function. For instance 1:5 gives all the number between 1 and 5. The seq function lets you specify the interval between the successive numbers. You can also repeat a pattern using the rep function. You can also create a numeric vector of missing values using numeric, a character vector of missing values using character and a logical vector of missing values (ie FALSE) using logical
The length computes the length of a vector. last (sfsmisc) returns the last element of a vector but this can also be achieved simply without the need for an extra package.

## Factors

factor transforms a vector into a factor. A factor can also be ordered with the option ordered=T or the function ordered. levels returns the levels of a factor. gl generates factors. n is the number of levels, k the number of repetition of each factor and length the total length of the factor. labels is optional and gives labels to each level.
Factors can be most easily thought of as categorical variables. An important function for factor analysis is the table function, which offers a type of summary. When considering the types of statistical data (nominal, ordinal, interval and ratio), factors can be nominal, ordinal or interval. Nominal factors are categorical names, examples of which could be country names paired with some other information. An example of an ordinal factor would be a set of race times for a particular athlete paired with the athlete's finishing place (first, second, ...). When trying to summarize this factor, please see the example with ordinal examples below for an example on self-ordering your factors. Finally, an example of interval level factors would be age brackets such as "20 - 29", "30 - 39", etc. In general, R can automatically order numbers stored as factors appropriately but a programmer may use the same techniques with this type of data to order in the manner most appropriate to their application.
See also is.factor, as.factor, is.ordered and as.ordered.

## Matrix

* If you want to create a new matrix, one way is to use the matrix function. You have to enter a vector of data, the number of rows and/or columns and finally you can specify if you want R to read your vector by row or by column (the default option). Here are two examples.
* Functions cbind and rbind combine vectors into matrices in a column by column or row by row mode:
* The dimension of a matrix can be obtained using the dim function. Alternatively nrow and ncol returns the number of rows and columns in a matrix:
* Function t transposes a matrix:
* Unlike data frames matrices must either be numeric or character in type:

## Arrays

An array is composed of n dimensions where each dimension is a vector of R objects of the same type. An array of one dimension of one element may be constructed as follows.
The array x was created with a single dimension (dim=c(1)) drawn from the vector of possible values c(T,F). A similar array, y, can be created with a single dimension and two values.
A three dimensional array - 3 by 3 by 3 - may be created as follows.
R arrays are accessed in a manner similar to arrays in other languages: by integer index, starting at 1 (not 0). The following code shows how the third dimension of the 3 by 3 by 3 array can be accessed. The third dimension is a 3 by 3 array.
Specifying two of the three dimensions returns an array on one dimension.
Specifying three of three dimension returns an element of the 3 by 3 by 3 array.
More complex partitioning of array may be had.
Arrays need not be symmetric across all dimensions. The following code creates a pair of 3 by 3 arrays.
Objects of the vectors composing the array must be of the same type, but they need not be numbers.

## Lists

A list is a collection of R objects. list creates a list. unlist transform a list into a vector. The objects in a list do not have to be of the same type or length.
lists have very flexible methods for reference
* by index number:
* By name:
* This can also be recursive and in combination
Using the scoping rules in R one can also dynamically name and create list elements

## Data Frames

A dataframe has been referred to as "a list of variables/vectors of the same length". In the following example, a dataframe of two vectors is created, each of five elements. The first vector, v1, is composed of a sequence of the integers 1 through 5. A second vector, v2, is composed of five logical values drawn of type T and F. The dataframe is then created, composed of the vectors. The columns of the data frame can be accessed using integer subscripts or the column name and the $ symbol.
The dataframe may be created directly. In the following code, the dataframe is created - naming each vector composing the dataframe as part of the argument list.

### External links

* data.frame objects in R (a sample chapter from the “R in Action” book)
* Aggregation and Restructuring of data.frame objects (a sample chapter from the “R in Action” book)

# Working with data frames

In this section, we deal with methods to read, manage and clean-up a data frame.
In R, a dataframe is a list of vectors of the same length. They don't have to be of the same type. For instance, you can combine in one dataframe a logical, a character and a numerical vector.

## Reading and saving data

If data are already in an R format (.Rda or .Rdata), you can load them in memory using load. You can save data to the R format using save.

## Example Datasets

* Most packages include example datasets to test the functions.
* The data function without argument gives the list of all example datasets in all the loaded packages.
* If you want to load them in memory, you just need to use the data function and include the name of the dataset as an argument.
* str_data (sfsmisc) gives the structure of all datasets in a package.
* Some packages include lots of datasets.
** The datasets package
** The AER package includes replication datasets for some important textbooks in econometrics.
** The EcDat package includes replication archive for the Journal of Applied Econometrics, the Journal of Business and Economic Statistics, etc.

## Building your own data frames

You can create a dataframe using vectors.
R has a spreadsheet-style data editor. One can use it to enter the data into a spreadsheet.
Read table from the clipboard :
You can also read space delimited tables in your code using gsource (Zelig). Here is an example with Yule 1899 data.
You can change the column names for a dataFrame.

## Describing a data frame

There are various ways to inspect a data frame, such as:
* str(df) gives a very brief description of the data
* names(df) gives the name of each variable
* summary(df) gives some very basic summary statistics for each variable
* head(df) shows the first few rows
* tail(df) shows the last few rows.

## Browsing data

* You can browse your data in a spreadsheet using View. Depending on your operating system, this option is not always available and the result is not always the same.
* You can print the first lines using head and the last lines using tail.
* RStudio has a nice data browser (View(mydata)).
* RKward has also a nice data browser
* Paul Murrell is currently developing the rdataviewer package (pdf).

## Binding row or column

Most of the times when you are working with data frames, you are changing the data and one of the several changes you can do to a data frame is adding column or row and as the result increase the dimension of your data frame. There are few different ways to do it but the easiest ones are cbind and rbind which are part of the base package:
Remember that the length of the newVector should match the length of the side of the data frame that you are attaching it to. For example, in the cbind command the following statement should be TRUE:
To see more samples, you can always do ?base::cbind and ?base::rbind.

## Attaching data

One of the big advantages of R over Stata is that you can deal with multiple datasets at the same time. You just need to specify the name of the dataset and a "$" symbol before each variable name ( for instance mydat1$var1 and mydat2$var1). If you only work with one dataset and you don't want to write again and again the name of the dataset as a prefix for each variable, you can use attach.

## Detecting duplicates

When you want to clean up a data set, it is very often useful to check if you don't have the same information twice in the data. R provides some functions to detect duplicates.
* duplicated looks at duplicated elements and returns a logical vector. You can use table to summarize this vector.
* Duplicated (sfsmisc) generalizes this command. Duplicated only marks unique values with "NA".
* remove.dup.rows (cwhmisc).
* unique keeps only the unique lines in a dataset.
* distinct (dplyr) retains only unique/distinct rows from a dataset.

## Creating and removing variables

To create a new variable
If you want to delete a variable in a dataset, you can assign NULL to that variable :

## Renaming variables

* It is possible to rename a variable by redefining the vector of names of a data frame.
* There is also a rename function in the reshape package.

## Creating a subset of the data

One can subset the data using subset. The first argument is the name of the dataset, the second argument is a logical condition which say which lines will be included in the new dataset and the last argument is the list of variable which will be included in the new dataset.
In the following example, we generate a fake dataset and we use the subset command to select the lines and columns of interest. We choose the lines such that x1 > 0 and x2 < 0 and we only keep x1 and x2 as variables.
It is also possible to reorder the columns using the select option.

## Sorting and ordering

* order
Suppose you want to randomize the order in a data set. You just need to generate a vector from a uniform distribution and to sort following that vector.

## Detecting missing values

* is.na returns a logical vector equal to TRUE if any of the variable in a dataset is missing and to FALSE otherwise.
* complete.cases returns a logical vector indicating TRUE if all cases are complete and FALSE otherwise.

## Reshaping a dataframe

This topic is important if you deal with panel data. Panel data can be stored in a wide format with one observation per unit and a variable for each time period or in a long format with one observation per unit and time period. reshape reshapes a dataset in a wide or long format.
* varying gives the numbers of the columns which are time-varying
* v.names gives the prefix of the time-varying variables
* direction gives the direction, either "long" or "wide".
* See also :
** reShape (Hmisc)
** See Hadley Wickham's reshape package
** See Duncan Murdoch's tables package

### External links

* Printing nested tables in R – bridging between the {reshape} and {tables} packages

## Expanding a dataset

Sometimes we need to duplicate some lines in a dataset. For instance, if we want to generate a fake dataset with a panel data structure. In that case, we would first generate time invariant variables and then duplicate each line by a given scalar in order to create time-varying variables.
It is possible to use the expand function in the epicalc package (since this package does not exist anymore, an option to expand is given in ). This will multiply each line by a given number.
We can also use the do it yourself solution or create our own function. The idea is simple. We create a vector which igives for each line the number of times it should be replicated (dups in the following example). Then we use the rep function to create a vector which repeats the line numbers according to what we want. The last step creates a new dataset which repeats lines according to the desired pattern.

## Merging dataframes

Merging data can be very confusing, especially if the case of multiple merge. Here is a simple example :
We have one table describing authors :
and one table describing books
We want to merge tables books and authors by author's name ("surname" in the first dataset and "name" in the second one). We use the merge command. We specify the name of the first and the second datasets, then by.x and by.y specify the identifier in both datasets. all.x and all.y specify if we want to keep all the observation of the first and the second dataset. In that case we want to have all the observations from the books dataset but we just keep the observations from the author dataset which match with an observation in the books dataset.
It is also possible to merge two data.frame objects while preserving the rows’ order by one of the two merged objects.

## Resources

* R Data Manual.
* Paul Murrell's Introduction to Data Technologies.

## References

* ↑ The AER Package http://cran.r-project.org/web/packages/AER/index.html
* ↑ The EcDat Package http://cran.r-project.org/web/packages/Ecdat/index.html
* ↑ "An investigation into the causes of changes in pauperism in England, chiefly during the last two intercensal decades (Part I.)" - GU Yule - Journal of the Royal Statistical Society, June 1899, p 283
* ↑ Reshaping Data with the reshape Package : http://www.jstatsoft.org/v21/i12
* ↑ vignette for the tables package: http://cran.r-project.org/web/packages/tables/vignettes/tables.pdf
* ↑ Merging data frames while preserving the rows
* ↑ R Data Manual http://cran.r-project.org/doc/manuals/R-data.html
* ↑ Paul Murrell introduction to Data Technologies http://www.stat.auckland.ac.nz/~paul/ItDT/

# Importing and exporting data

Data can be stored in a large variety of formats. Each statistical package has its own format for data (xls for Microsoft Excel, dta for Stata, sas7bdat for SAS, ...). R can read almost all file formats. We present a method for each kind of file. If none of the following methods work, you can use a specific software for data conversion such as the free software OpenRefine or the commercial software Stat Transfer. In any case, most statistical software can export data in a CSV (comma separated values) format and all of them can read CSV data. This is often the best solution to make data available to everyone.

## Graphical user interfaces

Some IDE or GUI provides some press button solution to import data.
You may also have a look at speedR, a graphical user interface which helps at importing data from Excel, OpenOfficeCalc, CSV and other text files.

## CSV (csv,txt,dat)

You can import data from a text file (often CSV) using read.table, read.csv or read.csv2. The option header = TRUE indicates that the first line of the CSV file should be interpreted as variables names and the option sep = gives the separator (generally "," or ";").
csv.get (Hmisc) is another possibility.
Note that there is no problem if your data are stored on the internet.
By default, strings are converted to factors. If you want to avoid this conversion, you can specify the option stringsAsFactors = FALSE.
You can export data to a text file using write.table.
For large CSV files, it is possible to use the ff package.

## Fixed width text files

read.fwf and write.fwf.
Some fixed width text files are provided with a SAS script to import them. Anthony Damico has created SAScii package to easily import those data.

## Unstructured text files

* See scan and readLines in the Reading and writing text files section.

## Stata (dta)

* We can read Stata data using read.dta in the foreign package and export to Stata data format using write.dta.
* Note that string variables in Stata are limited to 244 characters. This can be an issue during the exportation process.
* See also Stata.file in the memisc package and stata.get in the Hmisc package.

## SAS (sas7bdat)

Experimental support for SAS databases having the sas7bdat extension is provided by the sas7bdat package. However, sas7bdat files generated by 64 bit versions of SAS, and SAS running on non-Microsoft Windows platforms are not yet supported.

### SAS (xpt)

* See also sasexport.get and sas.get in the Hmisc
* See also the SASxport package.

## SPSS (sav)

* read.spss (foreign) and spss.get (Hmisc)

## EViews

readEViews in the hexView package for EViews files.

## Excel (xls,xlsx)

Importing data from Excel is not easy. The solution depends on your operating system. If none of the methods below works, you can always export each Excel spreadsheets to CSV format and read the CSV in R. This is often the simplest and quickest solution.
XLConnect supports reading and writing both xls and xlsx file formats. Since it is based on Apache POI it only requires a Java installation and as such works on many platforms including Windows, UNIX/Linux and Mac. Besides reading & writing data it provides a number of additional features such as adding plots, cell styling & style actions and many more.
The RODBC solution:
The xlsReadWrite package (actually, this package does not exist on CRAN repos, but you can download old versions from CRAN archive).
* "sheet" specifies the name or the number of the sheet you want to import.
* "from" specifies the first row of the spreadsheet.
The gnumeric package. This package use an external software called ssconvert which is usually installed with gnumeric, the Gnome office spreadsheet. The read.gnumeric.sheet function reads xls and xlsx files.
See also xlsx for Excel 2007 documents and read.xls (gdata).

## Google Spread Sheets

You should make the spreadsheet public, publish it as a CSV file. Then you can read it in R using read.csv. See more on the Revolution's computing blog (link). See also RGoogleDocs (link).

## gnumeric spreadsheets

The gnumeric package. read.gnumeric.sheet reads one sheet and read.gnumeric.sheets reads all sheets and store them in a list.

## OpenOffice and LibreOffice (ods)

readODS does not require external dependencies, making it crossplatform.
speedR is another alternative.
Note that you can also use the speedR graphical user interface (speedR) which will return the command line for replication.

## JSON

JSON (JavaScript Object Notation) is a very common format on the internet. The rjson library makes it easy to import data from a json format.
Is is easy to export a list or a dataframe to a JSON format using the toJSON function :
Sometimes the JSON data can be more complex with structures such as nested arrays. In this case you may find it more useful to use an online converter like json-csv.com to convert the file to CSV. Then import the resulting data as per the CSV instructions above.

## dBase (dbf)

read.dbf in the foreign package.

## Hierarchical Data Format (hdf5)

hdf5 data can be read using the hdf5 package.

## DICOM and NIfTI

* See "Working with the {DICOM} and {NIfTI} Data Standards in R" in the Journal of Statistical Software

## Resources

* R Data Manual.
* Paul Murrell's Introduction to Data Technologies.

## References

* ↑ Stat Transfer
* ↑ speedR
* ↑ "Opening Large CSV Files in R". Retrieved March 7, 2013. {{cite web}}: Unknown parameter |site= ignored (help)
* ↑ David Smith. "Importing public data with SAS instructions into R". Revolution Analytics. Retrieved February 1, 2013.
* ↑ sas7bdat
* 1 2 This command has been tested using Ubuntu 10.10 and R 2.11.1
* ↑ http://cran.r-project.org/web/packages/rjson/index.html
* ↑ http://cran.r-project.org/web/packages/hdf5/index.html
* ↑ Brandon Whitcher, Volker J. Schmid, Andrew Thorton "Working with the {DICOM} and {NIfTI} Data Standards in R", Journal of Statistical Software Vol. 44, Issue 6, Oct 2011, link
* ↑ R Data Manual
* ↑ Paul Murrell introduction to Data Technologies

# Text Processing

This page includes all the material you need to deal with strings in R. The section on regular expressions may be useful to understand the rest of the page, even if it is not necessary if you only need to perform some simple tasks.
This page may be useful to :
* perform statistical text analysis.
* collect data from an unformatted text file.
* deal with character variables.
In this page, we learn how to read a text file and how to use R functions for characters. There are two kind of function for characters, simple functions and regular expressions. Many functions are part of the standard R base package.
However, their name and their syntax is not intuitive to all users. Hadley Wickham has developed the stringr package which defines functions with similar behaviour but their names are easier to retain and their syntax much more systematic.
* Keywords : text mining, natural language processing
* See CRAN Task view on Natural Language Processing
* See also the following packages tm, tau, languageR, scrapeR.

## Reading and writing text files

R can read any text file using readLines or scan. It is possible to specify the encoding of the imported text file with readLines. The entire contents of the text file can be read into an R object (e.g., a character vector). scan is more flexible. The kind of data expected can be specified in the second argument (e.g., character(0) for a string).
We can write the content of an R object into a text file using cat or writeLines. By default cat concatenates vectors when writing to the text file. You can change it by adding options sep="\n" or fill=TRUE. The default encoding depends on your computer.
Before reading a text file, you can look at its properties. nlines (parser package) and countLines (R.utils package) count the number of lines in the file. count.chars (parser package) counts the number of bytes and characters in each line of a file. You can also display a text file using file.show.

## Character encoding

R provides functions to deal with various set of encoding schemes. This is useful if you deal with text file which have been created with another operating system and especially if the language is not English and has many accents and specific characters. For instance, the standard encoding scheme in Linux is "UTF-8" whereas the standard encoding scheme in Windows is "Latin1". The Encoding functions returns the encoding of a string. iconv is similar to the unix command iconv and converts the encoding.
* iconvlist gives the list of available encoding scheme on your computer.
* readLines, scan and file.show have also an encoding option.
* is.utf8 (tau) tests if the encoding is "utf8".
* is.locale (tau) tests if encoding is the same as the default encoding on your computer.
* translate (tau) translates the encoding into the current locale.
* fromUTF8 (descr) is less general than iconv.
* utf8ToInt (base)

### Example

The following example was run under Windows. Thus, the default encoding is "latin1".

## Regular Expressions

A regular expression is a specific pattern in a set of strings. For instance, one could have the following pattern : 2 digits, 2 letters and 4 digits. R provides powerful functions to deal with regular expressions. Two types of regular expressions are used in R
* extended regular expressions, used by ‘perl = FALSE’ (the default),
* Perl-like regular expressions used by ‘perl = TRUE’.
There is a also an option called ‘fixed = TRUE’ which can be considered as a literal regular expression. fixed (stringr) is equivalent to fixed=TRUE in the standard regex functions. These functions are by default case sensitive. This can be changed by specifying the option ignore.case = TRUE.
If you are not a specialist in regular expression you may find the glob2rx useful. This function suggests some regular expression for a specific ("glob" or "wildcard") pattern :

### Functions which use regular expressions in R

* sub, gsub, str_replace (stringr) make some substitutions in a string.
* grep, str_extract (stringr) extract some value
* grepl, str_detect (stringr) detect the presence of a pattern.
* see also splitByPattern (R.utils)
* See also gsubfn in the gsubfn package.

### Extended regular expressions (The default)

* "." stands for any character.
* "[ABC]" means A,B or C.
* "[A-Z]" means any upper letter between A and Z.
* "[0-9]" means any digit between 0 and 9.
Here is the list of metacharacters ‘$ * + . ? [ ] ^ { } | ( ) \’. If you need to use one of those characters, precede them with a doubled backslash.
Here are some classes of regular expressions : For numbers :
* ‘[:digit:]’ Digits: ‘0 1 2 3 4 5 6 7 8 9’.
For letters :
* ‘[:alpha:]’ Alphabetic characters: ‘[:lower:]’ and ‘[:upper:]’.
* ‘[:upper:]’ Upper-case letters.
* ‘[:lower:]’ Lower-case letters.
Note that the set of alphabetic characters includes accents such as é è ê which are very common in some languages like French. Therefore, it is more general than "[A-Za-z]" which does not include letters with accent.
For other characters :
* ‘[:punct:]’ Punctuation characters: ‘! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~’.
* ‘[:space:]’ Space characters: tab, newline, vertical tab, form feed, carriage return, and space.
* ‘[:blank:]’ Blank characters: space and tab.
* ‘[:cntrl:]’ Control characters.
For combination of other classes :
* [:alnum:] Alphanumeric characters: ‘[:alpha:]’ and ‘[:digit:]’.
* ‘[:graph:]’ Graphical characters: ‘[:alnum:]’ and ‘[:punct:]’.
* ‘[:print:]’ Printable characters: ‘[:alnum:]’, ‘[:punct:]’ and space.
* ‘[:xdigit:]’ Hexadecimal digits: ‘0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f’.
You can quantify the number of repetition by adding after the regular expression the following characters :
* ‘?’ The preceding item is optional and will be matched at most once.
* ‘*’ The preceding item will be matched zero or more times.
* ‘+’ The preceding item will be matched one or more times.
* ‘{n}’ The preceding item is matched exactly ‘n’ times.
* ‘{n,}’ The preceding item is matched ‘n’ or more times.
* ‘{n,m}’ The preceding item is matched at least ‘n’ times, but not more than ‘m’ times.
* ^ to force the regular expression to be at the beginning of the string
* $ to force the regular expression to be at the end of the string
If you want to know more, have a look at the 2 following help files :

### Perl-like regular expressions

It is also possible to use "perl-like" regular expressions. You just need to use the option perl=TRUE.

#### Examples

If you want to remove space characters in a string, you can use the \\s Perl macro.

#### See also

* Perl Programming/Regular Expressions

## Concatenating strings

* paste concatenates strings.
* str_c (stringr) does a similar job.
* cat prints and concatenates strings.

### Examples
## Splitting a string

* strsplit : Split the elements of a character vector ‘x’ into substrings according to the matches to substring ‘split’ within them.
* See also str_split (stringr).
* tokenize (tau) split a string into tokens.

## Counting the number of characters in a string

* nchar gives the length of a string. Note that that for non-ASCII encodings, there is more one way to measure such a length.
* See also str_length (stringr)

## Detecting the presence of a substring
### Detecting a pattern in a string ?

* grepl returns a logical expression (TRUE or FALSE).
* str_detect (stringr) does a similar job.
The 1st one is true and the second one is false since there is only one digit in the first number.

### Counting the occurrence of each pattern in a string ?

* textcnt (tau) counts the occurrence of each pattern or each term in a text.

## Extracting the position of a substring or a pattern in a string
### Extracting the position of a substring ?

* cpos (cwhmisc) returns the position of a substring in a string.
* substring.location (cwhmisc) does the same job but returns the first and the last position.

### Extracting the position of a pattern in a string ?

* regexpr returns the position of the regular expression. str_locate (stringr) does the same job. gregexpr is similar to regexpr but the starting position of every match is returned. str_locate_all (stringr) does the same job.

## Extracting a substring from a string
### Extracting a fixed width substring ?

* substr takes a sub string.
* str_sub (stringr) is similar.

### Extracting the first word in a string ?

* first.word First Word in a String or Expression in the Hmisc package

### Extracting a pattern in a string ?

* grep returns the value of the regular expression if value=T and its position if value=F.
* str_extract, str_extract_all, str_match, str_match_all (stringr) and m (caroline package) are similar to grep. str_extract and str_extract_all return a vector. str_match and str_match_all return a matrix and m a dataframe.
* Named capture regular expressions can be used to define column names in the regular expression (this also serves to document the regular expression). Install the namedCapture package via devtools::install_github("tdhock/namedCapture") to use str_match_all_named. It uses the base function gregexpr(perl=TRUE) to parse a Perl-Compatible Regular Expression, and returns a list of match matrices with column names:

## Making some substitution inside a string
### Substituting a pattern in a string

* sub makes a substitution.
* gsub is similar to sub but replace all occurrences of the pattern whereas sub only replaces the first occurrence.
* str_replace (stringr) is similar to sub, str_replace_all (stringr) is similar to gsub.
In the following example, we have a French date. The regular pattern is the following : 2 digits, a blank, some letters, a blank, 4 digits. We capture the 2 digits with the [[:digit:]]{2} expression, the letters with [[:alpha:]]+ and the 4 digits with [[:digit:]]{4}. Each of these three substrings is surrounded with parenthesis. The first substring is stored in "\\1", the second one in "\\2" and the 3rd one in "\\3".
In the following example, we compare the outcome of sub and gsub. The first one removes the first space whereas the second one removes all spaces in the text.

### Substituting characters in a string ?

* chartr substitutes characters in an expression. It stands for "character translation".
* replacechar (cwhmisc) does the same job ...
* as well as str_replace_all (stringr).

## Converting letters to lower or upper-case

* tolower converts upper-case characters to lower-case.
* toupper converts lower-case characters to upper-case.
* capitalize (Hmisc) capitalize the first letter of a string
* See also cap, capitalize, lower, lowerize and CapLeading in the cwhmisc package.

## Filling a string with some character

* padding (cwhmisc) fills a string with some characters to fit a given length. See also str_pad (stringr).
Note that str_pad is very slow. For instance for a vector of length 10,000, we have a very long computing time. paddingdoes not seem to handle character vectors but the best solution may be to use the sapply and padding functions together.

## Removing leading and trailing spaces

* trimws (memisc package) trim leading and trailing white spaces.
* trim (gdata package) does the same job.
* See also str_trim (stringr)

## Comparing two strings
### Assessing if they are identical

* == returns TRUE if both strings are the same and false otherwise.

### Computing distance between strings

Few packages implement the Levenshtein distance between two strings:
* adist in base package utils
* stringMatch in MiscPsycho
* stringdist in stringdist
* levenshteinDist in RecordLinkage
A benchmark comparing the speed of levenshteinDist and stringdist is available here: .

#### Example with MiscPsycho

stringMatch (MiscPsycho) computes If normalize="YES" the levenshtein distance is divided by the maximum length of each string.

#### Approximate matching

agrep search for approximate matches using the Levenshtein distance.
* If 'value = TRUE', this returns the value of the string
* If 'value = FALSE' this returns the position of the string
* max returns the maximal levenshtein distance.

## Miscellaneous

* deparse : Turn unevaluated expressions into character strings.
* char.expand (base) expands a string with respect to a target.
* pmatch (base) and charmatch (base) seek matches for the elements of their first argument among those of their second.
* make.unique makes a character string unique. This is useful if you want to use a string as an identifier in your data.

## References

* ↑ Hadley Wickham "stringr: modern, consistent string processing" The R Journal, December 2010, Vol 2/2, http://journal.r-project.org/archive/2010-2/RJournal_2010-2_Wickham.pdf
* ↑ http://cran.r-project.org/web/views/NaturalLanguageProcessing.html
* ↑ In former versions (< 2.10) we had also basic regular expressions in R : extended regular expressions, used by extended = TRUE (the default), basic regular expressions, as used by extended = FALSE (obsolete in R 2.10). Since basic regular expressions (‘extended = FALSE’) are now obsolete, the extended option is obsolete in version 2.11.

# Times and Dates

R contains a set of object types for holding date and time information. The system time and date can also be requested.

## Format

Many time and date units are recognised. These include:
| Unit | Symbol | Example |
| 4 digit year | %Y | 1932 |
| 2 digit year | %y | 84 |
| Numerical Month | %m | 03 |
| Full Month | %B | January |
| Abbreviated Month | %b | Jan |
| Day of the month | %d | 31 |
| Full weekday | %A | Wednesday |
| Abbreviated weekday | %a | Wed |
| Hours (24hr clock) | %H | 16 |
| Minutes | %M | 35 |
| Seconds | %S | 52 |
The default format is yyyy-mm-dd hh:mm:ss or %Y-%m-%d %H:%M:%S
For example 2010-02-13 23:12:24

## System Date and Time

To get the system date and time:

## Convert strings to date/time objects

Convert a string representing the date or time into a Date/Time object:

## Extracting information from dates

Get weekday, month and an integer representing the number of days since the beginning of epoch:
Note that weekdays and months returns results in the local language. For instance, if you turn R into French, you can get weekdays and months in French :

## References

* ↑ Issue on Stackoverflow

## External links

* Do more with dates and times in R with lubridate 1.1.0 (a sample chapter from the “R in Action” book)

# Graphics

R includes at least three graphical systems, the standard graphics package, the lattice package for Trellis graphs and the grammar-of-graphics ggplot2 package. R has good graphical capabilities but there are some alternatives like gnuplot.

## Interactive Graphics

This section discuss some ways to draw graphics without using R scripts.
The playwith package provides a graphical user interface to customize the graphs, add a title, a grid, some text, etc and it exports the R code you need if you want to replicate the analysis. If you want to know more, you can have a look at the screenshots on the website (link). See also the example on "R you Ready" . This package require GTK+ libraries.
There is also a graphical user interface GrapheR which makes it very easy to draw graphs for beginners. This solution is cross-platform.
latticist (link) is another similar project.
Note also that some graphical user interface such as RKward and R Commander makes it easy to draw graphs.

## Standard R graphs

In this section we present what you need to know if you want to customize your graphs in the default graph system.
* plot is the main function for graphics. The arguments can be a single point such as 0 or c(.3,.7), a single vector, a pair of vectors or many other R objects.
* par is another important function which defines the default settings for plots.
* There are many other plot functions which are specific to some tasks such as hist, boxplot, etc. Most of them take the same arguments as the plot function.

### Titles, legends and annotations
#### Titles

main gives the main title, sub the subtitle. They can be passed as argument of the plot function or using the title function. xlab the name of the x axis and ylab the name of the y axis.
The size of the text can be modified using the parameters cex.main, cex.lab, cex.sub, cex.axis. Those parameters define a scaling factor, ie the value of the parameter multiply the size of the text. If you choose cex.main=2 the main title will be twice as big as usual.

#### Legend

legend. The position can be "bottomleft", "bottomright", "topleft", "topright" or exact coordinates.

#### Text in the margin

mtext puts some texts in the margin. The margin can be at the bottom (1), the left (2), the top (3) or the right (4).

#### Text in the graph

text

#### Mathematical annotations

We can add mathematical symbols using expression and makes some substitution in a formula using substitute.

### Types

The type of a plot can be :
* n for none (nothing is printed),
* p for points,
* l for lines,
* b for both,
* o for both overlayed,
* h for histogram-like
* and s/S for steps.
| R code | Output |
| x1 <- rnorm(50) png("plottype.png") par(mfrow = c(2,2)) plot(x1, type = "p", main = "points", ylab = "", xlab = "") plot(x1, type = "l", main = "lines", ylab = "", xlab = "") plot(x1, type = "b", main = "both", ylab = "", xlab = "") plot(x1, type = "o", main = "both overplot", ylab = "", xlab = "") dev.off | click on the graph to zoom |

### Axes

The default output print the axes. We can remove them with axes=FALSE. We can also change them using the axis function.
las specifies the style of axis labels. It can be 0, 1, 2 or 3.
* 0 : always parallel to the axis [default],
* 1 : always horizontal,
* 2 : always perpendicular to the axis,
* 3 : always vertical.
| R code | Output |
| x1 <- rnorm(100) par(mfrow = c(2,2)) plot(x1, las = 0, main = "las = 0", sub = "always parallel to the axis", xlab = "", ylab = "") plot(x1, las = 1, main = "las = 1", sub = "always horizontal", xlab = "", ylab = "") plot(x1, las = 2, main = "las = 2", sub = "always perpendicular to the axis", xlab = "", ylab = "") plot(x1, las = 3, main = "las = 3", sub = "always vertical", xlab = "", ylab = "") | click on the graph |
It is also possible to add another y axis on the right by adding axis(4,).

### Margins

Margins can be computed in inches or in lines. The default is par(mar = c(5,4,4,2)) which means that there are 5 lines at the bottom, 4 lines on the left, 4 lines in the top and 2 lines on the right. This can be modified using the par function. If you want to specify margins in inches, use par(mai = c(bottom, left, top, right). If you want to modify margins in lines, use par(mar = c(bottom, left, top, right). See ?par to learn more about the topic.

### Colors

The color of the points or lines can be changed using the col argument, fg for foreground colors (boxes and axes) and bg for background colors.
* show.col(object=NULL) (Hmisc) package plots the main R colors with their numeric code.
* The list of all colors in R (pdf)
* We can also generate new colors using the rgb function. The first argument is the intensity of red, the second, the intensity of green and the third, the intensity of blue. They vary between 0 and 1 by default but this can be modified with the option max = 255. col2rgb returns the RGB code of R colors. col2hex (gplots) gives the hexadecimal code. col2grey and col2gray (TeachingDemos) converts colors to grey scale.

### Points

For points the symbols can be changed using the pch option which takes integer values between 0 and 25 or a single character. pch can also takes a vector as argument. In that case the first points will use the first element of the vector as symbol, and so on.
The following code displays all the symbols on the same plot :
points adds points to an existing plot.

### Lines

We can change the line type with lty. The argument is a string ("blank", "solid", "dashed", "dotted", "dotdash", "longdash", or "twodash") or an integer (0=blank, 1=solid (default), 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash). The line width can be changed with lwd. The default is lwd=1. lwd=2 means that the width is twice the normal width.
lines adds an additional lines on a graph.
abline adds an horizontal line (h=), a vertical line (v=) or a linear function to the current plot (a= for the constant and b= for the slope). abline can also plot the regression line.

### Boxes

Each graph is framed by a box. bty specifies the box type.
See also box to add a box to an existing plot.

### Grid

grid adds a grid to the current graph.
Although grid has an optional argument nx for setting the number of grid lines, it is not possible to tell it explicitly where to place those lines (it will usually not place them at integer values). A more precise and manageable alternative is to use abline.

### Other figures

We can also add a circle to a plot with the circle function in the calibrate package.

### Background

You can choose the background of your plot. For instance, you can change the background color with par(bg=).

### Overlaying plots

matplot can plot several plots at the same time.

### Multiple plots

With par we can display multiple figures on the same plot. mfrow = c(3,2) prints 6 figures on the same plot with 3 rows and 2 columns. mfcol = c(3,2) does the same but the order is not the same.

### Plotting a function

* curve plots a function. This can be added to an existing plot with the option add = TRUE.
* plot can also plots functions.
* square root function, made using plot

## Exporting graphs

How can you export a graph ?
* First you can plot the graph and use the context menu (right click on Windows and Linux or control + click on Mac) to copy or save the graphs. The available options depend on your operating system. On Windows, you can also use copy the current graph to the clipboard as a Bitmap file (raster graphics) using CTRL + C or as a Windows Metafile (vector graphics) using CTRL + W. You can then paste it into another application.
* You can export a plot to pdf, png, jpeg, bmp or tiff by adding pdf("filename.pdf"), png("filename.png"), jpeg("filename.jpg"), bmp("filename.bmp") or tiff("filename.tiff") prior to the plotting, and dev.off after the plotting.
* You can also use the savePlot function to save existing graphs.
* Sweave also produce ps and pdf graphics (See the Sweave section).
It is better to use vectorial devices such as pdf, ps or svg.
How can you know the list of all available devices ?
* ?Devices
* Use the capabilities function to see the list of available devices on your computer.
We can also export to SVG using the svg function.
The RSvgDevice library which was used in earlier versions of R seems now outdated.

## Advanced topics
### Animated plots

The animation package provides dynamic graphics capabilities. It is possible to export the animation in flash, mpeg or gif format. There are more example on the aniwiki website : http://animation.yihui.name/.
You can also create motion charts using the googleVis package.

#### Examples
### Interactive Graphics

The iplots package provides a way to have interactive data visualization in R ·.
* R GUI now offers interactive graphics – Deducer 0.4-2 connects with iplots
To create an interactive, animated plot viewable in a web browser, the animint package can be used. The main idea is to define an interactive animation as a list of ggplots with two new aesthetics:
* showSelected=variable means that only the subset of the data that corresponds to the selected value of variable will be shown.
* clickSelects=variable means that clicking a plot element will change the currently selected value of variable.

## Graphics gallery

In this section, we review all kind of statistical plots and review all alternatives to draw them using R. This include code for the standard graphics package, the lattice package and the ggplot2 package. Also, we add some examples from the commons repository. We only add examples which are provided with the R code. You can click on any graph and find the R code.

### Line plot

To draw a line plot, use the generic plot function by setting type="l".
Then, you can add further lines on the same plot using the lines function.

#### Examples
### Scatter plot

* plot(x,y)
* plot(y ~ x)
* xyplot(y ~ x) (lattice)
* qplot(x,y) (ggplot2)

#### Log scale

Sometimes it is useful to plot the log of a variable and to have a log scale on the axis. It is possible to plot the log of a variable using the log option in the plot function.
* For a log log plot, use log = "xy"
* For a log in the x axis only, use log = "x"
* For a log in the x axis only, use log = "y"

#### Label points in a plot

* It is possible to add labels with the text function.
* textxy (calibrate) makes it easy to add labels.

#### Examples
### Histogram

* hist
* histogram (lattice)
You can learn more about histograms in the Non parametric methods page.

#### Examples
### Box plot

Box plot :
* boxplot

#### See also

* How to label all the outliers in a boxplot

### Bar charts

See Bar charts on wikipedia.
* barplot takes a table as argument and returns a bar chart.
* qlot (ggplot2) with the option geom = "bar" takes a variable as argument and returns a bar chart.
* barchart takes a variable as argument and returns a bar chart.

#### Examples
### Dot plot

See also Dot plot on Wikipedia.
* dotchart

#### Examples
### Pie charts

* pie

#### Examples
### Treemap

The tmPlot function in the treemap package makes it easy to draw a treemap.

### Confidence interval plot

Standard error bar chart are very useful to plot several estimates with confidence intervals.
* The Hmisc package has an errbar function. This function takes the upper and lower bounds of the confidence intervals as argument.
* coefplot function in Gelman and Hill's arm package. This functions is designed to display estimation results. It takes point estimates and standard errors as arguments.
See also
* There is another errbar function in the sfsmisc package.
* plotCI (gplots) also plot error bars.
* plotmeans (gplots)
* ciplot (hacks)
* See also Error bar on Wikipedia

### 3D plots

* contour, image, persp
* plot3d (rgl)
* wireframe (lattice)

#### Examples

* click on the graph to see the R code
* Example with wireframe (lattice)

### Diagrams

* grid package by Paul Murrell
* diagram package
* Rgraphviz package
* igraph package

### Arc Diagrams

It is also possible to draw Arc Diagrams.

### Dendrograms

It is possible to plot dendrograms in R.

### Treemap

It is possible to draw a treemap using the treemap function in the treemap package.

### Wordcloud

There is :
* the wordcloud function in the wordcloud package
* the tagcloud function in the tagcloud package

### Timeline

* timeline in the timeline package

### Maps
#### See also

* Geograpic Maps in R (Revolution Computing Blog)
* Global Administrative Areas
* Andy Egger's googlemap.r function

## Resources

* Tables 2 Graphs
* R Graphics by Paul Murrell
* ggplot2
* Graphical Parameters

## References

* ↑ D. Sarkar. Lattice: Multivariate Data Visualization with R. Springer, 2008. ISBN 9780387759685.
* ↑ ggplot2: Elegant Graphics for Data Analysis (Use R) by Hadley Wickham and a list of examples on his own website : http://had.co.nz/ggplot2/
* ↑ playwith : http://code.google.com/p/playwith/
* ↑ Hervé, Maxime (2011). "GrapheR: a Multiplatform GUI for Drawing Customizable Graphs in R" (PDF). The R Journal. 3 (2).
* ↑ Tutorial for the googleVis package : http://stackoverflow.com/questions/4646779/embedding-googlevis-charts-into-a-web-site/4649753#4649753
* ↑ http://www.r-bloggers.com/interactive-graphics-with-the-iplots-package-from-%E2%80%9Cr-in-action%E2%80%9D/
* ↑ http://www.r-statistics.com/2012/01/interactive-graphics-with-the-iplots-package-from-r-in-action/ Interactive Graphics with the iplots Package] - a chapter from the R in action book
* ↑ Hadley Wickham ggplot2: Elegant Graphics for Data Analysis, Springer Verlag, 2009
* ↑ The default output in errbar changed between R version 2.8.1 and R version 2.9.2. Axis are not displayed by default anymore
* ↑ Paul Murrell Drawing Diagrams with R, The R Journal, 2009 http://journal.r-project.org/2009-1/RJournal_2009-1_Murrell.pdf
* ↑ (example: Using a binary tree diagram for describing a Bernoulli process)
* ↑ Gaston Sanchez (Feburary 3rd, 2013). "Arc Diagrams in R: Les Miserables". Retrieved February 5th, 2013. {{cite web}}: Check date values in: |accessdate= and |date= (help)
* ↑ Gaston Sanchez (October 3, 2012). "7+ ways to plot dendrograms in R". Retrieved February 5th, 2013. {{cite web}}: Check date values in: |accessdate= and |date= (help); line feed character in |date= at position 9 (help)
* ↑ http://cran.r-project.org/web/packages/treemap/treemap.pdf
* ↑ http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html
* ↑ http://had.co.nz/ggplot2/

# Grammar of graphics

Hadley Wickham has developped the ggplot2, a graphical library designed according to the principles of the Grammar of Graphics.

## Plotting a function

We use qplot with the option stat=function :
Here is another example with the sinus function :

## Bibliography

* Leland Wilkinson, The Grammar of Graphics (Statistics and Computing), Springer, 2005
* Hadley Wickham, ggplot2: Elegant Graphics for Data Analysis, Use R!, Springer, 2009

## Resources

* Documentation page for ggplot2

# Publication quality ouput
## Formatting numbers

You can use the format function to control the number of digits and other characteristics of a displayed object.

## Sweave

Sweave is a literate programming language which integrates LaTeX and R code. The Sweave file generates a LaTeX file and an R file which can in turn be compiled. Roger Koenker, Meredith and Racine (2009) and Charles Geyer argue that Sweave favors reproducible econometric/statistical research.
There are some alternatives to Sweave for literate programming. One of them is Babel which is included in Emacs Orgmode. This tool allow export to LaTeX and HTML. It is also possible to include code chunks for various programming languages (R, Ruby, etc).

### Syntax

The main idea is that you write a file which includes LaTeX and R code. LaTeX code begins with @ and R code with <<>>= (some options can be included between << and >>).
The file is stored with extension .Rnw or .rnw. At the end, you extract from this file an R file using Stangle and a LaTeX file using Sweave. Here is an example with a file called file.Rnw which generates file.tex and file.R
Then you can run LaTeX on your file.tex. This can be done using the system function or texi2dvi.
Note that you may need to download Sweave.sty from the internet since it is not part of the standard MikTeX distribution.
You can also add your results in your text using the \Sexpr function.

### Options

There are some options. These options can be included for each code chunk or in the Sweave command.
* For figures, you can either include them in the tex file using fig=T or not include them using fig=F.
By default, figures are exported as pdf and eps files. If you only want one format suppress the other one with pdf=F or eps=F option.
* The R code can be displayed in the tex file using echo=T. If you don't want to include it in the tex file, use echo=F.
* The R code can be evaluated using eval=T. If you don't want to evaluate the R code, use eval=F.
* The results :
** results=tex treats the output as LaTeX code
** results=verbatim treats the output as Verbatim (the default)
** results=hide does not include the results in the LaTeX output
These options can be passed to the Sweave function.
They can also be passed to each code chunk.

### Text editor for Sweave

The main issue with Sweave is that few text editors include syntax highlighting for Sweave. Here are some exceptions :
* RStudio is a very good solution. It is easy to install and use and it includes buttons to run Sweave files.
* Vim provides syntax highlighting for Sweave file (R no web syntax)
* Emacs + ESS (Emacs Speaks Statistics) provides full support for Sweave file. It includes a keyboard shortcut to run Sweave files and syntax highlighting switching between LaTeX and R.
* Eclipse StatET plugin provides support for Sweave (LaTeX/R) documents with all basic features (syntax highlighting, bracket matching, toggle comment, ...) and with detection of R chunks.

### See also

Some example of Sweave documents :
* Charles Geyer foo.Rnw example
* Julien Barnier's introduction to R (document in french)
* trick : type filetype:Rnw or filetype:Snw in Google to get Sweave files
* Notice that you can find lots of examples by browsing in the R library folder. The documentation is often written using Sweave and the Sweave file is often included in the package. See for instance in the np package the doc folder.
Some handouts :
* "Literate Programming with Sweave and DOCSTRIP" (pdf) by Michael Lundholm
* Charles Geyer 2008 "An Sweave Demo" (pdf) (short)
* Learning To Sweave in APA Style
Some packages
* pgfSweave package
* ascii package
* cacheSweave
* exam automatic generation of exams
Some alternative literate programming packages :
* odfWeave package to Sweave with OpenOffice.
* knitr package
* decumar, a literate programming interface for R by Hadley Wickham
* relax package
* wikirobot is similar to Sweave but works with MediaWiki.

## Pubprint

Pubprint is a small utility that is able to transform the output of statistical tests to publication ready output. Pubprint is able to export outputs to severall formats (HTML, LaTeX, Markdown and plain text), but unfortunately supports only the APA style (publication style of the American Psychological Association). However, this style is widely used and may be appropriate in more cases.

### Example

Obviously pubprint prints a LaTeX formatted string, but changing the output format is possible (according to the manual pubprint is intended to use with knitr and detects output format automatically if it is used with it):
The output can be pasted into a documented or may included in a knitr/sweave \Sexpr statement.

## Export to LaTeX

R has lots of functions which allow it to export results to LaTeX.

### General functions

toLatex in the utils package.
* Note that toLatex does not handle matrices.
* toLatex has been adapted to handle matrices and ftables in the memisc package.
* mat2tex (sfsmisc) exports matrix to LaTeX.
* tex.table (cwhmisc) package exports a dataframe into a LaTeX table.
* xtable (xtable) exports various objects, including tables, data frames, lm, aov, and anova, to LaTeX.
See also :
* The highlight package by Romain François exports R code to LaTeX and HTML.
* format.df and latex in the Hmisc package.
* The MEMISC and the quantreg packages include other latex function.

### Descriptive statistics

* estout package.
* The reporttools package include some functions for table of descriptive statistics.

### Estimation results

* The stargazer package provides an easy way to export the results of regressions to LaTeX
* texreg provides the same kind of features.
* The estout package provides functions similar to the Stata's esttab and estout utilities. Estimates are stored using eststo and printed using esttab. They can be exported to CSV and LaTeX. These functions support lm, glm and plm objects (see plm package).
* apsrtable (apsrtable) exports the results of multiple regression to LaTeX in a way similar to the American Political Science Review publication standard.
* The xtable (xtable package) exports dataframes, matrix, estimation results. xtable can also be used to export the results to an HTML file.
* The outreg function developped by Paul Johnson is similar to the Stata outreg function. See "R you ready ?" post on this topic.
* mtable and toLatex in the 'memisc package.

## Export to HTML

The rpublisher is a literate programming language which publish results in HTML (it is based on python and was last updated in 2008).
See R2HTML, xtable, hwriter, prettyR, highlight, HTMLUtils
wiki.table in the hacks package export a matrix or a dataframe into Mediawiki table markup (as used on this wiki and many others).

## References

* ↑ The Sweave Homepage http://www.stat.uni-muenchen.de/~leisch/Sweave/
* ↑ http://www.econ.uiuc.edu/~roger/repro.html
* ↑ Meredith, E. and J.S. Racine (2009), “Towards Reproducible Econometric Research: The Sweave Framework,” Journal of Applied Econometrics, Volume 24, pp 366-374.
* ↑ Charles Geyer "Why Reproducible Research is the Right Thing" http://www.stat.umn.edu/~charlie/Sweave/
* ↑ Babel in Emacs Orgmode http://orgmode.org/worg/org-contrib/babel/intro.html
* ↑ Ista ZahnLearning To Sweave in APA Style, The PracTeX Journal 2008, 1
* ↑ decumar git archive : http://github.com/hadley/decumar
* ↑ wikirobot http://r-forge.r-project.org/projects/wikirobot/
* ↑ See the LaTeX Wikibook if you want to learn about LaTeX
* ↑ reporttools: R Functions to Generate LaTeX Tables of Descriptive Statistics
* ↑ http://www.r-statistics.com/2013/01/stargazer-package-for-beautiful-latex-tables-from-r-statistical-models-output/
* ↑ http://www.r-bloggers.com/texreg-a-package-for-beautiful-and-easily-customizable-latex-regression-tables-from-r/
* ↑ estout : http://repec.org/bocode/e/estout/
* ↑ xtable on dataninja blog
* ↑ The outreg function http://pj.freefaculty.org/R/WorkingExamples/outreg-worked.R
* ↑ Stata outreg http://ideas.repec.org/c/boc/bocode/s375201.html
* ↑ rpublisher : http://code.google.com/p/rpublisher/

# Descriptive Statistics

In this section, we present descriptive statistics, ie a set of tools to describe and explore data. This mainly includes univariate and bivariate statistical tools.

## Generic Functions

We introduce some functions to describe a dataset.
* names gives the names of each variable
* str gives the structure of the dataset
* summary gives the mean, median, min, max, 1st and 3rd quartile of each variable in the data.
* describe (Hmisc package) gives more details than summary
* contents (Hmisc package)
* dims in the Zelig package.
* descr in the descr package gives min, max, mean and quartiles for continuous variables, frequency tables for factors and length for character vectors.
* whatis (YaleToolkit) gives a good description of a dataset.
* detail in the SciencesPo package gives a broad range of statistics for continuous variables, frequency tables for factors and length for character vectors.
* describe in the psych package also provides summary statistics:

## Univariate analysis
### Continuous variable
#### Moments

* mean computes the mean
* the variance : var.
* the standard deviation sd.
* the skewness skewness (fUtilities, moment or e1071)
* the kurtosis : kurtosis (fUtilities, moment or e1071)
* all the moments : moment (moment) and all.moments (moment).

#### Order statistics

* the range, the minimum and the maximum : range returns the range of a vector (minimum and maximum of a vector), min the minimum and max the maximum.
* IQR computes the interquartile range. median computes the median and mad the median absolute deviation.
* quantile, hdquantile in the Hmisc package and kuantile in the quantreg packages computes the sample quantiles of a continuous vector. kuantile may be more efficient when the sample size is big.

#### Inequality Index

* The gini coefficient : Gini (ineq) and gini (reldist).
* ineq (ineq) gives all inequalities index.
* Concentration index
* Poverty index

#### Plotting the distribution

We can plot the distribution using a box plot (boxplot), an histogram (hist), a kernel estimator (plot with density) or the empirical cumulative distribution function (plot with ecdf). See the Nonparametric section to learn more about histograms and kernel density estimators. qqnorm produces a normal QQ plot and qqline adds a line to the QQ plot which passes through the first and the third quartile.
* A box-plot is a graphical representation of the minimum, the first quartile, the median, the third quartile and the maximum.
* stripchart and stem are also availables.

#### Goodness of fit tests

Kolmogorov Smirnov Test :
The KS test is one sample goodness of fit test. The test statistic is simply the maximum of the absolute value of the difference between the empirical cumulative distribution function and the theoritical cumulative distribution function. KSd (sfsmisc) gives the critical values for the KS statistic. As an example, we draw a sample from a Beta(2,2) distribution and we test if it fits a Beta(2,2) a Beta(1,1) and a uniform distribution.
Some tests are specific to the normal distribution. The Lillie Test is an extension of the KS test when the parameters are unknown. This is implemented with the lillie.test in the nortest package. shapiro.test implements the Shapiro Wilk Normality Test
* Andersen Darling Test :
See also the package ADGofTest for another version of this test.
* Shapiro-Francia normality test :
* Pearson chi-square normality test :
* Cramer-von Mises normality test
* Jarque-Bera test :

### Discrete variable

We generate a discrete variable using sample and we tabulate it using table. We can plot using a pie chart (pie), a bar chart (barplot or barchart (lattice)) or a dot chart (dotchart or dotplot (lattice)).
* freq (descr) prints the frequency, the percentages and produces a barplot. It supports weights.

## Multivariate analysis
### Continuous variables

* Covariance : cov
* Pearson's linear correlation : cor.
* Pearson's correlation test cor.test performs the test.
* Spearman's rank correlation :
** cor with method = "spearman".
** spearman (Hmisc)
* Spearman's rank correlation test :
** spearman2 (Hmisc)
** spearman.test (Hmisc)
** spearman.test (pspearman package) performs the Spearman’s rank correlation test with precomputed exact null distribution for n <= 22.
* Kendall's correlation : cor with method = "kendall". See also the Kendall package.

### Discrete variables

* table, xtabs and prop.table for contingency tables. ftable (stats package) for a flat (nested) table.
* assocplot and mosaicplot for graphical display of contingency table.
* CrossTable (descr) is similar to SAS Proc Freq. It returns a contingency table with Chi square and Fisher independence tests.
* my.table.NA and my.table.margin (cwhmisc)
* chisq.detail (TeachingDemos)

### Discrete and Continuous variables

* bystats Statistics by Categories in the Hmisc package
* summaryBy (doBy)
* Multiple box plots : plot or boxplot
* Equality of two sample mean t.test and wilcox.test, Equality of variance var.test, equality of two distributions ks.test.

## References

* ↑ Carlos J. Gil Bellosta (2009). ADGofTest: Anderson-Darling GoF test. R package version 0.1. http://CRAN.R-project.org/package=ADGofTest

# Mathematics
## Linear Algebra
### Vectors
#### The inner product

The inner product is also called the dot product or the scalar product. It is the sum of the item-by-item product.

#### The outer product

The outer product is also called the cross product or the vector product. It is a matrix resulting from the product of the elements of the two vectors.

### Matrix Algebra

If you want to create a new matrix, one way is to use the matrix function. You have to enter a vector of data, the number of rows and/or columns and finally you can specify if you want R to read your vector by row or by column (the default option) with byrow. You can also combine vectors using cbind or rbind. The dimension of a matrix can be obtained using the dim function or alternatively nrow and ncol.

### Some special matrix

The identity matrix has ones on the diagonal and zeros outside the diagonal.
* eye (matlab)
* diag(1,nrow=10,ncol=10)
* diag(rep(1,10))
J matrix is full of ones
* ones (matlab)
A matrix full of zeros
* zeros (matlab)
Diagonal matrix
Upper triangular
Lower triangular
Same as upper triangular but using lower.tri instead
* create an Hilbert matrix using hilbert (fUtilities).

### Matrix calculations

* compute a matrix multiplication X%*%Y.
* compute the Kronecker product using %x% or kron (fUtilities).

### Matrix transposition

* Transpose the matrix

### The trace and determinant of a matrix

* compute the trace of a matrix using tr (fUtilities)
* returns the rank of a matrix using rk (fBasics:)

### Matrix inversion

* Invert a matrix using solve or inv (fUtilities). We can also compute the generalized inverse using ginv in the MASS package.

### Eigenvalue, eigenvector and eigenspace

* Eigenvalues and eigenvectors

### Misc

* compute the norm of a matrix using norm (fUtilities).
* check if a matrix is positive definite isPositiveDefinite (fUtilities).
* make a matrix positive definite makePositiveDefinite (fUtilities).
* computes row statistics and column statistics (fUtilities).
* extract the upper and the lower part of a matrix triang and Triang (fUtilities).
* See also the matrix, matlab, matrixcalc, matrixStats packages.

## Analysis
### Logarithm and Exponents

We have the power function 10^3 or 10**3 , the logarithm and the exponential log(2.71), log10(10),exp(1).

### Polynomial equations

To solve $ax^{k}+bx^{k-1}+\cdots +n=0$, where $a,b,\dots ,n$ are given numbers, use the command
So, for example, to calculate the roots of the equation $2x^{2}-5x-3=0$ one would do as follows:
and the solution can be read to be $x_{1}=-0.5\land x_{2}=3$.
See also polynom and multipol packages

### Derivatives
#### Symbolic calculations

R can give the derivative of an expression. You need to convert your function as an expression using the expression function. Otherwise you get an error message.
Here are some examples :

#### Numerical approximation

* numDeriv package

### Integration

R can perform one dimensional integration. For example we can integrate over the density of the normal distribution between $-\infty$ and $+\infty$
see the adapt package for multivariate integration.
* See also integrate.gh in the ecoreg package.

## Probability

* The number of combination of length k within n numbers : $C_{n}^{k}$
* Union and intersection

## Arithmetics
### The factorial function

factorial returns the factorial of an integer. This can also be computed using the prod (product) applied to the vector of integers between 1 and the number of interest.
Note that by convention $0!=1$. factorial returns 1 in 0. This is not the case with the prod functions.
Factorial numbers can be very large and cannot be computed for high values.

### The modulo function and euclidian division

* Modulo and integer division (i.e. euclidean division)
Note: R is affected by the problem with non integer numbers and euclidian divisions.

## Geometry

* pi the constant
* cos, sin, tan the trigonometric functions.

## Symbolic calculus

rSymPy (rsympy) provides sympy (link) functions in R.
If you want to do more symbolic calculus, see Maxima, SAGE, Mathematica

## See also

The following command gives help on special mathematical functions related to the beta and gamma functions.

## References

* ↑ Maxima is open source http://maxima.sourceforge.net/
* ↑ SAGE is an open source package which includes R and Maxima : http://www.sagemath.org/
* ↑ Mathematica is not open source http://www.wolfram.com/products/mathematica/index.html

# Optimization

* optimize is devoted to one dimensional optimization problem.
* optim, nlm, ucminf (ucminf) can be used for multidimensional optimization problems.
* nlminb for constrained optimization.
* quadprog, minqa, rgenoud, trust packages
* Some work is done to improve optimization in R. See Updating and improving optim, Use R 2009 slides, the R-forge optimizer page and the corresponding packages including optimx.

## Numerical Methods
### One dimensional problem

The one dimensional problem :

### Newton-Raphson

* nlm provides a Newton algorithm.
* maxLik package for maximization of a likelihood function. This package includes the Newton Raphson method.
* newtonraphson in the spuRs package.

### BFGS

* The BFGS method

### Conjugate gradient method

* optim with method="cg".

### Trust Region Method

* "trust" package for trust region method

### The Nelder-Mead simplex method

* The Nelder Mead Method
* The boot package includes another simplex method

## Simulation methods
### Simulated Annealing

* The Simulated Annealing is an algorithm which is useful to maximise non-smooth functions. It is pre implemented in optim.

### Genetic Algorithm

* rgenoud package for genetic algorithm
* gaoptim package for genetic algorithm
* ga general purpose package for optimization using genetic algorithms. It provides a flexible set of tools for implementing genetic algorithms search in both the continuous and discrete case, whether constrained or not.

## References
### Citations

* ↑ Updating and improving optim, Use R 2009 slides http://www.agrocampus-ouest.fr/math/useR-2009/slides/Nash+Varadhan.pdf
* ↑ R-forge optimizer http://optimizer.r-forge.r-project.org/
* ↑ Jasjeet Sekhon homepage : http://sekhon.berkeley.edu/rgenoud/
* ↑ gaoptim on CRAN: http://cran.r-project.org/web/packages/gaoptim/index.html
* ↑ ga on CRAN: http://cran.r-project.org/web/packages/GA/index.html/

### Sources

* Venables and Ripley, Chapter 16.
* Cameron and Trivedi, Microeconometrics, Chapter 10.
* Braun and Murdoch, first course in statistical programming with R (a very good reference on optimization using R), Chapter 7.

# Probability Distributions

This page review the main probability distributions and describe the main R functions to deal with them.
R has lots of probability functions.
* r is the generic prefix for random variable generator such as runif, rnorm.
* d is the generic prefix for the probability density function such as dunif, dnorm.
* p is the generic prefix for the cumulative density function such as punif, pnorm.
* q is the generic prefix for the quantile function such as qunif, qnorm.

## Discrete distributions
### Benford Distribution

The Benford distribution is the distribution of the first digit of a number. It is due to Benford 1938 and Newcomb 1881.

### Bernoulli

We can draw from a Bernoulli using sample, runif or rbinom with size = 1.

### Binomial

We can sample from a binomial distribution using the rbinom function with arguments n for number of samples to take, size defining the number of trials and prob defining the probability of success in each trial.

### Hypergeometric distribution

We can sample n times from a hypergeometric distribution using the rhyper function.

### Geometric distribution

The geometric distribution.

### Multinomial

The multinomial distribution.

### Negative binomial distribution

The negative binomial distribution is the distribution of the number of failures before k successes in a series of Bernoulli events.

### Poisson distribution

We can draw n values from a Poisson distribution with a mean set by the argument lambda.

### Zipf's law

The distribution of the frequency of words is known as Zipf's Law. It is also a good description of the distribution of city size. dzipf and pzipf (VGAM)

## Continuous distributions
### Beta and Dirichlet distributions

* Beta distribution
* Dirichlet in gtools and MCMCpack

### Cauchy

We can sample n values from a Cauchy distribution with a given location parameter $x_{0}$ (default is 0) and scale parameter $\gamma$ (default is 1) using the rcauchy function.

### Chi Square distribution

Quantile of the Chi-square distribution ($\chi ^{2}$ distribution)

### Exponential

We can sample n values from a exponential distribution with a given rate (default is 1) using the rexp function

### Fisher-Snedecor

We can draw the density of a Fisher distribution (F-distribution) :

### Gamma

We can sample n values from a gamma distribution with a given shape parameter and scale parameter $\theta$ using the rgamma function. Alternatively a shape parameter and rate parameter $\beta =1/\theta$ can be given.

### Levy

We can sample n values from a Levy distribution with a given location parameter $\mu$ (defined by the argument m, default is 0) and scaling parameter (given by the argument s, default is 1) using the rlevy function.

### Log-normal distribution

We can sample n values from a log-normal distribution with a given meanlog (default is 0) and sdlog (default is 1) using the rlnorm function

### Normal and related distributions

We can sample n values from a normal or gaussian Distribution with a given mean (default is 0) and sd (default is 1) using the rnorm function
Quantile of the normal distribution
* The mvtnorm package includes functions for multivariate normal distributions.
** rmvnorm generates a multivariate normal distribution.

### Pareto Distributions

* Generalized Pareto dgpd in evd
* dpareto, ppareto, rpareto, qpareto in actuar
* The VGAM package also has functions for the Pareto distribution.

### Student's t distribution

Quantile of the Student t distribution
The following lines plot the .975th quantile of the t distribution in function of the degrees of freedom :

### Uniform distribution

We can sample n values from a uniform distribution (also known as a rectangular distribution] between two values (defaults are 0 and 1) using the runif function

### Weibull

We can sample n values from a Weibull distribution with a given shape and scale parameter $\mu$ (default is 1) using the rweibull function.

### Extreme values and related distribution

* The Gumbel distribution
* The logistic distribution : distribution of the difference of two gumbel distributions.
plogis, qlogis, dlogis, rlogis
* Frechet dfrechet evd
* Generalized Extreme Value dgev evd
* Gumbel dgumbel evd
* Burr, dburr, pburr, qburr, rburr in actuar

### Distribution in circular statistics

* Functions for circular statistics are included in the CircStats package.
** dvm Von Mises (also known as the nircular normal or Tikhonov distribution) density function
** dtri triangular density function
** dmixedvm Mixed Von Mises density
** dwrpcauchy wrapped Cauchy density
** dwrpnorm wrapped normal density.

## See also

* Packages VGAM, SuppDists, actuar, fBasics, bayesm, MCMCpack

## References

* ↑ Benford, F. (1938) The Law of Anomalous Numbers. Proceedings of the American Philosophical Society, 78, 551–572.
* ↑ Newcomb, S. (1881) Note on the Frequency of Use of the Different Digits in Natural Numbers. American Journal of Mathematics, 4, 39–40.
* ↑ Gabaix, Xavier (August 1999). "Zipf's Law for Cities: An Explanation". Quarterly Journal of Economics 114 (3): 739–67. doi:10.1162/003355399556133. ISSN 0033-5533. http://pages.stern.nyu.edu/~xgabaix/papers/zipf.pdf.

# Random Number Generation
## Random Number Generators

To a very high degree computers are deterministic and therefore are not a reliable source of significant amounts of random values. In general pseudo random number generators are used. The default algorithm in R is Mersenne-Twister but a long list of methods is available. See the help of RNGkind to learn about random number generators.
It is possible to use true random numbers. Some of them are collected on random.org (link). The random (link) package gives an access to them.

### Randu

Randu is an old linear congruential pseudorandom number generator. There is a dataset generated with Randu in the datasets package. The function which is used to generate the dataset is in the help of this page.

## Seed

A pseudo random number generator is an algorithm based on a starting point called "seed". If you want to perform an exact replication of your program, you have to specify the seed using the function set.seed. The argument of set.seed has to be an integer.

## Sampling in a vector

Toss 10 coins
Roll 10 dice
play lottery (6 random numbers out of 49 without replacement)
You can sample in a multinomial distribution :

## Sampling in a standard univariate distribution

You can use rnorm, rt, etc.

### Misspecified argument

Note that if you put as argument of rnorm a vector instead of a number, R takes by default the length of the vector instead of returning an error. Here is an example :

## Inverse CDF method

* If you know the inverse CDF (quantile function), you can generate the random variable by sampling in the standard uniform distribution and transforming using the CDF.
For instance, if you want to simulate from a standard normal distribution, you can simulate from a standard uniform and transform it using the quantile function of the normal distribution.
This gives the same results as the rnorm function but the computing time is higher :

## Importance sampling

* See Jeff Gill's routine

## Gibbs algorithm

* rgs package (link)

## Quasi random numbers

* quasi RNG
Sometimes you need to generate quasi random sequences. The randtoolbox library provides several quasi random number generators.
See also sHalton and QUnif (sfsmisc).
* Halton Sequence
You can compare Halton draws with the standard R (pseudo) random number generator. Halton draws are much more systematic.

### Examples
## Resources

* Revolution Computing entry on pseudo random numbers
* Statistical Distributions Module Wessa.net it is online application which generates random numbers using R. You can have access to the R code and use it in your own programs.

## References
# Maximum Likelihood
## Introduction

Maximum likelihood estimation is just an optimization problem. You have to write down your log likelihood function and use some optimization technique. Sometimes you also need to write your score (the first derivative of the log likelihood) and or the hessian (the second derivative of the log likelihood).

## One dimension

If there is only one parameter, we can optimize the log likelihood using optimize.

### Example with a type 1 Pareto distribution

We provide an example with a type 1 Pareto distribution. Note that in this example we treat the minimum as known and do not estimate it. Therefore this is a one-dimensional problem.
We use the rpareto1 (actuar) function to generate a random vector from a type 1 Pareto distribution with shape equal to 1 and minimum value equal to 500. We use the dpareto1 (actuar) function with option log = TRUE to write the log likelihood. Then we just need to use optimize with maximum=TRUE. We provide a minimum and a maximum value for the parameter with the interval option.

## Multiple dimension

* fitdistr (MASS package) fits univariate distributions by maximum likelihood. It is a wrapper for optim.
* If you need to program yourself your maximum likelihood estimator (MLE) you have to use a built-in optimizer such as nlm, optim. R also includes the following optimizers :
* mle in the stats4 package
* The maxLik package

### Example with a logistic distribution

For instance, we draw from a logistic distribution and we estimate the parameters using .

### Example with a Cauchy distribution

For instance, we can write a simple maximum likelihood estimator for a Cauchy distribution using the nlm optimizer. We first draw a vector x from a Cauchy distribution. Then we define the log likelihood function and then we optimize using the nlm function. Note that nlm is minimizer and not a maximizer.

### Example with a beta distribution

Here is an other example with the Beta distribution and the optim function.

## Tests
### Likelihood Ratio Test

* lrtest in the lmtest package.

## Some Specific cases

* gum.fit (ismev package) provides MLE for a Gumbel distributon

## Resources

* Charles Geyer : Maximum Likelihood in R (pdf, 9 pages)
* Marco Steenbergen Maximum Likelihood Programming in R (pdf, 7 pages)

## References

* ↑ Achim Zeileis, Torsten Hothorn (2002). Diagnostic Checking in Regression Relationships. R News 2(3), 7-10. URL http://CRAN.R-project.org/doc/Rnews/

# Method of Moments

* Package gmm implements the generalized method of moment and the generalized empirical likelihood.
First, it is possible to estimate a simple linear model or a simple linear model with instrumental variables using the gmm function. The GMM method is often used to estimate heteroskedastic instrumental variable models.

# Bayesian Methods
## Introduction

R has lots of bayesian estimation procedures, much more than Stata or SAS.
* LearnBayes by Jim Albert
* bayesm by Peter Rossi and Rob McCulloch
* BaM by Jeff Gill
* arm package by Jennifer Hill and Andrew Gelman.
* MCMCpack package.
* mcsm package by Christian Robert and George Casella.
* umacs (link) by Jouni Kerman and Andrew Gelman.

## Interface with WinBugs

* WinBugs/OpenBugs is a popular statistical package for MCMC techniques.
* Andrew Gelman has some instruction to use R and WinBugs on his webpage
* There is also an interface with JAGS

## Resources

* Christian Robert and Jean Michel Marin The Bayesian Core (link including slides and R scripts)
* Jim Albert : Bayesian Computation with R, Use R!, Springer 2007.
* Christian Robert and George Casella Introducing Monte Carlo Methods with R, Use R!, Springer 2009 (link including the mcsm package).
* Peter Rossi, Greg Allenby, Robert McCulloch : Bayesian Statistics and Marketing and "bayesm" package.
* CRAN task view for bayesian statistics (link)

## References
# Bootstrap

* boot package includes functions from the book Bootstrap Methods and Their Applications by A. C. Davison and D. V. Hinkley (1997, CUP)
* bootstrap package.

## Quick how-to

Do a bootstrap of some data for some function (here, mean):
Use this to compute a 90%-confidence interval:

## References

* Instructions for the boot package: http://www.statmethods.net/advstats/bootstrapping.html
* Sample using the boot package: http://druedin.com/2012/11/10/bootstrapping-in-r/

# Multiple Imputation

Multiple imputation of missing data generally includes two steps. First, an imputation step which results in multiple complete datasets. Second, combining the results obtained by applying the chosen technique on each separate dataset. The packages needed for these two steps are not necessary the same, but can be.
* "mi" package Andrew Gelman Blog Entry on the "mi" package
* "mice" package.
* Amelia
* mitools by Thomas Lumley
* survey and Zelig have support for multiple imputated datasets.

## References
# Nonparametric Methods

This page deals with a set of non-parametric methods including the estimation of a cumulative distribution function (CDF), the estimation of probability density function (PDF) with histograms and kernel methods and the estimation of flexible regression models such as local regressions and generalized additive models.
For an introduction to nonparametric methods you can have a look at the following books or handout :
* Nonparametric Econometrics: A Primer by Jeffrey S. Racine.
* Li and Racine's handbook, Nonparametric econometrics.
* Larry Wasserman All of Nonparamatric Statistics

## Empirical distribution function

* The easiest way to estimate the empirical CDF uses the rank and the length functions.
* ecdf computes the empirical cumulative distribution function.
* ecdf.ksCI (sfsmisc) plots the empirical distribution function with confidence intervals.

## Density Estimation
### Histogram

* hist is the standard function for drawing histograms. If you store the histogram as an object the estimated parameters are returned in this object.
It is also possible to choose the break points.
* n.bins (car package) includes several methods to compute the number of bins for an histogram.
* histogram (lattice)
* truehist (MASS)
* hist.scott (MASS) plot a histogram with automatic bin width selection, using the Scott or Freedman–Diaconis formulae.
* histogram package.

### Kernel Density Estimation

* density estimates the kernel density of a vector.
** Choose the bandwidth selection method with bw.
** Check the sensitivity of the bandwidth choice using adjust. The default is one. It is good practice to look at adjust=.5 and adjust=2.
** Choose the kernel function with kernel : "gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", "optcosine".
* tkdensity (sfsmisc) is a nice function which allow to dynamically choose the kernel and the bandwidth with a handy graphical user interface. This is a good way to check the sensitivity of the bandwidth and/or kernel choice on the density estimation.
* kde2d (MASS) estimates a bivariate kernel density.

#### Examples
## Local Regression

* loess is the standard function for local linear regression.
* lowess is similar to loess but does not have a standard syntax for regression y ~ x .This is the ancestor of loess (with different defaults!).
* ksmooth (stats) computes the Nadaraya–Watson kernel regression estimate.
* locpoly (KernSmooth package)
* npreg (np package)
* locpol computes local polynomial estimators
* locfit local regression, likelihood and density estimation

### Examples
## Generalized additive semiparametric models (GAM)

* gam (gam)
* gam (mgcv)

## References

* ↑ Jeffrey S. Racine Nonparametric Econometrics: A Primer http://socserv.mcmaster.ca/racine/ECO0301.pdf and at the R code examples http://socserv.mcmaster.ca/racine/primer_code.zip
* ↑ Qi Li, Jeffrey S. Racine, Nonparametric econometrics, Princeton University Press - 2007
* ↑ Wasserman, Larry, "All of nonparametric statistics", Springer (2007) (ISBN: 0387251456)

# Linear Models
## Standard linear model

In this section we present estimation functions for the standard linear model estimated by ordinary least squares (OLS). Heteroskedasticity and endogeneity are treated below. The main estimation function is lm.

### Fake data simulations

We first generate a fake dataset such that there is no hetereoskedasticity, no endogeneity and no correlation between the error terms. Therefore the ordinary least square estimator is unbiased and efficient. We choose a model with two variables and take all the coefficients equal to one.
$y_{i}=1+x_{1,i}+x_{2,i}+u_{i}$

### Least squares estimation

* The standard function to estimate a simple linear model is lm.
* lsfit performs the least square procedure but the output is not formatted in fashionable way.
* ols (Design) is another alternative.
We estimate the model using lm. We store the results in fit and print the result using summary which is the standard function.
There are some alternative to display the results.
* display in the arm package is one of them.
* coefplot (arm) graphs the estimated coefficients with confidence intervals. This is a good way to present the results.
* mtable in the memisc package can display the results of a set of regressions in the same table.
fit is a list of objects. You can see the list of these objects by typing names(fit). We can also apply functions to fit.
We can get the estimated coefficients using fit$coeff or coef(fit).
se.coef (arm) returns the standard error of the estimated coefficients.
The vector of fitted values can be returned via fit$fitted, fitted(fit) or the predict function. The predict function also returns standard error and confidence intervals for predictions.
The vector of residuals:
The number of degrees of freedom :

#### Confidence intervals

We can get the confidence intervals using confint or conf.intervals in the alr3 package.

#### Tests

coeftest (lmtest) performs the Student t test and z test on coefficients.
linear.hypothesis (car) performs a finite sample F test on a linear hypothesis or an asymptotic Wald test using $\mathrm {X} ^{2}$ statistics.
See also waldtest (lmtest) for nested models.

#### Analysis of variance

We can also make an analysis of variance using anova.

#### Model Search and information criteria

The stats4 package includes AIC and BIC function:
The step functions performs a model search using the Akaike Information Criteria.

#### Zelig

* The method is also supported in Zelig

### Bayesian estimation

* MCMCregress (MCMCpack)
* BLR (BLR)

## Heteroskedasticity

* See the lmtest and sandwich packages.
* gls (nlme) computes the generalized least squares estimator.
* See "Cluster-robust standard errors using R" (pdf) by Mahmood Arai. He suggests two functions for cluster robust standard errors. clx allow for one-way clustering and mclx for two-way clustering. They can be loaded with the following command source("http://people.su.se/~ma/clmclx.R").

## Robustness

Cook's distance
Influence plot:
Leverage plots:
Bonferroni's outlier test:
See also outlier.t.test in the alr3 package.
* inf.index in the alr3 package computes all the robustness statistics (Cook's distance, studentized residuals, outlier test, etc)
* rlm performs a robust estimation
* See UCLA example
* See also the robustbase package

## Instrumental Variables

* ivreg in the AER package
* tsls in the sem package.
* It is also possible to use the gmm command in the gmm package. See Methods of moments for an example.

### Fake data simulations

We first simulate a fake data set with x correlated to u, z and u independent and x correlated with z. Thus x is an endogenous explanatory variable of y and z is a valid instrument for x.

### Two stage least squares

Then we estimate the model with OLS (lm) and IV using z as an instrument for x.
We plot the results :

## Panel Data

plm (plm) implements the standard random effect, fixed effect, first differences methods. It is similar to Stata's xtreg command.
Note that plm output are not compatible with xtable and mtable for publication quality output.
* lme4 and gee implements random effect and multilevel models.
* See also BayesPanel

### Random effects model

To implement a random effects model we generate a fake data set with 1000 observations over 5 time periods.
We estimate the random effect model with the plm function and the model = "random" option.

### Fixed effects model

For a fixed effects model we generate a fake dataset and we correlate the fixed effects f with covariates :
We first transform our data in a plm data frame using plm.data. We estimate the fixed model using plm with model = "within" as an option. Then, we compare the estimate with the random effect model and perform an Hausman test. At the end, we plot the density of the fixed effects.

### Dynamic panel data

* pgmm (plm) implements the Arellano Bond estimation procedure. It is similar to xtabond2 in Stata.

## Simultaneous equations model

For a [:w:Simultaneous_equations_model|simultaneous equations model] the following packages are needed :
* sem package
* systemfit package

## References

* ↑ Christian Kleiber and Achim Zeileis (2008). Applied Econometrics with R. New York: Springer-Verlag. ISBN 978-0-387-77316-2. URL http://CRAN.R-project.org/package=AER
* ↑ Yves Croissant, Giovanni Millo (2008). Panel Data Econometrics in R: The plm Package. Journal of Statistical Software 27(2). URL http://www.jstatsoft.org/v27/i02/.
* ↑ M Arellano, S Bond "Some tests of specification for panel data: Monte Carlo evidence and an application to employment equations" - The Review of Economic Studies, 1991
* ↑ David Roodman, XTABOND2: Stata module to extend xtabond dynamic panel data estimator, http://ideas.repec.org/c/boc/bocode/s435901.html

## External links

* Visualization of regression coefficients

# Quantile Regression

Quantile regression is a very old method which has become popular only in the last years thanks to computing progress. One of the main researcher in this area is also a R practitioner and has developed a specific package for quantile regressions (quantreg) ·.
In theory, Quantile regression are also linear and thus could have been included in the Linear regression page. However, this is a very specific topic and we think that it is worth writing a specific page for this topic.

## Simple quantile model

We simulate from a simple quantile model. We first generate a uniform error term u and a covariate x.
We estimate the quantile model for some values of tau (the quantile) and plot the coefficients :
We then plot the scatterplot, the predicted values using a standard linear model and the predicted values using a quantile linear model :
We can also estimate the model for all quantiles at the same time :

### Computing time

For large data sets it is better to use the "fn" or "pfn" method.

## Resources

* Koenker, Roger (2005) Quantile Regression, Cambridge University Press. ISBN 0-521-60827-9

## References

* ↑ Roger Koenker (2010). quantreg: Quantile Regression. R package version 4.50. http://CRAN.R-project.org/package=quantreg
* ↑ Roger Koenker's personal webpage

# Binomial Models

In this section, we look at the binomial model. We have one outcome which is binary and a set of explanatory variables.
This kind of model can be analyzed using a linear probability model. However a drawback of this model for the parameter of the Bernoulli distribution is that, unless restrictions are placed on $\beta$, the estimated coefficients can imply probabilities outside the unit interval $[0,1]$ . For this reason, models such as the logit model or the probit model are more commonly used. If you want to estimate a linear probability model, have a look at the linear models page.

## Logit model

The model takes the form : $y_{i}\sim Bernoulli(\pi _{i})$ with the inverse link function : $\pi _{i}={\frac {exp(x_{i}'\beta )}{(1+exp(x_{i}'\beta ))}}$. It can be estimated using maximum likelihood or using bayesian methods.

### Maximum likelihood estimation

* The standard way to estimate a logit model is glm function with family binomial and link logit.
* lrm (Design) is another implementation of the logistic regression model.
* There is an implementation in the Zelig package.
In this example, we simulate a model with one continuous predictor and estimate this model using the glm function.

#### Zelig

The Zelig' package makes it easy to compute all the quantities of interest.
We develop a new example. First we simulate a new dataset with two continuous explanatory variables and we estimate the model using zelig with the model = "logit" option.
* We the look at the predicted values of y at the mean of x1 and x2
* Then we look at the predicted values when x1 = 0 and x2 = 0
* We also look at what happens when x1 changes from the 3rd to the 1st quartile.
* ROC Curve in the verification package.
* Zelig has a rocplot function.
* See UCLA Statistical Computing example

### Bayesian estimation

* bayesglm in the arm package
* MCMClogit in the MCMCpack for a bayesian estimation of the logit model.

## Probit model

The probit model is a binary model in which we assume that the link function is the cumulative density function of a normal distribution.
We simulate fake data. First, we draw two random variables x1 and x2 in any distributions (this does not matter). Then we create the vector xbeta as a linear combination of x1 and x2. We apply the link function to that vector and we draw the binary variable y as Bernouilli random variable.

### Maximum likelihood

We can use the glm function with family=binomial(link=probit) option or the probit function in the sampleSelection package which is a wrapper of the former one.

### Bayesian estimation

* MCMCprobit (MCMCpack)

### See Also

* There is an example of a probit model with R on the UCLA statistical computing website.

## Semi-Parametric models

* Klein and Spady estimator is implemented in the np package (see npindex with method = "kleinspady" option).

## References

* ↑ Kosuke Imai, Gary King, and Oliva Lau. 2008. "logit: Logistic Regression for Dichotomous Dependent Variables" in Kosuke Imai, Gary King, and Olivia Lau, "Zelig: Everyone's Statistical Software," http://gking.harvard.edu/zelig
* ↑ UCLA statistical computing probit example http://www.ats.ucla.edu/stat/R/dae/probit.htm
* ↑ Klein, R. W. and R. H. Spady (1993), “An efficient semiparametric estimator for binary response models,” Econometrica, 61, 387-421.
* ↑ Tristen Hayfield and Jeffrey S. Racine (2008). Nonparametric Econometrics: The np Package. Journal of Statistical Software 27(5). URL http://www.jstatsoft.org/v27/i05/.

# Multinomial Models
## Multinomial Logit

* mlogit package.
* mnlogit package
* Bayesm package
* multinom nnet
* multinomial, which is used by vglm VGAM

## Conditional Logit

* clogit in the survival package
* mclogit package.

## Multinomial Probit

* mprobit package
* MNP package to fit a multinomial probit.

## Multinomial ordered logit model

We consider a multinomial ordered logit model with unknown thresholds. First, we simulate fake data. We draw the residuals in a logistic distribution. Then we draw some explanatory variable x and we define ys the latent variable as a linear function of x. Note that we set the constant to 0 because the constant and the thresholds cannot be identified simultaneously in this model. So we need to fix one of the parameters. Then, we define thresholds (-1,0,1) and we define our observed variable y using the cut function. So y is an ordered multinomial variable.

### Maximum likelihood estimation

This model can be estimated by maximum likelihood using the polr function in the MASS package. Since it is not possible to achieve identification of the constant and the thresholds, R assumes by default that the constant is equal to 0.

### Bayesian estimation

* bayespolr (arm) performs a bayesian estimation of the multinomial ordered logit

## Multinomial ordered probit model

We generate fake data by drawing an error term in normal distribution and cutting the latent variables in 4 categories.

### Maximum likelihood estimation

The model can be fitted using maximum likelihood method. This can be done using the polr function in the MASS package with the probit method.

### Bayesian estimation

* bayespolr (arm) performs a bayesian estimation of the multinomial ordered probit

## Rank Ordered Logit Model

This model was introduced in econometrics by Beggs, Cardell and Hausman in 1981. One application is the Combes et alii paper explaining the ranking of candidates to become professor. Is is also known as Plackett–Luce model in biomedical literature or as exploded logit model in marketing.

## Conditionally Ordered Hierarchical Probit

* The Conditionally Ordered Hierarchical Probit can be estimated using the anchors package developped by Gary King and his coauthors.

## References

* ↑ Harry Joe, Laing Wei Chou and Hongbin Zhang (2006). mprobit: Multivariate probit model for binary/ordinal response. R package version 0.9-2.
* ↑ Beggs, S; Cardell, S; Hausman, J (1981). "Assessing the potential demand for electric cars". Journal of Econometrics. 17: 1–19. doi:10.1016/0304-4076(81)90056-7.
* 1 2 3 Combes, Pierre-Philippe; Linnemer, Laurent; Visser, Michael (2008). "Publish or peer-rich? The role of skills and networks in hiring economics professors". Labour Economics. 15 (3): 423–41. doi:10.1016/j.labeco.2007.04.003.
* ↑ Jonathan Wand, Gary King, Olivia Lau (2009). anchors: Software for Anchoring Vignette Data. Journal of Statistical Software, Forthcoming. URL http://www.jstatsoft.org/.

# Tobit And Selection Models
## Tobit (type 1 Tobit)

In this section, we look at simple tobit model where the outcome variable is observed only if it is above or below a given threshold.
* tobit in the AER package. This is a wrapper for survreg.

## Selection models (type 2 tobit or heckit)

In this section we look at endogenous selection process. The outcome y is observe only if d is equal to one with d a binary variable which is correlated with the error term of y.
* heckit and selection in sampleSelection. The command is called heckit in honor of James Heckman.

## Multi-index selection models

In this section we look at endogenous selection processes in matching markets. Matching is concerned with who transacts with whom, and how. For example, which students attend which college. The outcome y is observed only for equilibrium student-college pairs (or matches). These matches are indicated with d equal to one with d a binary variable which is correlated with the error term of y.
* stabit and stabit2 in matchingMarkets. The command is called stabit in reference to the application in stable matching markets.
Simulate two-sided matching data for 20 markets (m=20) with 100 students (nStudents=100) per market and 20 colleges with quotas of 5 students, each (nSlots=rep(5,20)). True parameters in selection and outcome equations are all equal to 1.
Observe the bias from sorting between students and colleges.
Correct for sorting bias by running the Gibbs sampler in Sorensen (2007).

## Truncation

* truncreg package
* DTDA "An R package for analyzing truncated data" pdf.

## References

* ↑ Christian Kleiber and Achim Zeileis (2008). Applied Econometrics with R. New York: Springer-Verlag. ISBN 978-0-387-77316-2. URL http://CRAN.R-project.org/package=AER
* ↑ Sample Selection Models in R: Package sampleSelection http://www.jstatsoft.org/v27/i07
* ↑ James Heckman "Sample selection bias as a specification error", Econometrica: Journal of the econometric society, 1979
* ↑ Klein, T. (2015). "Analysis of Stable Matchings in R: Package matchingMarkets" (PDF). Vignette to R Package matchingMarkets.
* ↑ "matchingMarkets: Analysis of Stable Matchings". R Project.
* ↑ Sorensen, M. (2007). "How Smart is Smart Money? A Two-Sided Matching Model of Venture Capital". Journal of Finance. 62 (6): 2725–2762.

# Count Data Models
## The Poisson model
### Fake data simulations

We assume that y follows a poisson distribution with mean exp(1 + 1 * x). We store the data in the "df" dataframe.

### Maximum likelihood

We estimate this simple model using the glm function with family = poisson as option.

### Bayesian estimation

The model can also be estimated using bayesian methods with the MCMCpoisson function which is provided in the MCMCpack.

## Overdispersion test

* dispersiontest (AER package) provides a test for equidispersion.

## Zero inflated model

See the zic package

## Bivariate poisson regression

* bivpois package for bivariate poisson regression.

## References

* See UCLA website for an example
* Zeileis, A., Kleiber, C. and Jackman, S. Regression Models for Count Data in R
* Replication files for Cameron and Trivedi's 1998 book are provided in the AER package. You can simply type ?CameronTrivedi1998 and you will find the source code.
* ↑ Markus Jochmann (2010). zic: Bayesian Inference for Zero-Inflated Count Models. R package version 0.5-3. http://CRAN.R-project.org/package=zic
* ↑ Cameron, A.C. and Trivedi, P.K. (1998). Regression Analysis of Count Data. Cambridge: Cambridge University Press.
* ↑ Christian Kleiber and Achim Zeileis (2008). Applied Econometrics with R. New York: Springer-Verlag. ISBN 978-0-387-77316-2. URL http://CRAN.R-project.org/package=AER

# Duration Analysis

* Using R for Survival Analysis (pdf)
* See the survival package
* bootkm Bootstrap Kaplan-Meier Estimates in Hmisc package
* event.chart Flexible Event Chart for Time-to-Event Data in the Hmisc package

## References
# Time Series
## Introduction

In the following examples we will use the data set Mpyr which is included in the R-package Ecdat, which can be loaded into R and viewed in R by the following code.

## Creating time-series objects

* The function ts is used to create time-series objects.
* The function as.ts coerces an object to a time-series.
* The function is.ts tests whether an object is a time-series.
Example:

## Creating lagged and differenced variables

* The function lag creates a lagged variable.
* The function diff creates a differenced variable.
Example:

## Plotting time-series objects

* The function plot.ts is used for plotting time-series objects.

## Fit Autoregressive Models to Time-series by OLS

In order to fit an autoregressive time series model to the data by ordinary least squares it is possible to use the function ar.ols which is part of the "stats" package.

## Autocorrelation function

The function acf computes (and by default plots) estimates of the autocovariance or autocorrelation function. Function pacf is the function used for the partial autocorrelations. Function ccf computes the cross-correlation or cross-covariance of two univariate series.

## Useful R-packages

* fBasics, tis, zoo, tseries, xts, urca, forecast

## References

* ↑ http://www.inside-r.org/r-doc/stats/acf
http://cran.r-project.org/web/views/TimeSeries.html
http://cran.r-project.org/doc/contrib/Ricci-refcard-ts.pdf

# Factor Analysis
## Introduction

Factor analysis is a set of techniques to reduce the dimensionality of the data. The goal is to describe the dataset with a smaller number of variables (ie underlying factors). Factor Analysis was developed in the early part of the 20th century by L.L. Thurstone and others. Correspondence analysis was originally developed by Jean-Paul Benzécri in the 60's and the 70's. Factor analysis is mainly used in marketing, sociology and psychology. It is also known as data mining, multivariate data analysis or exploratory data analysis.
There are three main methods. Principal Component Analysis deals with continuous variables. Correspondence Analysis deals with a contingency table (two qualitative variables) and Multiple correspondence analysis is a generalization of the correspondence analysis with more than two qualitative variables. The major difference between Factor Analysis and Principal Components Analysis is that in FA, only the variance which is common to multiple variables is analysed, while in PCA, all of the variance is analysed. Factor Analysis is a difficult procedure to use properly, and is often misapplied in the psychological literature. One of the major issues in FA (and PCA) is the number of factors to extract from the data. Incorrect numbers of factors can cause difficulties with the interpretation and analysis of the data.
There are a number of techniques which can be applied to assess how many factors to extract. The two most useful are parallel analysis and the minimum average partial criterion. Parallel analysis works by simulating a matrix of the same rank as the data and extracting eigenvalues from the simulated data set. The point at which the simulated eigenvalues are greater than those of the data is the point at which the "correct" number of factors have been extracted. The Minimum Average Partial criterion uses a different approach but can often be more accurate. Simulation studies have established these two methods as the most accurate. Both of these methods are available in the psych package under the fa.parallel and the VSS commands.
Another issue in factor analysis is which rotation (if any) to choose. Essentially, the rotations transform the scores such that they are more easily interpretable. There are two major classes of rotations, orthogonal and oblique. Orthogonal rotations assume that the factors are uncorrelated, while oblique rotations allow the factors to correlate (but do not force this). Oblique rotations are recommended by some (e.g. MacCallum et al 1999) as an orthogonal solution can be obtained from an oblique rotation, but not vice versa.
One of the issues surrounding factor analysis is that there are an infinite number of rotations which explain the same amount of variance, so it can be difficult to assess which model is correct. In response to such concerns, Structural Equation Modelling (SEM), which is also known as Confirmatory Factor Analysis (CFA) was developed by Joreskeg in the 1970's. The essential principle of SEM is that given a model, it attempts to reproduce the observed covariance matrix seen in the data. The ability of a model to reproduce the data can be used as a test of that model's truth. SEM is implemented in R in the sem and lavaan packages, as well as the OpenMx package (which is not available on CRAN).
See the following packages : FactoMineR (website), amap, ade4, anacor, vegan, '"psych"'

## Principal Component Analysis (PCA)

PCA deals with continuous variables
* prcomp in the stats package.
* princomp in the stats package.
* PCA (FactoMineR)
* See also factanal
* See also fa and prcomp in the psych package

## Correspondence Analysis (CA)

Correspondence analysis is a tool for analyzing contingency tables.
* corresp MASS
* Michael Greenacre's ca package (JSS article)
* Correspondence Analysis and Related Network (link)
* Quick-R's page (link)
* Simple and Canonical Correspondence Analysis Using the R Package anacor (pdf, JSS article)
* multiv

## References
# Ordination
## Overview

This page provides basic code for creating a distance matrix and running and plotting a Non-metric Multidimensional Scaling (NMDS) ordination.
Read more about Ordination on Wikipedia.
This code relies on package vegan in R by Jari Oksanen.

## Data

First, import data and load required libraries:

## Distance matrix
# Unconstrained Ordination
## Displaying dissimilarity using NMDS

NMDS analysis and plotting:
In the metaMDS function, k is user-defined and relates to how easily the projection fits the dataframe when constrained to k dimensions. Conventional wisdom seems to suggest that stress should not exceed 10-12%. Stress is reduced by increasing the number of dimensions. However, increasing dimensionality might decrease the "realism" of a 2-dimensional plot of the first two NMDS axes.
We can also run a nMDS with 3 dimensions, fit environmental vectors and create a dynamic graph:

## Running a principle component analysis (PCA) on environmental data
# Clustering
## Basic clustering
### K-Means Clustering

You can use the kmeans function.
First create some data:
To apply kmeans, you need to specify the number of clusters:

### Hierarchical Clustering

The basic hierarchical clustering function is hclust, which works on a dissimilarity structure as produced by the dist function:
The resulting tree can be cut using the cutree function.
Cutting it at a given height:
Cutting it to obtain given number of clusters:

## Available alternatives

* See packages class, amap and cluster
* See The R bioinformatic page on clustering

## References

* "The Elements of Statistical Learning"

## External links

* Clustergram: visualization and diagnostics for cluster analysis

# Network Analysis
## Introduction

We mainly use the following packages to demonstrate network analysis in R: statnet, sna, igraph. They are however not representing a complete list. See Task view of gR, graphical models in R for a complete list.

## Creating graphs from data

First load the igraph package
then you can choose your preferred format. Below are examples of data provided as edge list and as adjacency matrix.

### Creating graph from an edge list

An edge list is formed by a two-column matrix, with each row defining one edge. An edge is drawn from each element in the first column to the corresponding element in the second one. Use the graph.edgelist function to import your data.

### Creating graph from an adjacency matrix

An adjacency matrix is a n × n matrix containing n vertices and where each entry a$_{ij}$ represents the number of edges from vertex i to vertex j. To import your adjacency matrix, use the graph.adjacency function.

## References

* Statnet website includes all the documentation on network analysis using R.
* Julien Barnier's introduction (in French)
* Journal of Statistical Software #24 Special Issue on Networks in R

# Profiling R code

Before starting with parallel or high performance computing it is important to analyze and optimize R code. R provides some useful tools to analyze and profile R code. A good and short introduction is provided in the R extension documentation.
Soon we are going to provide some example code:

# Parallel computing with R

There are many packages and tools available for parallel computing with R. A good overview is provided by the CRAN Task View: High-Performance and Parallel Computing with R and several publications:
* State of the Art in Parallel Computing with R; Markus Schmidberger, Martin Morgan, Dirk Eddelbuettel, Hao Yu, Luke Tierney, Ulrich Mansmann; Journal of Statistical Software 2009: JSS
Soon we are going to provide some code examples:

# Sources

For the following resources, authors have explicitly given the permission to include their material on the R programming wikibook. Remember that even if they have given their permission, they should be correctly cited.

## Blogs

* R-statistics (the R category) (A link to a post which provides proper licence for approving this content for use).
* GETTING GENETICS DONE - R tag. The R content is available from here: http://gettinggeneticsdone.blogspot.com/search/label/R. The R code is copyrighted under The open source BSD license (as is described here: http://gettinggeneticsdone.blogspot.com/p/copyright.html). The content itself is licensed under a Creative Commons Attribution-Share-Alike 3.0 Unported License (as is shown at the bottom of every post). Bottom line - the R code and written content can be used freely (with attribution).
* Struggling Through Problems: http://strugglingthroughproblems.blogspot.com/search/label/R
* Backsidesmack R-stuff category. Copyright information is in the footer and explicit permission is in this post
* Al3xandr3: http://al3xandr3.github.com/tags/r.html
* Cloudnumbers.com (the R category): Posts about high-performance computing and cloud computing with R. A link to a post which provides proper license for approving this content for use.
* The R Tutorial Series (http://rtutorialseries.blogspot.com) by John M. Quick provides a collection of user-friendly guides to researchers, students, and others who want to learn how to use R for their statistical analyses. Its content is available for use in the R Programming wikibook under a Creative Commons BY-SA License.
* Exploring Indian census data using R and useful scripts to download weather related data from websites. The content is available for use in the R wikibook under cc-sa license.
* Plain Data Analysis tips at www.danielmarcelino.com .Topics covered in the blog are related to social sciences, but there is a great variety of them.
* R Tutorial
* R Workshop

## Handouts
# Index

This page provides tables which make it easy to find functions for usual tasks in statistics in R, SAS and Stata. Other software may also be included in the future such as SPSS.

## Data management

| Function | R | Stata | SAS |
| Merge | merge | merge / mmerge | - |
| Reshape | reshape | reshape | - |
| Expand a dataset | expand (epicalc) | expand | - |

## Descriptive Statistics

| Function | R | Stata | SAS |
| Mean | mean | mean | proc means |
| Histogram | hist | hist | - |
| Frequency table | table | ta | proc freq |

## Regression models

| Function | R | Stata | SAS |
| Least Square | lm | reg | proc reg |
| GLM | glm | glm | proc glm |
| Probit models | glm(, family = binomial(link="probit")) | probit | - |
| Logit models | glm(, family = binomial(link="logit")) | logit | - |
| Linear fixed effects model | plm( , model = "within") (plm) | xtreg , fe | - |
| Linear random effects model | plm( , model = "random") (plm) | xtreg , re | - |
| Linear quantile regression | rq (quantreg) | qreg | - |
| Ordinal logistic regression | polr (MASS) | ologit | - |
| Linear IV (2sls) | ivreg (AER) | ivreg | proc syslin (2sls) - |

## Programming

| Function | R | Stata | SAS |
| Check some condition | stopifnot | assert | - |