2 Mathematics
2.1 Objects
Vectors are probably your most common mathematical objects, which nest scalars as a special case.
Vectors.
Make Your First Vector
Code
Apply Mathematical calculations “elementwise”
Code
See that scalars are vectors
Matrices.
Matrices are also common objects
Code
There are elementwise calculations
Code
x_mat+2
## [,1] [,2] [,3]
## x1 3 6 11
## x2 5 2 4
x_mat*2
## [,1] [,2] [,3]
## x1 2 8 18
## x2 6 0 4
x_mat^2
## [,1] [,2] [,3]
## x1 1 16 81
## x2 9 0 4
x_mat + x_mat
## [,1] [,2] [,3]
## x1 2 8 18
## x2 6 0 4
x_mat*x_mat
## [,1] [,2] [,3]
## x1 1 16 81
## x2 9 0 4
x_mat^x_mat
## [,1] [,2] [,3]
## x1 1 256 387420489
## x2 27 1 4
And you can also use matrix algebra
Code
x_mat1 <- matrix(2:7,2,3)
x_mat1
## [,1] [,2] [,3]
## [1,] 2 4 6
## [2,] 3 5 7
x_mat2 <- matrix(4:-1,2,3)
x_mat2
## [,1] [,2] [,3]
## [1,] 4 2 0
## [2,] 3 1 -1
tcrossprod(x_mat1, x_mat2) #x_mat1 %*% t(x_mat2)
## [,1] [,2]
## [1,] 16 4
## [2,] 22 7
crossprod(x_mat1, x_mat2)
## [,1] [,2] [,3]
## [1,] 17 7 -3
## [2,] 31 13 -5
## [3,] 45 19 -7
# x_mat1 * x_mat2
2.2 Functions
Functions are applied to objects
Code
# Define a function that adds two to any vector
add_2 <- function(input_vector) {
output_vector <- input_vector + 2 # new object defined locally
return(output_vector) # return new object
}
# Apply that function to a vector
x <- c(0,1,3,10,6)
add_2(x)
## [1] 2 3 5 12 8
# notice 'output_vector' is not available here
There are many many generalizations
Code
add_vec <- function(input_vector1, input_vector2) {
output_vector <- input_vector1 + input_vector2
return(output_vector)
}
add_vec(x,3)
## [1] 3 4 6 13 9
add_vec(x,x)
## [1] 0 2 6 20 12
sum_squared <- function(x1, x2) {
y <- (x1 + x2)^2
return(y)
}
sum_squared(1, 3)
## [1] 16
sum_squared(x, 2)
## [1] 4 9 25 144 64
sum_squared(x, NA)
## [1] NA NA NA NA NA
sum_squared(x, x)
## [1] 0 4 36 400 144
sum_squared(x, 2*x)
## [1] 0 9 81 900 324
Functions can take functions as arguments
Code
You can apply functions to matrices
Code
There are many possible functions you can apply
Code
There are also some useful built in functions
Code
m <- matrix(c(1:3,2*(1:3)),byrow=TRUE,ncol=3)
m
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 2 4 6
# normalize rows
m/rowSums(m)
## [,1] [,2] [,3]
## [1,] 0.1666667 0.3333333 0.5
## [2,] 0.1666667 0.3333333 0.5
# normalize columns
t(t(m)/colSums(m))
## [,1] [,2] [,3]
## [1,] 0.3333333 0.3333333 0.3333333
## [2,] 0.6666667 0.6666667 0.6666667
# de-mean rows
sweep(m,1,rowMeans(m), '-')
## [,1] [,2] [,3]
## [1,] -1 0 1
## [2,] -2 0 2
# de-mean columns
sweep(m,2,colMeans(m), '-')
## [,1] [,2] [,3]
## [1,] -0.5 -1 -1.5
## [2,] 0.5 1 1.5
Loops.
Applying the same function over and over again
Code
store complicated example
Code
recursive loop
Logic.
Basic Logic
Code
The “&” and “|” commands are logical calculations that compare vectors to the left and right.
Code
Advanced Logic.
Code
x <- 1:10
cut(x, 4)
## [1] (0.991,3.25] (0.991,3.25] (0.991,3.25] (3.25,5.5] (3.25,5.5]
## [6] (5.5,7.75] (5.5,7.75] (7.75,10] (7.75,10] (7.75,10]
## Levels: (0.991,3.25] (3.25,5.5] (5.5,7.75] (7.75,10]
split(x, cut(x, 4))
## $`(0.991,3.25]`
## [1] 1 2 3
##
## $`(3.25,5.5]`
## [1] 4 5
##
## $`(5.5,7.75]`
## [1] 6 7
##
## $`(7.75,10]`
## [1] 8 9 10
Code
2.3 Arrays
Arrays are generalization of matrices. They are often used in spatial econometrics, and are a very efficient way to store numeric data with the same dimensions.
Code
Apply extends to arrays
Code
Outer products yield arrays
Code
x <- c(1,2,3)
x_mat1 <- outer(x, x) # x %o% x
x_mat1
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 2 4 6
## [3,] 3 6 9
is.array(x_mat) # Matrices are arrays
## [1] TRUE
x_mat2 <- matrix(6:1,2,3)
outer(x_mat2, x)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 6 4 2
## [2,] 5 3 1
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 12 8 4
## [2,] 10 6 2
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 18 12 6
## [2,] 15 9 3
# outer(x_mat2, matrix(x))
# outer(x_mat2, t(x))
# outer(x_mat1, x_mat2)