X

Basic R commands – 1.2 – permutations and matrix functions

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Basic Commands and Statistics with R - 1.2 - permutations
# path: ~/ownCloud/STA_Statistics/Basic_Statistics/
# file_name: statistic_basics3.R
# files_used:

#install.packages( 'gtools' )
library( gtools )
#install.packages( "combinat" )
library( combinat )

#install.packages( "magrittr" )
library( "magrittr" )

# - - - - - - - - - - - - - - - - 
#

x <- 6

factorial( x )
# [1] 720

for( i in 1:x ) print( factorial( i ) )
# [1] 1
# [1] 2
# [1] 6
# [1] 24
# [1] 120
# [1] 720

# - - - - - - - - - - - - - - - - 
#

x <- c( "a", "b", "c" )

combinat::permn( x )

# [[1]]
# [1] "a" "b" "c"
# [[2]]
# [1] "a" "c" "b"
# [[3]]
# [1] "c" "a" "b"
# [[4]]
# [1] "c" "b" "a"
# [[5]]
# [1] "b" "c" "a"
# [[6]]
# [1] "b" "a" "c"

# - - - - - - - - - - - - - - - - 
# string conversion using combinat::

x <- "ABCDEFG"
x <- strsplit( x, "" )[[1]]
y <- combinat::permn( x )
y

x <- LETTERS[1:5]
y <- combinat::permn( x )

paste( y[[1]], collapse = "" )
for( i in 1:length( y ) ) print( paste( y[[i]], collapse = "" ) )

# - - - - - - - - - - - - - - - - 
# using gtools:: for permutations

gtools::permutations( 5, 5, x )
# [,1] [,2] [,3] [,4] [,5]
# [1,] "A"  "C"  "I"  "M"  "P" 
# [2,] "A"  "C"  "I"  "P"  "M" 
# [3,] "A"  "C"  "M"  "I"  "P" 
# [4,] "A"  "C"  "M"  "P"  "I" 
# [5,] "A"  "C"  "P"  "I"  "M"
# ...

gtools::permutations( 5, 5, x, repeats.allowed = TRUE )
# [,1] [,2] [,3] [,4] [,5]
# [1,] "A"  "A"  "A"  "A"  "A" 
# [2,] "A"  "A"  "A"  "A"  "C" 
# [3,] "A"  "A"  "A"  "A"  "I" 
# [4,] "A"  "A"  "A"  "A"  "M" 
# [5,] "A"  "A"  "A"  "A"  "P" 
# [6,] "A"  "A"  "A"  "C"  "A" 
# [7,] "A"  "A"  "A"  "C"  "C" 
# [8,] "A"  "A"  "A"  "C"  "I" 
# [9,] "A"  "A"  "A"  "C"  "M" 
# [10,] "A"  "A"  "A"  "C"  "P" 
# ...

# - - - - - - - - - - - - - - - - 
# using gtools:: for combinations

gtools::combinations( 4, 2, x ) 

gtools::combinations( 5, 2, x )

gtools::combinations( 5, 3, x )

gtools::combinations( 4, 2, x, repeats.allowed = TRUE )

gtools::combinations( 2, 5, x, repeats.allowed = TRUE )

# Martin Stoppacher                                                             #
# office@martinstoppacher.com                                                   #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#################################################################################
# matrix algebra

m1 <- matrix( c( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) , 3, 3 )
m1

m2 <- matrix( rep( 2, 9 ), 3, 3 )
m2

#       [,1] [,2] [,3]
# [1,]    1    4    7
# [2,]    2    5    8
# [3,]    3    6    9

m1 * m1  # element multipliction

#       [,1] [,2] [,3]
# [1,]    1   16   49
# [2,]    4   25   64
# [3,]    9   36   81

m1 * m2

#       [,1] [,2] [,3]
# [1,]    2    8   14
# [2,]    4   10   16
# [3,]    6   12   18


# Matrix Multiplication
m1 %*% m1

#       [,1] [,2] [,3]
# [1,]   30   66  102
# [2,]   36   81  126
# [3,]   42   96  150

# Transpose
t( m1 )

#       [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    4    5    6
# [3,]    7    8    9

# Outer Product

m1[ 1 , ]
m1[ , 1 ] 
m1[ 1 , ] %o%  m1[ , 1 ] 

m1 %o% m1

# , , 1, 1
# 
#       [,1] [,2] [,3]
# [1,]    1    4    7
# [2,]    2    5    8
# [3,]    3    6    9
# 
# , , 2, 1
# 
#       [,1] [,2] [,3]
# [1,]    2    8   14
# [2,]    4   10   16
# [3,]    6   12   18
# 
# , , 3, 1
# 
#       [,1] [,2] [,3]
# [1,]    3   12   21
# [2,]    6   15   24
# [3,]    9   18   27
# 
# , , 1, 2
# 
#       [,1] [,2] [,3]
# [1,]    4   16   28
# [2,]    8   20   32
# [3,]   12   24   36
# 
# , , 2, 2
# 
#       [,1] [,2] [,3]
# [1,]    5   20   35
# [2,]   10   25   40
# [3,]   15   30   45
# 
# , , 3, 2
# 
#       [,1] [,2] [,3]
# [1,]    6   24   42
# [2,]   12   30   48
# [3,]   18   36   54
# 
# , , 1, 3
# 
#       [,1] [,2] [,3]
# [1,]    7   28   49
# [2,]   14   35   56
# [3,]   21   42   63
# 
# , , 2, 3
# 
#       [,1] [,2] [,3]
# [1,]    8   32   56
# [2,]   16   40   64
# [3,]   24   48   72
# 
# , , 3, 3
# 
#       [,1] [,2] [,3]
# [1,]    9   36   63
# [2,]   18   45   72
# [3,]   27   54   81

