wants <- c("expm", "mvtnorm", "pracma")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
[,1] [,2]
[1,] 20 29
[2,] 26 27
[3,] 10 20
[4,] 19 12
[,1] [,2] [,3] [,4]
[1,] 20 26 10 19
[2,] 29 27 20 12
Extract the diagonal of a given matrix.
[1] 43.58333 59.33333
Create a diagonal matrix based on a vector.
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 2 0
[3,] 0 0 3
[,1] [,2]
[1,] 1 0
[2,] 0 1
[,1]
[1,] 5
[,1] [,2] [,3] [,4]
[1,] 0.75 -0.25 -0.25 -0.25
[2,] -0.25 0.75 -0.25 -0.25
[3,] -0.25 -0.25 0.75 -0.25
[4,] -0.25 -0.25 -0.25 0.75
[,1] [,2]
[1,] 1.25 7
[2,] 7.25 5
[3,] -8.75 -2
[4,] 0.25 -10
[,1] [,2]
[1,] 130.75 60
[2,] 60.00 178
[,1] [,2]
[1,] 130.75 60
[2,] 60.00 178
[,1] [,2]
[1,] 43.58333 20.00000
[2,] 20.00000 59.33333
[,1] [,2]
[1,] 43.58333 20.00000
[2,] 20.00000 59.33333
[,1] [,2]
[1,] 1.0000000 0.3932968
[2,] 0.3932968 1.0000000
[,1] [,2]
[1,] 1.0000000 0.3932968
[2,] 0.3932968 1.0000000
[,1] [,2]
[1,] 38 59
[2,] 50 55
[3,] 18 41
[4,] 36 25
[,1] [,2]
[1,] 0.5101443 0.6307329
[2,] 0.6631876 0.5872341
[3,] 0.2550722 0.4349882
[4,] 0.4846371 0.2609929
[,1] [,2]
[1,] 0.5101443 0.6307329
[2,] 0.6631876 0.5872341
[3,] 0.2550722 0.4349882
[4,] 0.4846371 0.2609929
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 7 8 0
[3,] 7 0 8
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 7 8 0
[3,] 7 0 8
[1] -3 6 -3
[,1] [,2]
[1,] 0.5 0.5
[2,] 0.5 -0.5
[,1] [,2]
[1,] 1 0
[2,] 0 1
[,1] [,2]
[1,] 1 0
[2,] 0 1
[1] -3.0 -6.4
[,1]
[1,] 5
[2,] -3
[,1]
[1,] 9.69536
[1] 9.69536
a1 a2
9.69536 18.16590
a1 a2
9.69536 18.16590
[1] 20.59126
[,1]
[1,] 20.59126
Length of difference vector
set.seed(123)
B <- matrix(sample(-20:20, 12, replace=TRUE), ncol=3)
sqrt(crossprod(B[1, ] - B[2, ]))
[,1]
[1,] 35.62303
1 2 3 4
1 0.00000 35.62303 20.80865 30.09983
2 35.62303 0.00000 24.61707 28.58321
3 20.80865 24.61707 0.00000 11.09054
4 30.09983 28.58321 11.09054 0.00000
library(mvtnorm)
N <- 100
mu <- c(-3, 2, 4)
sigma <- matrix(c(4,2,-3, 2,16,-1, -3,-1,9), byrow=TRUE, ncol=3)
Y <- round(rmvnorm(N, mean=mu, sigma=sigma))
ctr <- colMeans(Y)
S <- cov(Y)
Seig <- eigen(S)
sqrtD <- sqrt(Seig$values)
SsqrtInv <- Seig$vectors %*% diag(1/sqrtD) %*% t(Seig$vectors)
Xdot <- sweep(Y, 2, ctr, "-")
Xmt <- t(SsqrtInv %*% t(Xdot))
zapsmall(cov(Xmt))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
[1] 6.994405e-17 8.366571e-17 -2.466777e-17
y1 y2
3.192715 12.257083
[,1]
[1,] 3.192715
[,1]
[1,] 12.25708
[1] 0
[1] 74
[1] 1 2 3
[,1]
[1,] 3.192715
[,1]
[1,] 12.25708
[,1] [,2]
[1,] 9 1
[2,] 1 4
[1] 13
[1] 99
[1] 99
[1] 99
[1] 35
[1] TRUE
[1] 24
[1] TRUE
[1] 2
eigen() decomposition
$values
[1] 9.192582 3.807418
$vectors
[,1] [,2]
[1,] -0.9819564 0.1891075
[2,] -0.1891075 -0.9819564
[,1] [,2]
[1,] 1 0
[2,] 0 1
[1] 13
[1] 35
[,1] [,2]
[1,] 6.661338e-15 0
[2,] 5.773160e-15 0
We need to specify base::norm()
here because after attaching package expm
above, there is another function present with the same name.
[1] 7.931935
[1] 7.931935
[1] 7.931935
[1] 7.931935 1.502511 1.000000
[,1] [,2] [,3]
[1,] 43.58333 20.00000 -14.00000
[2,] 20.00000 59.33333 -23.33333
[3,] -14.00000 -23.33333 18.66667
[,1] [,2] [,3]
[1,] 43.58333 20.00000 -14.00000
[2,] 20.00000 59.33333 -23.33333
[3,] -14.00000 -23.33333 18.66667
[1] TRUE
[1] TRUE
[1] TRUE
We need to specify base::qr.Q()
here because after attaching package expm
above, there is another function present with the same name.
[1] TRUE
$B
[,1] [,2] [,3]
[1,] 6.372708 1.294488 -1.138518
[2,] 1.294488 7.328159 -1.988899
[3,] -1.138518 -1.988899 3.662611
$Binv
[,1] [,2] [,3]
[1,] 0.16819129 -0.01820349 0.04239706
[2,] -0.01820349 0.16201802 0.08232174
[3,] 0.04239706 0.08232174 0.33091129
$k
[1] 7
$acc
[1] 2.313559e-10
[,1] [,2] [,3]
[1,] 6.372708 1.294488 -1.138518
[2,] 1.294488 7.328159 -1.988899
[3,] -1.138518 -1.988899 3.662611
[,1] [,2] [,3]
[1,] 43.58333 20.00000 -14.00000
[2,] 20.00000 59.33333 -23.33333
[3,] -14.00000 -23.33333 18.66667
[,1] [,2] [,3]
[1,] 43.58333 20.00000 -14.00000
[2,] 20.00000 59.33333 -23.33333
[3,] -14.00000 -23.33333 18.66667
X <- matrix(c(20, 26, 10, 19, 29, 27, 20, 12, 17, 23, 27, 25), nrow=4)
ones <- rep(1, nrow(X))
P1 <- ones %*% solve(t(ones) %*% ones) %*% t(ones)
P1x <- P1 %*% X
head(P1x)
[,1] [,2] [,3]
[1,] 18.75 22 23
[2,] 18.75 22 23
[3,] 18.75 22 23
[4,] 18.75 22 23
[1] TRUE
[1] TRUE
A <- cbind(c(1, 0, 0), c(0, 1, 0))
P3 <- A %*% solve(t(A) %*% A) %*% t(A)
Px3 <- t(P3 %*% t(X))
Px3[1:3, ]
[,1] [,2] [,3]
[1,] 20 29 0
[2,] 26 27 0
[3,] 10 20 0
We need to specify base::qr.Q()
here because after attaching package expm
above, there is another function present with the same name.
qrX <- qr(X)
Q <- base::qr.Q(qrX)
R <- base::qr.R(qrX)
Xplus <- solve(t(X) %*% X) %*% t(X)
all.equal(Xplus, solve(R) %*% t(Q))
[1] TRUE
[1] TRUE
try(detach(package:MASS))
try(detach(package:expm))
try(detach(package:Matrix))
try(detach(package:pracma))
try(detach(package:mvtnorm))
R markdown - markdown - R code - all posts