3 Mathematical Objects


3.1 Scalars and Vectors

x0 <- 1 ## Your first scalar
x0 ## Print the scalar
## [1] 1
(x0+1)^2 ## Perform and print a simple calculation
## [1] 4
x0 + NA ## often used for missing values
## [1] NA
x0*2
## [1] 2
x <- c(0,1,3,10,6) ## Your First Vector
x ## Print the vector
## [1]  0  1  3 10  6
x[2] ## Print the 2nd Element; 1
## [1] 1
x+2 ## Print simple calculation; 2,3,5,8,12
## [1]  2  3  5 12  8
x*2
## [1]  0  2  6 20 12
(x+x)^2 ## Another simple calculation with two vectors
## [1]   0   4  36 400 144

In R, you use multiple functions on different types of data objects. Moreover, ``typically solve complex problems by decomposing them into simple functions, not simple objects.’’ (H. Wickham)

3.2 Functions of Scalars and Vectors

Define function sum_squared

sum_squared <- function(x1, x2) {
    y <- (x1 + x2)^2
    return(y)
} 
sum_squared(1, 3) ## 16
## [1] 16
sum_squared(x, 2) ## 0,4,9,36,144,400
## [1]   4   9  25 144  64
sum_squared(x, NA) ## NA,NA,NA,NA,NA
## [1] NA NA NA NA NA
sum_squared(x, x) ## 0,4,36,144,400
## [1]   0   4  36 400 144
sum_squared(x, 2*x)
## [1]   0   9  81 900 324

Random variables are vectors created by functions

## random standard-normal
rnorm(10) 
##  [1] -1.659114612 -0.893319072  1.028185724  0.175974438  0.805598106
##  [6] -0.286323007 -0.220622089  0.610450703  0.001307709 -1.270002728
rnorm(10)
##  [1] -0.4343422 -0.6215811 -0.2599112 -0.7581629 -0.5302672  1.7420162
##  [7] -0.5462935 -2.2278339  0.3593115  0.1203228
## random uniform
x2 <- runif(1000)
head(x2)
## [1] 0.2434022 0.8730505 0.3551018 0.3874562 0.1506225 0.4580846
length(x2)
## [1] 1000

3.3 Functions of Functions

Functions can take functions as arguments

fun_of_rv <- function(f){
    y <- f( runif(1e3) )
    return(y)
}
fun_of_rv(mean)
## [1] 0.502619
fun_of_rv(mean)
## [1] 0.5020712
fun_of_rv(sum)
## [1] 499.6072
fun_of_rv <- function(f=mean, n=20){
    y <- f( runif(n) )
    return(y)    
}
fun_of_rv()
## [1] 0.4750519

Very useful for applying a function over and over again

## sapply(1:3, f) is equivalent to c(f(1), f(2), f(3)).
## mapply takes multiple vectors
mapply(sum, 1:3, runif(3) )
## [1] 1.153112 2.289946 3.784061

3.4 Matrices and Matrix-Functions

x_mat <- cbind(x,x)
x_mat ## Print full matrix
##       x  x
## [1,]  0  0
## [2,]  1  1
## [3,]  3  3
## [4,] 10 10
## [5,]  6  6
x_mat[2,] ## Print Row 2 Elements
## x x 
## 1 1
x_mat[,2] ## Print Column 2 Elements
## [1]  0  1  3 10  6
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] 0 0 0 0 0

Many Other Functions

#col1 <- x_mat[,1]
#col1
#col2 <- x_mat[,2]
#col2
x_mat * x_mat
##        x   x
## [1,]   0   0
## [2,]   1   1
## [3,]   9   9
## [4,] 100 100
## [5,]  36  36
crossprod(x_mat)
##     x   x
## x 146 146
## x 146 146
tcrossprod(x_mat) ##x_mat %*% t(x_mat)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    0    0    0    0
## [2,]    0    2    6   20   12
## [3,]    0    6   18   60   36
## [4,]    0   20   60  200  120
## [5,]    0   12   36  120   72
outer(x,x) ##x %o% x
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    0    0    0    0
## [2,]    0    1    3   10    6
## [3,]    0    3    9   30   18
## [4,]    0   10   30  100   60
## [5,]    0    6   18   60   36

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?
## [1]   3   1  33 397 141
min(abs_diff_y)
## [1] 1
which.min(abs_diff_y)
## [1] 2
y[ which.min(abs_diff_y) ]
## [1] 4

3.5 Arrays and Array Functions

Generalization of matrices used in spatial econometrics

a <- array(data = 1:24, dim = c(2, 3, 4))
a
## , , 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
a[1, , , drop = FALSE]  # Row 1
## , , 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
a[, 1, , drop = FALSE]  # Column 1
## , , 1
## 
##      [,1]
## [1,]    1
## [2,]    2
## 
## , , 2
## 
##      [,1]
## [1,]    7
## [2,]    8
## 
## , , 3
## 
##      [,1]
## [1,]   13
## [2,]   14
## 
## , , 4
## 
##      [,1]
## [1,]   19
## [2,]   20
a[, , 1, drop = FALSE]  # Layer 1
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
a[ 1, 1,  ]  # Row 1, column 1
## [1]  1  7 13 19
a[ 1,  , 1]  # Row 1, "layer" 1
## [1] 1 3 5
a[  , 1, 1]  # Column 1, "layer" 1
## [1] 1 2
a[1 , 1, 1]  # Row 1, column 1, "layer" 1
## [1] 1

Apply extends to arrays

apply(a, 1, mean)    # Row means
## [1] 12 13
apply(a, 2, mean)    # Column means
## [1] 10.5 12.5 14.5
apply(a, 3, mean)    # "Layer" means
## [1]  3.5  9.5 15.5 21.5
apply(a, 1:2, mean)  # Row/Column combination 
##      [,1] [,2] [,3]
## [1,]   10   12   14
## [2,]   11   13   15

3.6 Other Commom Types of Data

l1 <- 1:10 ## cardinal numbers
l2 <- factor(c(1,2,3), ordered=T) ## ordinal numbers
l2 <- factor(c(1,2,3), ordered=F) ## "indicators", "names", 'etc'
l3 <- 'hello world'  ## character strings
l4 <- list(l1, l2, list(l3, 'way too late')) ## lists

## data.frames: your most common data type
    ## matrix of different data-types
    ## well-ordered lists
l5 <- data.frame(x=l1, y=l1)