c("expm", "mvtnorm", "pracma")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
4
N <- 2
Q <- matrix(c(20, 26, 10, 19, 29, 27, 20, 12), nrow=N, ncol=Q)) (X <-
[,1] [,2]
[1,] 20 29
[2,] 26 27
[3,] 10 20
[4,] 19 12
t(X)
[,1] [,2] [,3] [,4]
[1,] 20 26 10 19
[2,] 29 27 20 12
Extract the diagonal of a given matrix.
diag(cov(X))
[1] 43.58333 59.33333
Create a diagonal matrix based on a vector.
diag(1:3)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 2 0
[3,] 0 0 3
diag(2)
[,1] [,2]
[1,] 1 0
[2,] 0 1
diag(5, nrow=1)
[,1]
[1,] 5
diag(N) - matrix(rep(1/N, N^2), nrow=N)) (Xc <-
[,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
Xc %*% X) (Xdot <-
[,1] [,2]
[1,] 1.25 7
[2,] 7.25 5
[3,] -8.75 -2
[4,] 0.25 -10
t(Xdot) %*% Xdot) (SSP <-
[,1] [,2]
[1,] 130.75 60
[2,] 60.00 178
crossprod(Xdot)
[,1] [,2]
[1,] 130.75 60
[2,] 60.00 178
1/(N-1)) * SSP (
[,1] [,2]
[1,] 43.58333 20.00000
[2,] 20.00000 59.33333
cov(X)) (S <-
[,1] [,2]
[1,] 43.58333 20.00000
[2,] 20.00000 59.33333
diag(1/sqrt(diag(S)))
Ds <-%*% S %*% Ds Ds
[,1] [,2]
[1,] 1.0000000 0.3932968
[2,] 0.3932968 1.0000000
cov2cor(S)
[,1] [,2]
[1,] 1.0000000 0.3932968
[2,] 0.3932968 1.0000000
2
b <- c(-2, 1)
a <-sweep(b*X, 2, a, "+")
[,1] [,2]
[1,] 38 59
[2,] 50 55
[3,] 18 41
[4,] 36 25
sqrt(colSums(X^2))
colLens <-sweep(X, 2, colLens, "/")
[,1] [,2]
[1,] 0.5101443 0.6307329
[2,] 0.6631876 0.5872341
[3,] 0.2550722 0.4349882
[4,] 0.4846371 0.2609929
%*% diag(1/colLens) X
[,1] [,2]
[1,] 0.5101443 0.6307329
[2,] 0.6631876 0.5872341
[3,] 0.2550722 0.4349882
[4,] 0.4846371 0.2609929
cbind(c(1,1,1), c(0,2,0), c(0,0,2))
B <-%*% B %*% B B
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 7 8 0
[3,] 7 0 8
library(expm)
%^% 3 B
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 7 8 0
[3,] 7 0 8
c(1, 2, 3)
a <- c(4, 5, 6)
b <-library(pracma)
cross(a, b)
[1] -3 6 -3
matrix(c(1, 1, 1, -1), nrow=2)
Y <- solve(Y)) (Yinv <-
[,1] [,2]
[1,] 0.5 0.5
[2,] 0.5 -0.5
%*% Yinv Y
[,1] [,2]
[1,] 1 0
[2,] 0 1
library(MASS)
ginv(X)
gInv <-zapsmall(gInv %*% X)
[,1] [,2]
[1,] 1 0
[2,] 0 1
matrix(c(9, 1, -5, 0), nrow=2)
A <- c(5, -3)
b <- solve(A, b)) (x <-
[1] -3.0 -6.4
%*% x A
[,1]
[1,] 5
[2,] -3
c(3, 4, 1, 8, 2)
a1 <-sqrt(crossprod(a1))
[,1]
[1,] 9.69536
sqrt(sum(a1^2))
[1] 9.69536
c(6, 9, 10, 8, 7)
a2 <- cbind(a1, a2)
A <-sqrt(diag(crossprod(A)))
a1 a2
9.69536 18.16590
sqrt(colSums(A^2))
a1 a2
9.69536 18.16590
norm(A, type="F")
[1] 20.59126
sqrt(crossprod(c(A)))
[,1]
[1,] 20.59126
Length of difference vector
set.seed(123)
matrix(sample(-20:20, 12, replace=TRUE), ncol=3)
B <-sqrt(crossprod(B[1, ] - B[2, ]))
[,1]
[1,] 42.73172
dist(B, diag=TRUE, upper=TRUE)
1 2 3 4
1 0.00000 42.73172 24.55606 25.39685
2 42.73172 0.00000 33.12099 35.22783
3 24.55606 33.12099 0.00000 32.64966
4 25.39685 35.22783 32.64966 0.00000
library(mvtnorm)
100
N <- c(-3, 2, 4)
mu <- matrix(c(4,2,-3, 2,16,-1, -3,-1,9), byrow=TRUE, ncol=3)
sigma <- round(rmvnorm(N, mean=mu, sigma=sigma)) Y <-
colMeans(Y)
ctr <- cov(Y)
S <- eigen(S)
Seig <- sqrt(Seig$values)
sqrtD <- Seig$vectors %*% diag(1/sqrtD) %*% t(Seig$vectors)
SsqrtInv <-
sweep(Y, 2, ctr, "-")
Xdot <- t(SsqrtInv %*% t(Xdot))
Xmt <-zapsmall(cov(Xmt))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
colMeans(Xmt)
[1] -9.145462e-17 -1.058181e-17 -5.776629e-17
c(1, 2, 3)
ideal <- Y[1, ]
y1 <- Y[2, ]
y2 <- rbind(y1, y2) mat <-
mahalanobis(mat, ideal, S)
y1 y2
5.619871 13.170225
solve(S)
Sinv <-t(y1-ideal) %*% Sinv %*% (y1-ideal)
[,1]
[1,] 5.619871
t(y2-ideal) %*% Sinv %*% (y2-ideal)
[,1]
[1,] 13.17022
mahalanobis(Y, ideal, S)
mDist <-min(mDist)
[1] 1.139962
which.min(mDist)) (idxMin <-
[1] 66
Y[idxMin, ]
[1] -1 1 5
t(SsqrtInv %*% (ideal - ctr))
idealM <-crossprod(Xmt[1, ] - t(idealM))
[,1]
[1,] 5.619871
crossprod(Xmt[2, ] - t(idealM))
[,1]
[1,] 13.17022
matrix(c(9, 1, 1, 4), nrow=2)) (A <-
[,1] [,2]
[1,] 9 1
[2,] 1 4
sum(diag(A))
[1] 13
sum(diag(t(A) %*% A))
[1] 99
sum(diag(A %*% t(A)))
[1] 99
sum(A^2)
[1] 99
det(A)
[1] 35
matrix(c(-3, 4, -1, 7), nrow=2)
B <-all.equal(det(A %*% B), det(A) * det(B))
[1] TRUE
det(diag(1:4))
[1] 24
solve(A)
Ainv <-all.equal(1/det(A), det(Ainv))
[1] TRUE
qr(A)
qrA <-$rank qrA
[1] 2
eigen(A)) (eigA <-
$values
[1] 9.192582 3.807418
$vectors
[,1] [,2]
[1,] -0.9819564 0.1891075
[2,] -0.1891075 -0.9819564
zapsmall(eigA$vectors %*% t(eigA$vectors))
[,1] [,2]
[1,] 1 0
[2,] 0 1
sum(eigA$values)
[1] 13
prod(eigA$values)
[1] 35
library(MASS)
Null(X)
Xnull <-t(X) %*% Xnull
[,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.
matrix(c(20, 26, 10, 19, 29, 27, 20, 12, 17, 23, 27, 25), nrow=4)
X <-kappa(X, exact=TRUE)
[1] 7.931935
solve(t(X) %*% X) %*% t(X)
Xplus <-::norm(X, type="2") * base::norm(Xplus, type="2") base
[1] 7.931935
eigen(t(X) %*% X)$values
evX <-sqrt(max(evX) / min(evX[evX >= .Machine$double.eps]))
[1] 7.931935
sqrt(evX / min(evX[evX >= .Machine$double.eps]))
[1] 7.931935 1.502511 1.000000
matrix(c(20, 26, 10, 19, 29, 27, 20, 12, 17, 23, 27, 25), nrow=4)
X <- cov(X)) (S <-
[,1] [,2] [,3]
[1,] 43.58333 20.00000 -14.00000
[2,] 20.00000 59.33333 -23.33333
[3,] -14.00000 -23.33333 18.66667
eigen(S)
eigS <- eigS$vectors
G <- diag(eigS$values)
D <-%*% D %*% t(G) G
[,1] [,2] [,3]
[1,] 43.58333 20.00000 -14.00000
[2,] 20.00000 59.33333 -23.33333
[3,] -14.00000 -23.33333 18.66667
svd(X)
svdX <-all.equal(X, svdX$u %*% diag(svdX$d) %*% t(svdX$v))
[1] TRUE
all.equal(sqrt(eigen(t(X) %*% X)$values), svdX$d)
[1] TRUE
chol(S)
R <-all.equal(S, t(R) %*% R)
[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.
qr(X)
qrX <- base::qr.Q(qrX)
Q <- base::qr.R(qrX)
R <-all.equal(X, Q %*% R)
[1] TRUE
library(expm)
sqrtm(S)
$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
diag(sqrt(eigS$values))
sqrtD <- G %*% sqrtD %*% t(G)) (A <-
[,1] [,2] [,3]
[1,] 6.372708 1.294488 -1.138518
[2,] 1.294488 7.328159 -1.988899
[3,] -1.138518 -1.988899 3.662611
%*% A A
[,1] [,2] [,3]
[1,] 43.58333 20.00000 -14.00000
[2,] 20.00000 59.33333 -23.33333
[3,] -14.00000 -23.33333 18.66667
eigS$vectors %*% sqrt(diag(eigS$values))
N <-%*% t(N) N
[,1] [,2] [,3]
[1,] 43.58333 20.00000 -14.00000
[2,] 20.00000 59.33333 -23.33333
[3,] -14.00000 -23.33333 18.66667
matrix(c(20, 26, 10, 19, 29, 27, 20, 12, 17, 23, 27, 25), nrow=4)
X <- rep(1, nrow(X))
ones <- ones %*% solve(t(ones) %*% ones) %*% t(ones)
P1 <- P1 %*% X
P1x <-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
ones / sqrt(crossprod(ones))
a <- a %*% t(a)
P2 <-all.equal(P1, P2)
[1] TRUE
diag(nrow(X)) - P1
IP1 <- IP1 %*% X
IP1x <-all.equal(IP1x, sweep(X, 2, colMeans(X), "-"))
[1] TRUE
cbind(c(1, 0, 0), c(0, 1, 0))
A <- A %*% solve(t(A) %*% A) %*% t(A)
P3 <- t(P3 %*% t(X))
Px3 <-1:3, ] Px3[
[,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.
qr(X)
qrX <- base::qr.Q(qrX)
Q <- base::qr.R(qrX)
R <- solve(t(X) %*% X) %*% t(X)
Xplus <-all.equal(Xplus, solve(R) %*% t(Q))
[1] TRUE
all.equal(X %*% Xplus, tcrossprod(Q))
[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