Topic1 R Introduction Note
Topic1 R Introduction Note
PG Econometrics (Computation)
Toshiaki Aizawa
Acknowledgement
• This note is mainly based on the course text book.
– Florian Heiss “Using R for Introductory Econometrics” Createspace Independent Pub, 2016
∗ Free version available on the web:
∗ [Link]
R introduction
Why do we use R?
• Many alternatives:
– Stata, Matlab, Python, etc. . .
• Free software!!
– Stata is expensive.
– Campus-wide licence for Matlab is available.
• Good balance of flexibility and easy-to-use for econometrics
– Stata is easy to use for econometrics, but hard to write your own program.
1
– Matlab is the opposite.
– You can do everything with R, including data construction, regression analysis, and complicated
structural estimation.
• Many users
– Popular in data science.
– Many packages being developed (especially machine learning methods)
Don’t be scared
• Programming isn’t all that hard.
• You’re just telling the computer what to do.
• The computer will do exactly as you say.
Rstudio panes
• Console (Left bottom)
• Environment Pane (Right top)
• Browser Pane (Right bottom)
• Source Editor (Left top)
• tip: Ctrl/Cmd + Shift + number will maximize one of these panes
2
Console (Left bottom)
• Typically bottom-left
• This is where you can type in code and have it run immediately
• Or, when you run code from the Source Editor, it will show up here
• It will also show any output or errors
Console Example
• Let’s copy/paste some code in there to run
#Generate 500 heads and tails
toss <- sample(c("Heads","Tails"),size=500,replace=TRUE)
#Calculate the proportion of heads
mean(toss=="Heads")
## [1] 0.496
Helps
• The RStudio team has developed a number of “cheatsheets” for working with both R and RStudio.
• This particular cheatsheet for Base R will summarize many of the concepts in this document.
3
Getting Help
• In using R as a calculator, we have seen a number of functions:
– sqrt(), exp(), log() and sin().
• To get documentation about a function in R, simply put a question mark in front of the function name
and RStudio will display the documentation, for example:
?log
?sin
?sqrt
Installing Packages
• One of the main strengths of R as an open-source project is its package system.
• To install a package, use the [Link]() function.
– Think of this as buying a recipe book from the store, bringing it home, and putting it on your
shelf.
[Link]("ggplot2")
• Once a package is installed, it must be loaded into your current R session before being used.
– Think of this as taking the book off of the shelf and opening it up to read.
library(ggplot2)
• Once you close R, all the packages are closed and put back on the imaginary shelf.
• The next time you open R, you do not have to install the package again, but you do have to load any
packages you intend to use by invoking library().
R programming preparation
Data Types
R has a number of basic data types.
• Numeric
– Also known as Double.
– The default type when dealing with numbers.
– Examples: 1, 1.0, 42.5
• Logical
– Two possible values: TRUE and FALSE
– You can also use T and F, but this is not recommended.
– NA is also considered logical.
• Character
– Examples: “a”, “Statistics”, “1 plus 2.”
4
• A new object with the name x is created and has the value 5.
– If there was an object with this name before, its content is overwritten.
– Assigning a value to an object will not produce any output.
– The simplest shortcut for immediately displaying the result is to put the whole expression into
parentheses as in (x <- 5).
## [1] 5
xˆ2
## [1] 25
# generate objects y & z with immediate display using ():
(y <- 3)
## [1] 3
(z <- yˆx)
## [1] 243
• A list of all currently defined object names can be obtained using ls().
• The command exists("name") checks whether an object with the name ‘’name’ ’ is defined and returns
either TRUE or FALSE.
• Removing a previously defined object (for example x) from the workspace is done using rm(x).
• All objects are removed with rm(list = ls()).
– This command is often used when you start your new project.
Vectors
• For statistical calculations, we obviously need to work with data sets including many numbers instead
of scalars.
– The simplest way we can collect many numbers is to use a vector.
• To define a vector, we can collect different values using c(value1,value2,...).
– All the operators and functions used above can be used for vectors.
– Then they are applied to each of the elements separately.
## [1] 1 2 3 4 5 6
(b <- a+1)
## [1] 2 3 4 5 6 7
5
(c <- a+b)
## [1] 3 5 7 9 11 13
(d <- b*c)
## [1] 6 15 28 45 66 91
sqrt(d)
• There are also specific functions to create, manipulate and work with vectors.
# Define vector
(a <- c(7,2,6,9,4,1,3))
## [1] 7 2 6 9 4 1 3
# Basic functions:
sort(a)
## [1] 1 2 3 4 6 7 9
6
length(a)
## [1] 7
min(a)
## [1] 1
max(a)
## [1] 9
sum(a)
## [1] 32
prod(a)
## [1] 9072
## [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rep(1,20)
## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
seq(50)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
5:15
## [1] 5 6 7 8 9 10 11 12 13 14 15
seq(4,20,2)
## [1] 4 6 8 10 12 14 16 18 20
7
# Basic comparisons:
0 == 1
## [1] FALSE
0 < 1
## [1] TRUE
# Logical vectors:
( a <- c(7,2,6,9,4,1,3) )
## [1] 7 2 6 9 4 1 3
( b <- a<3 | a>=6 )
• If we want to access a single element or a subset from a vector, we can work with indices.
– They are written in square brackets [] next to the vector name.
– For example myvector[4] returns the 4th element of myvector.
– myvector[6] <- 8 changes the 6th element to take the value 8.
• For extracting more than one element, the indices can be provided as a vector themselves.
– If the vector elements have names, we can also use those as indices like in myvector["elementname"].
• Finally, logical vectors can also be used as indices.
– If a general vector vec and a logical vector b have the same length, then vec[b] returns the
elements of vec for which b has the value TRUE.
# Indices by number:
avgs[2]
## Hornsby
8
## 0.358
avgs[1:4]
# Indices by name:
avgs["Jackson"]
## Jackson
## 0.356
# Logical indices:
avgs[ avgs>=0.35 ]
Matrices
• Matrices are important tools for econometric analyses.
• Most often in applied econometrics, matrices will be generated from an existing data set.
1. matrix(vec,nrow=m) takes the numbers stored in vector vec and put them into a matrix with m rows.
2. rbind(r1,r2,...) takes the vectors r1,r2,. . . (which obviously should have the same length) as the
rows of a matrix.
3. cbind(c1,c2,...) takes the vectors c1,c2,. . . (which obviously should have the same length) as the
columns of a matrix.
9
col3 <- c(7,0)
( A <- cbind(col1, col2, col3) )
• We can also create special matrices as the examples in the output show:
1. diag(vec) (where vec is a vector) creates a diagonal matrix with the elements on the main diagonal
given in vector vec.
2. diag(n) (where n is a scalar) creates the n × n identity matrix.
• If instead of a vector or scalar, a matrix M is given as an argument to the function diag, it will return
the main diagonal of M.
10
• We can give a row and then a column index (or vectors of indices), separated by a comma:
1. A[2,3] is the element in row 2, column 3
2. A[2,c(1,2)] is a vector consisting of the elements in row 2, columns 1 and 2
3. A[2,] is a vector consisting of the elements in row 2, all columns
## [1] -4
A[,2]
## Aleph Bet
## -1 5
A[,c(1,3)]
## Alpha Gamma
## Aleph 2 7
## Bet -4 0
Matrix algebra
• Basic matrix algebra includes:
1. Matrix addition using the operator + as long as the matrices have the same dimensions.
2. The operator * does not do matrix multiplication but rather element-wise multiplication.
3. Matrix multiplication is done with the somewhat clumsy operator %*% (yes, it consists of three characters!)
as long as the dimensions of the matrices match.
4. Transpose of a matrix X: as t(X)
5. Inverse of a matrix X: as solve(X)
Element-wise multiplication
A <- matrix( c(2,-4,-1,5,7,0), nrow=2)
B <- matrix( c(2,1,0,3,-1,5), nrow=2)
A
11
Transpose
# Transpose:
(C <- t(B) )
## [,1] [,2]
## [1,] 2 1
## [2,] 0 3
## [3,] -1 5
Multiplication
# Matrix multiplication:
(D <- A %*% C )
## [,1] [,2]
## [1,] -3 34
## [2,] -8 11
Inverse
# Inverse:
solve(D)
## [,1] [,2]
## [1,] 0.0460251 -0.1422594
## [2,] 0.0334728 -0.0125523
Data Frames
Data Frames
• A data frame is an object that collects several variables.
– It is similar to a matrix.
– The most important difference to a matrix is that a data frame can contain variables of different
types (like numerical, logical, string and factor), whereas matrices can only contain numerical
values.
• Like a matrix, the rows can have names.
– Unlike a matrix, the columns always contain names which represent the variables.
– We can define a data frame from scratch by using the command [Link] or [Link]
which transform inputs of different types (like a matrix) into a data frame.
12
## [4,] 9 5 2
## [5,] 7 9 3
## [6,] 8 6 2
• The outputs of the matrix sales_mat and the data frame sales look exactly the same, but they behave
differently.
• In RStudio, the difference can be seen in the Workspace window (top right by default).
– It reports the content of sales_mat to be a ‘’6x3 double matrix’ ’ whereas the content of sales is ‘’6
obs. of 3 variables’ ’.
• We can address a single variable var of a data frame df using the matrix-like syntax df[,"var"] or by
stating df$var.
– This can be used for extracting the values of a variable but also for creating new variables.
## [1] 1 2 3 5 9 6
# Generating a new variable in the data frame:
sales$totalv1 <- sales$product1 + sales$product2+
sales$product3
# tidyverse style
sales <- sales %>% mutate(totalv1=product1+product2+product3)
# Result:
sales
13
Subset of the data
• Sometimes, we do not want to work with a whole data set but only with a subset.
• This can be easily achieved with the command subset(df,criterion),
– where criterion is a logical expression which evaluates to TRUE for the rows which are to be
selected.
14
# Check the numbers of observations and variables
dim(affairs)
## [1] 601 19
##
## 0 1
## 171 430
• Marriage ratings:
– 1 “very unhappy”
– 2 “unhappy”
– 3 “average”
– 4 “happy”
– 5 “very happy”
# Marriage ratings (share):
(rate <- table(affairs$ratemarr) )
##
## 1 2 3 4 5
## 16 66 93 194 232
# Marriage ratings (share):
[Link](rate)
##
## 1 2 3 4 5
## 0.0266223 0.1098170 0.1547421 0.3227953 0.3860233
Contingency Tables
• If we provide two arguments like table(x,y), we get the contingency table,
• i.e. the counts of each combination of outcomes for variables x and y.
# contingency table
(countstab <- table(affairs$kids,affairs$ratemarr))
##
## 1 2 3 4 5
## 0 3 8 24 40 96
## 1 13 58 69 154 136
15
##
## 1 2 3 4 5
## 0 0.004991681 0.013311148 0.039933444 0.066555740 0.159733777
## 1 0.021630616 0.096505824 0.114808652 0.256239601 0.226289517
##
## 1 2 3 4 5
## 0 0.01754386 0.04678363 0.14035088 0.23391813 0.56140351
## 1 0.03023256 0.13488372 0.16046512 0.35813953 0.31627907
##
## 1 2 3 4 5
## 0 0.1875000 0.1212121 0.2580645 0.2061856 0.4137931
## 1 0.8125000 0.8787879 0.7419355 0.7938144 0.5862069
Statistics with R
Fundamental Statistics
• The command summary is a generic command that accepts many different object types and reports appropriate
summary information.
– For numerical vectors, summary displays the mean, median, quartiles and extreme values.
• summary(df) shows the summary statistics for all variables.
– To calculate all averages within rows or columns of matrices or data frames, consider the commands
colSums, rowSums, colMeans, and rowMeans.
data(ceosal1, package='wooldridge')
# sample average:
mean(ceosal1$salary)
## [1] 1281.12
# sample median:
median(ceosal1$salary)
## [1] 1039
#standard deviation:
sd(ceosal1$salary)
## [1] 1372.345
# summary information:
summary(ceosal1$salary)
16
# correlation with ROE:
cor(ceosal1$salary, ceosal1$roe)
## [1] 0.1148417
1. How many women are in the sample, and how many report smoking during pregnancy?
2. What is the average number of cigarettes smoked per day? Is the average a good measure of the ‘’typical’ ’
woman in this case? Explain.
# Road the data set
data(bwght, package='wooldridge')
# Write your answer here
• Hint:
• Check the number of rows by dim(df) or nrow(df).
• Define smokers. smoker <- bwght$cigs>0.
• Count the number of smokers by sum(smoker).
• use mean(df$var) to calculate the mean value of variable.
• Check the distribution of the variable by table(df$var).
– Alternatively, you can visualize the distribution by hist(df$var).
Lists
Lists
• A list is a generic collection of objects.
• Unlike vectors, the components can have different types.
• Each component can (and in the cases relevant for us will) be named.
• Lists can be generated with a command like
mylist <- list( name1=component1, name2=component2, ... )
## $A
## [1] 8 12 16 20 24 28 32 36
##
## $this
## [1] "that"
##
## $idm
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
17
# Vector of names:
names(mylist)
## [1] 8 12 16 20 24 28 32 36
# or
mylist[[1]]
## [1] 8 12 16 20 24 28 32 36
R Data Files
• R has its own data file format.
• The usual extension of the file name is .RData.
– It can contain one or more objects of arbitrary type (scalars, vectors, matrices, data frames, . . . ).
– If the objects v1,v2,. . . are currently in the workspace, they can be saved to a file named [Link] by
save(v1,v2,..., file="[Link]")
load("[Link]")
• The optional arguments that can be added, separated by comma, include but are not limited to:
1. header=TRUE: The text file includes the variable names as the first line
2. sep=",": Instead of spaces or tabs, the columns are separated by a comma.
• Instead, an arbitrary other character can be given. sep=";" might be another relevant example of a separator.
• For example, some international versions of MS Excel produce these sorts of text files.
1. [Link]=number: The values in column number are used as row names instead of variables.
• RStudio provides a graphical user interface for importing text files which also allows to preview the effects of
changing the options:
– In the Workspace window, click on ‘’Import Dataset’ ’.
18
• In file [Link], the columns are separated by a comma.
• The correct command for the import would be
or
library(tidyverse)
mydata <- read_csv("[Link]")
• Since this data file does not contain any variable names, they are set to their default values V1 through V4 in
the resulting data frame mydata.
• They can be changed manually afterward, e.g. by
• Given some data in a data frame mydata, they can be exported to a text file using similar options as for
[Link] using
• You can make R read the various types of dataset. Some packages such as foreign allows you to read Stata
data set (.dta).
library(foreign)
19