# kroenecker product

m1 %x% m1

#       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
# [1,]    1    4    7    4   16   28    7   28   49
# [2,]    2    5    8    8   20   32   14   35   56
# [3,]    3    6    9   12   24   36   21   42   63
# [4,]    2    8   14    5   20   35    8   32   56
# [5,]    4   10   16   10   25   40   16   40   64
# [6,]    6   12   18   15   30   45   24   48   72
# [7,]    3   12   21    6   24   42    9   36   63
# [8,]    6   15   24   12   30   48   18   45   72
# [9,]    9   18   27   18   36   54   27   54   81

m1 %x% m2

#       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
# [1,]    2    2    2    8    8    8   14   14   14
# [2,]    2    2    2    8    8    8   14   14   14
# [3,]    2    2    2    8    8    8   14   14   14
# [4,]    4    4    4   10   10   10   16   16   16
# [5,]    4    4    4   10   10   10   16   16   16
# [6,]    4    4    4   10   10   10   16   16   16
# [7,]    6    6    6   12   12   12   18   18   18
# [8,]    6    6    6   12   12   12   18   18   18
# [9,]    6    6    6   12   12   12   18   18   18

# cross Product 

m1[ , 1 ]
m1[ , 2 ] 

crossprod( m1, m2[ , 1 ]  )

#       [,1]
# [1,]   32
# [2,]   77
# [3,]  122

crossprod( m1 )

#      [,1] [,2] [,3]
# [1,]   14   32   50
# [2,]   32   77  122
# [3,]   50  122  194

crossprod( m1, m2 )

#       [,1] [,2] [,3]
# [1,]   12   12   12
# [2,]   30   30   30
# [3,]   48   48   48

# Diagonal Matrix with elements of x in the diagonal

diag( nrow = 3 )

# [,1] [,2] [,3]
# [1,]    1    0    0
# [2,]    0    1    0
# [3,]    0    0    1

diag( 3,  nrow = 3 )

# [,1] [,2] [,3]
# [1,]    3    0    0
# [2,]    0    3    0
# [3,]    0    0    3

# returns a vector of the principal diagonal elements
m1
diag( m1 )

# [,1] [,2] [,3]
# [1,]    1    0    0
# [2,]    0    2    0
# [3,]    0    0    3

# k * k identity matrix for m1[ , 1 ]
diag( m1[ , 1 ] )

# [,1] [,2] [,3]
# [1,]    1    0    0
# [2,]    0    2    0
# [3,]    0    0   

# simple combinations

cbind( m1, m2 )
rbind( m1, m2 )

# means and sums

rowMeans( m1 )
rowSums( m1 )

colMeans( m1 )
colSums( m1 )

# eigenvalues, eigenvectors

eigen( m1 )
e <- eigen( m1 )
e$values
e$vectors

# eigen() decomposition
# $values
# [1]  1.611684e+01 -1.116844e+00 -5.700691e-16
#
# $vectors
# [,1]       [,2]       [,3]
# [1,] -0.4645473 -0.8829060  0.4082483
# [2,] -0.5707955 -0.2395204 -0.8164966
# [3,] -0.6770438  0.4038651  0.4082483

# single value decomposition

svd( m1 )

# $d    singular values of m1
# [1] 1.684810e+01 1.068370e+00 5.543107e-16
#
# $u    left singular vectors of m1
# [,1]        [,2]       [,3]
# [1,] -0.4796712  0.77669099  0.4082483
# [2,] -0.5723678  0.07568647 -0.8164966
# [3,] -0.6650644 -0.62531805  0.4082483
#
# $v    right singular vectors of m1
# [,1]       [,2]       [,3]
# [1,] -0.2148372 -0.8872307  0.4082483
# [2,] -0.5205874 -0.2496440 -0.8164966
# [3,] -0.8263375  0.3879428  0.4082483


# QR decomposition 

qr( m1 )

# $qr
# [,1]      [,2]          [,3]
# [1,] -3.7416574 -8.552360 -1.336306e+01
# [2,]  0.5345225  1.963961  3.927922e+00
# [3,]  0.8017837  0.988693  1.776357e-15
#
# $rank
# [1] 2
#
# $qraux  
# [1] 1.267261e+00 1.149954e+00 1.776357e-15
#
# $pivot
# [1] 1 2 3
#
# attr(,"class")
# [1] "qr"


# inverse 

solve( m1 )

# Error in solve.default(m1) : 
#   Lapack routine dgesv: system is exactly singular: U[3,3] = 0

m3 <- matrix( c( 2, 6, 1, 4 ) , 2, 2 )
m3

# [,1] [,2]
# [1,]    2    1
# [2,]    6    4

sm3 <- solve( m3 )

# [,1] [,2]
# [1,]    2 -0.5
# [2,]   -3  1.0

m3 %*% sm3

# [,1] [,2]
# [1,]    1    0
# [2,]    0    1

 

Martin Stoppacher: