Lab 01
Lab 01
Principles of Statistics
1 / 36
A first R session
x <- c(1,2,4)
x[2:3]
## [1] 2 4
▶ < − or =: standard assignment operator
▶ c: concatenate
2 / 36
x <- c(1,2,4)
q <- c(x,x,8)
q
## [1] 1 2 4 1 2 4 8
▶ Set q to (1, 2, 4, 1, 2, 4, 8) including the duplicates
Note: If you type any variable name (or any expression) while in
interactive mode, R will print out the value of that variable (or
expression).
3 / 36
Summary statistics
c(mean(x),sd(x))
## [1] 2.333333 1.527525
summary(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 1.500 2.000 2.333 3.000 4.000
4 / 36
Comments
5 / 36
Internal data sets
data()
6 / 36
# Nile data set (including the flow of the Nile River)
c(mean(Nile),sd(Nile))
800
600
Time
7 / 36
▶ Plot a histogram of the data (bare-bones simple)
hist(Nile)
Histogram of Nile
25
20
15
Frequency
10
5
0
Nile
8 / 36
Quit R
q()
9 / 36
Introduction to Functions
10 / 36
Simple example: counting the odd numbers
oddcount(c(1,1.5,3,7,10,11,12,15))
## [1] 5
11 / 36
▶ Guess what happens with the following code:
1. Set n to x[1], and then tests that value for being odd or not
3. Then n is set to x[2], tested for being odd or not, and so on.
12 / 36
▶ The following code would also work (unless x have length 0).
13 / 36
return(k) # or simple k
## [1] 5
14 / 36
Formal or actual argument
In programming terminology,
▶ x is the formal argument (or formal parameter) of the
function oddcount().
Here, x is just a placeholder in the function defined
▶ c(1, 1.5, 3, 7, 10, 11, 12, 15) in the first function call is the
actual argument.
c(1, 1.5, 3, 7, 10, 11, 12, 15) is the value actually used in the
computation
15 / 36
Variable scope (local and global)
oddcount(c(1,5,2,100,3,99))
## [1] 4
> n
Error: object 'n' not found
16 / 36
▶ Variable created outside function are global and are available
within functions as well.
> f(5)
Error in f(5) : object 'y' not found
y <- 3
f(5)
## [1] 8
Here y is global variable.
17 / 36
Defualt arguments
18 / 36
Preview of data structures
19 / 36
Scalars (or individual numbers)
x <- 8
x
## [1] 8
Note: The [1] here signifies that the following row of numbers
begins with element 1 of a vector. R was indeed treating x as a
vector, albeit a vector with just one element.
20 / 36
Character Strings
x <- c(9,8,2022)
c(length(x),mode(x))
## [1] "3" "numeric"
z <- c("HYU","Stat","29 88")
c(length(z),mode(z))
## [1] "3" "character"
21 / 36
▶ R has various string-manipulation functions.
## [[1]]
## [1] "spatial" "statistic" "s"
22 / 36
Matrices
m <- rbind(c(9,4),c(1,5))
m
## [,1] [,2]
## [1,] 9 4
## [2,] 1 5
m %*% c(1,2)
## [,1]
## [1,] 17
## [2,] 11
23 / 36
▶ An useful feature of R is that you can extract submatrices from
a matrix, much as you extract subvectors from vectors.
m[1,] # row 1
## [1] 9 4
m[,2] # column 2
## [1] 4 5
24 / 36
Lists
25 / 36
▶ A common use of lists is to combine multiple values into a
single package that can be returned by a function.
hn <- hist(Nile)
Histogram of Nile
25
20
15
Frequency
10
5
0
Nile
26 / 36
print(hn)
## $breaks
## [1] 400 500 600 700 800 900 1000 1100 1200 1300 1400
##
## $counts
## [1] 1 0 5 20 25 19 12 11 6 1
##
## $density
## [1] 0.0001 0.0000 0.0005 0.0020 0.0025 0.0019 0.0012 0.0011 0.0006 0
##
## $mids
## [1] 450 550 650 750 850 950 1050 1150 1250 1350
##
## $xname
## [1] "Nile"
##
## $equidist
## [1] TRUE
##
## attr(,"class")
## [1] "histogram"
27 / 36
str(hn)
## List of 6
## $ breaks : int [1:11] 400 500 600 700 800 900 1000 1100 1200 1300 .
## $ counts : int [1:10] 1 0 5 20 25 19 12 11 6 1
## $ density : num [1:10] 0.0001 0 0.0005 0.002 0.0025 0.0019 0.0012 0.
## $ mids : num [1:10] 450 550 650 750 850 950 1050 1150 1250 1350
## $ xname : chr "Nile"
## $ equidist: logi TRUE
## - attr(*, "class")= chr "histogram"
Note: Here str stands for structure. This function shows the
internal structure of any R object, not just lists.
28 / 36
Data Frames
29 / 36
Classes
▶ R is an object-oriented language.
▶ Classes are a bit more abstract than the data types, and we will
skip the concept.
30 / 36
Getting Help
31 / 36
The help() Fucntion
help(rep) # or ?repeat
help(seq) # or ?seq
32 / 36
The example() Function
33 / 36
The help.search() Function
help.search("multivariate normal")
??"multivariate normal"
34 / 36
Help on the Internet
35 / 36
Reference
36 / 36