Theory 1. R Basics
Theory 1. R Basics
Econometrics - R Basics
Catalin Starica
26th September 2017
Econometrics
First R session
Installation
References (1/2)
I http://cran.r-project.org/manuals.html
I See An Introduction to R
http://cran.r-project.org/doc/manuals/R-intro.html
I See the FAQ(s)
http://cran.r-project.org/doc/FAQ/R-FAQ.html
I R: Getting started
http://wiki.math.yorku.ca/index.php/R:_Getting_started
I Introduction to R (Vincent Zoonekynd),
http://zoonek2.free.fr/UNIX/48_R/all.html
I How to make simple graphs
http://www.harding.edu/fmccown/r/
Econometrics
First R session
Documentation
References (2/2)
I In French:
I R pour les débutants (Emmanuel Paradis)
http://cran.r-project.org/doc/contrib/Paradis-
rdebuts_fr.pdf
I Introduction au système R, (Yves Brostaux)
http://cran.r-project.org/doc/contrib/Brostaux-
Introduction-au-R.zip
I In English:
I simpleR - Using R for Introductory Statistics (John Verzani)
http://cran.r-project.org/doc/contrib/Verzani-
SimpleR.pdf
I Book : The R software. Fundamentals of Programming and
Statistical Analysis (Lafaye de Micheaux et al., Springer, 2011)
Econometrics
First R session
First R session
I How to open R
Click on the R icon and everything is ready to work
I The R console
Here you can execute a command instantly, i.e. you type the
formula, you press enter and you receive the output
I Example: (everything after the symbol # is not executed, it is
just a way to add comments):
> 2*(-4.5) # Note that the decimal separator is a point (.)
[1] -9
> 3^2 # 3 to the power of 2
[1] 9
> sqrt(4) # square root of 4
[1] 2
> log(1) # natural logarithm (base e) of 1
[1] 0
Econometrics
First R session
First R session
I How to close R
Type the command q() ou File-> Quit Session... Choose to
do not save a workspace image
I How to set a Working Directory (WD) :
Follow File->Set working directory and set a working directory.
Check: Type getwd() to show the actual working directory
> getwd()
[1] "M:/TravailR"
Econometrics
First R session
First R session
I R script:
You can create a R script to define a sequence of commands
I Create a new script: File -> New script.
I Save a script: File -> Save as... select a directory, and name
the script (e.g. script1.R)
I Save an existing script: File -> Save
I Open an existing script: File -> Open file and choose
I Execute a line of command, right click and Execute, or you can
even push Ctrl + Enter (cmd + Enter on a Mac)
Econometrics
First R session
Define, list and show objects
Exercise 1
I Launch R and define a working directory
I Create a new script with the name script1.R
I Type all the commands shown in the next slide
I Run the obtained script line by line
I Save the script, and then close R
I Open the script: all the codes can be reused
Econometrics
First R session
Define, list and show objects
Logical operators
a==b TRUE, if a is equal to b, FALSE otherwise
!(a==b) TRUE, if a is different from b, FALSE otherwise
a!=b TRUE, if a is different from b, FALSE otherwise
a<b TRUE, if a is lower than b, FALSE otherwise
a>b TRUE, if a is greater than b, FALSE otherwise
a<=b TRUE, if a is lower or equal to b, FALSE otherwise
a>=b TRUE, if a is greater or equal to b, FALSE otherwise
a|b TRUE, if a, b, or both are TRUE, FALSE otherwise
a&b TRUE, if a and b are TRUE, FALSE otherwise
Econometrics
First R session
Logical operators
Example
> x=3
> y=4
> z = c(-1,2)
> x>3
[1] FALSE
> x>3 & y==4
[1] FALSE
> z>0
[1] FALSE TRUE
Econometrics
First R session
Installer une extension
I Vectors (vector ):
Formed by a set of elements (numbers, characters)
3 6 5
I Datasets (data frame):
Formed by one or more than one vectors of the same length
The rule: columns ↔ variables / rows ↔ observations
sex weight height
m 60 170
f 57 169
f 51 172
f 55 174
Econometrics
First R session
Most important objects
> c(1,7,0,2)
[1] 1 7 0 2
> 2:7
[1] 2 3 4 5 6 7
> rep(8, times=3)
[1] 8 8 8
Econometrics
Data
How to read data
Example
> data1
sex weight height
h 60 170
f 57 169
f 51 172
f 55 174
> data1$weight # variable weight of data1
[1] 60 57 51 55 50 50 48 72 52 64 53 72 61
[35] 52 57 53 55 66 65 75 50 53 55 55 72 75
> data1[2,3] # element in the second row, third column
[1] 169
> data1[2,] # second row of data1
sex weight height
f 57 169
> data1[,2] # second column of data1, same as data1$weight
[1] 60 57 51 55 50 50 48 72 52 64 53 72 61
[35] 52 57 53 55 66 65 75 50 53 55 55 72 75
Econometrics
Data
How to extract information from vectors
> ibm
AdjClose
2007-01-03 89.79
2007-02-01 84.42
2007-03-01 85.62
> ibm[3]
2007-03-01
85.62
> ibm_vect = as.vector(ibm)
> ibm_vect
[1] 89.79 84.42 85.62 92.84 97.21 95.98 100.90
[15] 106.14 111.27 119.80 109.72 118.46 113.12 110.55
Econometrics
Data
How to extract information from vectors
> data1$weight
[1] 60 57 51 55 50 50 48 72 52 64 53 72 61 78...
[35] ...52 57 53 55 66 65 75 50 53 55 55 72 75 73
> length(data1$weight)
[1] 66
> rev(data1$weight)
[1] 74 47 65 85 86 82 73 85 67 60 72 73 47 62...
[35] ...70 77 62 70 72 70 73 53 80 74 50 49 62 74
> sort(data1$weight)
[1] 47 47 48 49 50 50 50 50 51 51 51 52 52 53...
[35] ...66 67 68 70 70 70 71 71 72 72 72 72 72 73
> unique(sort(data1$weight))
[1] 47 48 49 50 51 52 53 55 57 60 61 62 64 65...
Econometrics
Data
How to extract information from vectors
> data1
sex weight height
1 m 60 170
2 f 57 169
...
> names(data1)
[1] "sex" "weight" "height"
> dim(data1) # 66 rows, 3 columns
[1] 66 3
> nrow(data1) # 66 rows (observations)
[1] 66
> ncol(data1) # 3 columns (variables)
[1] 3
> str(data1)
’data.frame’: 66 obs. of 3 variables:
$ sex : Factor w/ 2 levels "f","m": 2 1 1 1 1 1 1 2 1 2 ...
$ weight : int 60 57 51 55 50 50 48 72 52 64 ...
$ height: int 170 169 172 174 168 161 162 189 160 175 ...
...(it follows in the next slide)
Econometrics
Data
How to extract information from vectors
> data2
sex civil_status number_childs
f c 0
h c 0
...
f c 1
> plot(data1$weight,data1$height,
+ pch = 1, cex = 1.5, col = 2, main = "Weight and height",
+ xlab = "weight", ylab = "height")
Poids et taille
200
●
190
● ●
●
●
●
● ●
●
● ● ● ●
● ●
180
● ● ● ●
●
● ● ● ● ● ● ●
●
taille
● ● ● ●
● ● ●
●
● ● ● ●
●
170
● ● ●
● ●
● ● ● ●
●
● ●
● ●
● ●
● ●
160
● ●
● ●
150
50 60 70 80
poids
Econometrics
Plots
50 60 70 80 90
poids
Econometrics
Plots
4
> t1 = table(data2$civil_status)
> t1
3
c m v
4 3 1 2
+ of civil status",
+ xlab = "Civil status")
0
c m v
Etat civil
Econometrics
Plots
> t2 = table(data2$civil_status,
c m v
+ data2$sex)
> t2
f
f h
c 2 2
Sexe
m 2 1
v 1 0
h
+ ylab="Sex")
Econometrics
Basic of programming