3 Mathematics
3.2 Vectors
## [1] 0 1 3 10 6
## [1] 1
## [1] 2 3 5 12 8
## [1] 0 2 6 20 12
## [1] 0 1 9 100 36
## [1] 0 2 6 20 12
## [1] 0 1 9 100 36
## [1] 1.0000e+00 1.0000e+00 2.7000e+01 1.0000e+10 4.6656e+04
## [1] 1
## [1] 1 2 3 4 5 6 7
## [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
3.3 Functions
Function of a vector
## [1] 2 3 5 12 8
## [1] 2 3 5 12 8
## [1] 3 4 6 13 9
Function for two vectors
## [1] 16
## [1] 4 9 25 144 64
## [1] NA NA NA NA NA
## [1] 0 4 36 400 144
## [1] 0 9 81 900 324
Applying the same function over and over again
## [1] 2.718282 7.389056 20.085537
## [1] 2.718282 7.389056 20.085537
## [1] 3.718282 9.389056 23.085537
recursive functions
## [1] 1 4 25
r_fun <- function(n){
x <- rep(1,n)
for(i in 2:length(x) ){
x[i] <- (x[i-1]+1)^2
}
return(x)
}
r_fun(5)
## [1] 1 4 25 676 458329
Functions can take functions as arguments
## [1] 2
## [1] 2
3.4 Matrices
## [,1] [,2] [,3]
## x1 1 4 9
## x2 3 0 2
## [1] 3 0 2
## x1 x2
## 4 0
## x2
## 0
## [,1] [,2] [,3]
## x1 3 6 11
## x2 5 2 4
## [,1] [,2] [,3]
## x1 2 8 18
## x2 6 0 4
## [,1] [,2] [,3]
## x1 1 16 81
## x2 9 0 4
## [,1] [,2] [,3]
## x1 2 8 18
## x2 6 0 4
## [,1] [,2] [,3]
## x1 1 16 81
## x2 9 0 4
## [,1] [,2] [,3]
## x1 1 256 387420489
## x2 27 1 4
y <- apply(x_mat, 1, sum)^2 ## Apply function to each row
## ?apply #checks the function details
y - sum_squared(x, x) ## tests if there are any differences
## [1] 192 -39 -2304
Many Other Functions
## [,1] [,2] [,3]
## [1,] 2 4 6
## [2,] 3 5 7
## [,1] [,2] [,3]
## [1,] 4 2 0
## [2,] 3 1 -1
## [,1] [,2] [,3]
## [1,] 8 8 0
## [2,] 9 5 -7
## [,1] [,2]
## [1,] 16 4
## [2,] 22 7
## [,1] [,2] [,3]
## [1,] 17 7 -3
## [2,] 31 13 -5
## [3,] 45 19 -7
Example Calculations
## Return Y-value with minimum absolute difference from 3
abs_diff_y <- abs( y - 3 )
abs_diff_y ## is this the luckiest number?
## x1 x2
## 193 22
## [1] 22
## x2
## 2
## x2
## 25
3.5 Arrays
Generalization of matrices used in spatial econometrics
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 7 9 11
## [2,] 8 10 12
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 13 15 17
## [2,] 14 16 18
##
## , , 4
##
## [,1] [,2] [,3]
## [1,] 19 21 23
## [2,] 20 22 24
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 3 5
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 7 9 11
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 13 15 17
##
## , , 4
##
## [,1] [,2] [,3]
## [1,] 19 21 23
## , , 1
##
## [,1]
## [1,] 1
## [2,] 2
##
## , , 2
##
## [,1]
## [1,] 7
## [2,] 8
##
## , , 3
##
## [,1]
## [1,] 13
## [2,] 14
##
## , , 4
##
## [,1]
## [1,] 19
## [2,] 20
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
## [1] 1 7 13 19
## [1] 1 3 5
## [1] 1 2
## [1] 1
Apply extends to arrays
## [1] 12 13
## [1] 10.5 12.5 14.5
## [1] 3.5 9.5 15.5 21.5
## [,1] [,2] [,3]
## [1,] 10 12 14
## [2,] 11 13 15
Outer Products yield arrays
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 2 4 6
## [3,] 3 6 9
## [1] TRUE
## , , 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