Vous êtes sur la page 1sur 29

Calculator

Statistical Applications with R

Prof. Pritam Ranjan

OM & QT, IIM Indore

Email: pritamr@iimidr.ac.in
Office: A - 102, Phone: 512

Session - 1: R as a Calculator,
Arrays, Lists and Summary statistics

Prof. Pritam Ranjan SAR(June 21, 2018) 1/29


Calculator

A yog-ic perspective !!

SAR
=

Applied Statistics
+
R – Easy yet powerful implementation tool

Prof. Pritam Ranjan SAR(June 21, 2018) 2/29


Calculator

Reading material for Sessions 1-5

– https://cran.r-project.org/manuals.html

– Rakshit, S. (2017), R for beginners, McGraw Hill


Education (India) Private Limited.

Prof. Pritam Ranjan SAR(June 21, 2018) 3/29


Calculator

Basic Arithmetic

Addition, subtraction, multiplication, division,

help(exp) ≈ ?exp
help(”+”) ≈ ?”+”
help.search(”sin”) ≈ ??sin

mod (remainder), quotient (divisor)

brackets - only use () in arithmetic expression

Special functions: exp, log, sin, power

Prof. Pritam Ranjan SAR(June 21, 2018) 4/29


Calculator

Remark

Formal R code writing - a bit later

Saving output
– sink(”myoutput.txt”)
– sink()

# – comment

Prof. Pritam Ranjan SAR(June 21, 2018) 5/29


Calculator

Defining variables

x=2 or x <– 2
”2 –> x” also works
x = ”bob” / x = ’bob’
x = ”Statistical Applications with R”

case sensitive / alpha-numeric (start with alphabet)

bob = (2+3/4)*5+sin(30)

cos = (2+3/4)*5+sin(30)

varnames = paste(”X”,1:10,sep=””)

Prof. Pritam Ranjan SAR(June 21, 2018) 6/29


Calculator

Vectors - 1

x = c(32,54,54,32,21,1.23)
x = 2:10
x = -2.5:5.3
x=c(1,2,34,-23,”bob”)
x=c(1,2,34,-23, x)

Can also use: NA, Inf, NaN (case sensitive)

Prof. Pritam Ranjan SAR(June 21, 2018) 7/29


Calculator

Vectors - 2

x = seq()

x = numeric(5)

y = rep(-0.1,3) ; y = rep(x,3)

Vector arithmetic:
x+2 ; x*2 ; x/2 ;
x + y ; 1/x ; sin(x) ; log(x) ; x ˆ2

Prof. Pritam Ranjan SAR(June 21, 2018) 8/29


Calculator

subsetting

Index set via [ ]


x[1]; x[c()]
x[-2]; x[-c()]
y=x[condition]

Conditions:
y=x[!is.na(x)]
> is.nan(x) / is.infinite(x) / is.finite(x)
x[x < 0] = 0
use of multiple conditions via (& or &&) and (| or k)

Prof. Pritam Ranjan SAR(June 21, 2018) 9/29


Calculator

Matrices - 1

x = matrix(0,2,3) ; A = array(0,dim=c(2,3))

x = matrix(1:5,ncol=2,byrow=T)

arithmetic:
x+2 ; x*2 ; x/2 ;
x + y ; 1/x ; sin(x) ; log(x) ; x ˆ2

Prof. Pritam Ranjan SAR(June 21, 2018) 10/29


Calculator

Matrices - 2

subsetting
x[1,2] ; x[,2] ; x[,c(1,3)]
x[-1,2] ; x[6]
x[c(),c()]
A[condition-1, condition-2]

multiplication: A*B vs. A%*%B vs. a%o%b


Inverse: Aˆ(−1) vs. solve(A)
t(A) ; det(A);

Prof. Pritam Ranjan SAR(June 21, 2018) 11/29


Calculator

Important functions
For a vector: x
length(x)
sum(x) ; prod(x) ;
min(x); max(x)

pmax(v1,v2,..,vk)/pmin() – columnwise max/min


(pmax(A) - where A is a matrix)

For a matrix: A
dim(A)
rowSums(A) ; colSums(A)

Prof. Pritam Ranjan SAR(June 21, 2018) 12/29


Calculator

