e-Statistics > 4470-5470 Probability and Statistics I

# Get started with R

R is a language and environment for statistical computing and graphics, which is similar to S-Plus. It is available as Free Software under the terms of the Free Software Foundation's GNU General Public License. The system runs on Windows, Linux, Mac. You can download it from CRAN R project which has software packages for Unix, Linux, Windows, and Mac.

R is a programming language, and runs a command'' in an interactive manner, known as interpretor.'' Each command is requested in a form of function'': For example, it is the function q() to quit the program.

> q()


Use it as a calculator. We can execute arithmetic operations at the prompt. For example, can be performed by typing

> 8^4 * 12^3


Computation of factorials and combinations can be done by using the function factorial() and choose(). For example, you can obtain and by

> factorial(16)
> choose(20,8)


Use it with computer graphics. Here we write a simple code to create a probability frequency plot. To draw it we need to use it as a special function prob.hist() written as follows:

prob.hist = function(x,prob,...){
for(i in 1:length(x)){
polygon(c(x[i]-0.5,x[i]-0.5,x[i]+0.5,x[i]+0.5),
c(0,prob[i],prob[i],0),...)
}
}


The probability mass function of Poisson distribution at x with parameter lambda is given by

pmf = dpois(x,lambda)

Similarly the cumulative distribution function is obtained by ppois(x,lambda).

n = 15
lambda = 3
x = 0:n
prob = dpois(x,lambda)
cbind(x,prob)
par(mfrow=c(2,1))
plot(c(-0.5,n+0.5),c(0,max(prob)+0.1),type='n',main='p.m.f',xlab='x',ylab='pmf')
prob.hist(x,prob,col='green')
t = c(-0.5,x,n+0.5)
F = ppois(t,lambda)
plot(t,F,type='s',col='red',main='c.d.f')
lines(c(-0.5,n+0.5),c(1,1),lty=2,col='red')
cdf = ppois(x,lambda)
cbind(x,cdf)


Programming Note. The function par(mfrow=c(2,1)) set the parameter mfrow=c(2,1) which splits the multiple graphics to assign 2 figures horizontally and one vertically.