Exercise
solve the system of equation: Ax = b

The entries of A are as follows:


1 1 2 -5
2 5 -1 -9
2 1 -1 3
1 3 2 7
and b = c(3, -3, -11, -5)

Prof. Pritam Ranjan SAR(June 21, 2018) 13/29


Calculator

Important functions

apply() / tapply()

cbind() / rbind()

which() / which.max()

Prof. Pritam Ranjan SAR(June 21, 2018) 14/29


Calculator

Factors

values = c(1,2,1,1,2,3,3)
vlabels = factor(values, levels=1:3)
levels(vlabels) = c(”low”,”med”,”high”)

fvalues = factor(values) / fvalues = as.factor(values)


summary(fvalues)
table(values) / table(fvalues)

Prof. Pritam Ranjan SAR(June 21, 2018) 15/29


Calculator

Important commands

q()

control L / control C / control R / control ”enter”

up arrow / down arrow

dir() / ls() / objects() / attributes(object) / rm()

getwd() / setwd()

save.image(”mycommands.RData”)
load(”mycommands.RData”)

Prof. Pritam Ranjan SAR(June 21, 2018) 16/29


Calculator

Lists

Prof. Pritam Ranjan SAR(June 21, 2018) 17/29


Calculator

Lists

xx = NULL;
xx$a = scalar
xx$b = vector
xx$c = matrix
xx$d = character string

yy = NULL
define yy[[i]] for i=1,2,...,k

(allows different type of structures including “objects”)

Prof. Pritam Ranjan SAR(June 21, 2018) 18/29


Calculator

Lists
creating a list
a = c() # numeric
b = c() # characters
d = matrix
mylist = list(a,b,d)

Combining lists
list3 = append(list1,list2) / c(list1, list2)

Deleting entries
list3[[2]] = NULL

Prof. Pritam Ranjan SAR(June 21, 2018) 19/29


Calculator

Practice Exercises

Prof. Pritam Ranjan SAR(June 21, 2018) 20/29


Calculator

Debugging Exercise
x = c(10,21,45,56,67,78,24,47)
> print(x)
> length(x) <- 10
> print(x)

Prof. Pritam Ranjan SAR(June 21, 2018) 21/29


Calculator

Debugging Exercise
Which of the following is incorrect?
(a) 10 + 5
(b) 10 ∗ 5
(c) 10 − 5
(d) 10 / 5
(e) 10 % 5

Prof. Pritam Ranjan SAR(June 21, 2018) 22/29


Calculator

Exercise
Guess the possible output
(a) 1:10
(b) 10:1
(c) 4*10:2
(d) seq(1:5,by=0.2)
(e) 2:20 + 20:2

Prof. Pritam Ranjan SAR(June 21, 2018) 23/29


Calculator

Exercise
Construct the following matrix using R

2 7 12 17 3
3 8 13 18 4
4 9 14 19 5
5 10 15 20 6
6 11 16 2 7

Prof. Pritam Ranjan SAR(June 21, 2018) 24/29


Calculator

Summary Statistics

Prof. Pritam Ranjan SAR(June 21, 2018) 25/29


Calculator

Summary
mean, median, mode, variance, sd,
Find mode of x via
“sort(unique(x))[which.max(table(x))]”

sort, order,
order(x) contains the location of sorted-x (in increasing
order) in the original x vector
e.g., x = c(2,34,12,3,5) gives order(x) = [1 4 5 3 2], i.e.,
2 appears in the 1st location, 3 appears in 4th location, 5
appears in the 5th location, 12 appears in the 3rd
location and 34 appears in the 2nd location.

Prof. Pritam Ranjan SAR(June 21, 2018) 26/29


Calculator

Summary
correlation (spearman, pearson, ...)

na.rm=TRUE

Prof. Pritam Ranjan SAR(June 21, 2018) 27/29


Calculator

Summary
skewness, kurtosis

>install.packages(”moments”)
>library(moments)
>skewness(x)
>moment(x)

Another library: e1071

Prof. Pritam Ranjan SAR(June 21, 2018) 28/29


Calculator

Homework

Practice the commands !!


https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf

Prof. Pritam Ranjan SAR(June 21, 2018) 29/29

Vous aimerez peut-être aussi