R3: Graphics and Visualization



Vector and Matrix Geometry

split.screen(c(1,2)) # Divide the graphics device into two parts horizontally

[1] 1 2

par(bg="white", pty="s") # set background color (ensures proper erasure)

screen(2)

par(pty="s") # and square plot regions

1. Vector Representation

a = c(2, 1)

screen(1)

plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')

# point representation of a vector

abline(h=0,v=0,col="grey") # add coordinate axes

title("Point Representation of (2, 1)")

screen(2)

plot(0, 0, xlim=c(-3,3), ylim=c(-3,3), type="n", xlab='x', ylab='y')

# Empty plotting frame

arrows(0, 0, a[1], a[2]) # ray representation of a vector

abline(h=0,v=0,col="grey")

title("Ray Representation of (2, 1)")

[pic]

2. Data as a collection of vectors

X= read.table("")

X

V1 V2

1 57 93

2 58 110

: : :

20 69 155

mean(X)

V1 V2

62.85 123.60

sd(X)

V1 V2

3.297128 15.571229

screen(2)

screen(1)

plot(X, pch=16, xlab="Height", ylab="Weight")

title("Raw Data (Women's Heights and Weights)")

Xd = scale(X, scale=F) # mean-centered data (deviations)

Xd

V1 V2

1 -5.85 -30.6

2 -4.85 -13.6

: : :

20 6.15 31.4

apply(Xd, 2, mean) # Xd is matrix, not data frame

V1 V2

-1.421302e-15 5.685209e-15

screen(2)

plot(Xd, pch=16, xlab="Height", ylab="Weight")

title("Mean-Centered Data (Women's Heights and Weights)")

abline(h=0,v=0,col="grey")

[pic]

apply(Xd, 2, sd)

V1 V2

3.297128 15.571229

Xs = scale(X)# standardized data

Xs

V1 V2

1 -1.77427146 -1.96516286

2 -1.47097719 -0.87340572

: : :

20 1.86525974 2.01653967

apply(Xs, 2, mean)

V1 V2

-4.308077e-16 3.579493e-16

apply(Xs, 2, sd)

V1 V2

1 1

screen(2)

plot(Xs, pch=16, xlab="Height", ylab="Weight")

title("Standardized Data (Women's Heights and Weights)")

abline(h=0,v=0,col="grey")

[pic]

3. Scalar multiplication

a

[1] 2 1

screen(2)

screen(1)

plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')

arrows(0, 0, a[1], a[2])

abline(h=0,v=0,col="grey")

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

title("Representation of (2, 1)")

r= .6

ra = r*a

ra

[1] 1.2 0.6

screen(2)

plot(ra[1],ra[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')

arrows(0, 0, ra[1], ra[2])

abline(h=0,v=0,col="grey")

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

title("Point Representation of 0.6*(2, 1)")

[pic]

screen(2)

screen(1)

plot(Xs, pch=16, xlab="Height", ylab="Weight", xlim=c(-2,2), ylim=c(-2,2))

title("Standardized Data")

abline(h=0,v=0,col="grey")

screen(2)

plot(r*Xs, pch=16, xlab="Height", ylab="Weight", xlim=c(-2,2), ylim=c(-2,2))

title("Rescaled Standardized Data")

abline(h=0,v=0,col="grey")

[pic]

4. Vector multiplication: projection

w1 = matrix(rep(1,2),nc=1)

w1

[,1]

[1,] 1

[2,] 1

w1^2

[,1]

[1,] 1

[2,] 1

lw1 = sqrt(sum(w1^2))# length of w1, ||w1||

w1 = w1/lw1# unit length

w1

[,1]

[1,] 0.7071068

[2,] 0.7071068

sqrt(sum(w1^2))

[1] 1

a

[1] 2 1

aw1 = a %*% w1

aw1

[,1]

[1,] 2.121320

screen(2)

screen(1)

plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')

abline(h=0,v=0,col="grey")

arrows(0, 0, w1[1], w1[2])

abline(0, w1[2]/w1[1])

points(aw1*w1[1], aw1*w1[2], pch=16, col="red")

segments(a[1],a[2],aw1*w1[1],aw1*w1[2],lty=3)

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

w2 = matrix(c(-1, 1),nc=1)

w2

[,1]

[1,] -1

[2,] 1

w2^2

[,1]

[1,] 1

[2,] 1

lw2 = sqrt(sum(w2^2))

w2 = w2/lw2# unit length

w2

[,1]

[1,] -0.7071068

[2,] 0.7071068

sqrt(sum(w2^2))

[1] 1

aw2 = a %*% w2

aw2

[,1]

[1,] -0.7071068

screen(2)

plot(a[1],a[2], xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='x', ylab='y')

abline(h=0,v=0,col="grey")

arrows(0, 0, w2[1], w2[2])

abline(0, w2[2]/w2[1])

points(aw2*w2[1], aw2*w2[2], pch=16, col="green")

segments(a[1],a[2],aw2*w2[1],aw2*w2[2],lty=3)

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

[pic]

Xs

V1 V2

1 -1.77427146 -1.96516286

2 -1.47097719 -0.87340572

: : :

20 1.86525974 2.01653967

Z1 = Xs %*% w1

screen(2)

screen(1)

plot(Xs, xlim=c(-2,2), ylim=c(-2,2), pch=16, xlab='x', ylab='y')

abline(h=0,v=0,col="grey")

arrows(0, 0, w1[1], w1[2])

abline(0, w1[2]/w1[1])

points(Z1*w1[1], Z1*w1[2], pch=16, col="red")

segments(Xs[,1],Xs[,2],Z1*w1[1],Z1*w1[2],lty=3)

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

Z2 = Xs %*% w2

screen(2)

plot(Xs, xlim=c(-2,2), ylim=c(-2,2), pch=16, xlab='x', ylab='y')

abline(h=0,v=0,col="grey")

arrows(0, 0, w2[1], w2[2])

abline(0, w2[2]/w2[1])

points(Z2*w2[1], Z2*w2[2], pch=16, col="green")

segments(Xs[,1],Xs[,2],Z2*w2[1],Z2*w2[2],lty=3)

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

[pic]

5. Orthogonal vectors

t(w1) %*% w2

[,1]

[1,] 2.236167e-17

screen(2)

screen(1)

plot(0, 0, xlim=c(-3,3), ylim=c(-3,3), type="n", xlab='x', ylab='y')

arrows(0, 0, w1[1], w1[2])

arrows(0, 0, w2[1], w2[2])

abline(h=0,v=0,col="grey")

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

[pic]

6. Matrix Multiplication: Rotation

screen(1)

plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')

abline(h=0,v=0,col="grey")

arrows(0, 0, w1[1], w1[2])

abline(0, w1[2]/w1[1])

arrows(0, 0, w2[1], w2[2])

abline(0, w2[2]/w2[1])

points(Z1*w1[1], Z1*w1[2], col="red")

segments(Xs[,1],Xs[,2],Z1*w1[1],Z1*w1[2],lty=3)

points(Z2*w2[1], Z2*w2[2], col="green")

segments(Xs[,1],Xs[,2],Z2*w2[1],Z2*w2[2],lty=3)

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

screen(2)

W = cbind(w1, w2)

Z = Xs %*% W

plot(Z, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')

abline(h=0,v=0)

arrows(0,0,1,0)

arrows(0,0,0,1)

points(Z1, rep(0,20), col="red")

segments(Z1,rep(0,20),Z1,Z2,lty=3)

points(rep(0,20), Z2, col="green")

segments(rep(0,20),Z2,Z1,Z2,lty=3)

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

[pic]

screen(1)

plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')

abline(h=0,v=0,col="grey")

arrows(0, 0, w1[1], w1[2])

arrows(0, 0, w2[1], w2[2])

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

screen(2)

plot(Z, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')

abline(h=0,v=0,col="grey")

arrows(0,0,1,0)

arrows(0,0,0,1)

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

[pic]

plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')

abline(h=0,v=0,col="grey")

arrows(0, 0, w1[1], w1[2])

arrows(0, 0, w2[1], w2[2])

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

ZWt = Z %*% t(W)

points(ZWt, col="red")

[pic]

r1 = c(cos(pi/3),-sin(pi/3))# rotate axes pi/3

r2 = c(sin(pi/3),cos(pi/3))

R = cbind(r1,r2)

screen(2)

screen(1)

plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')

abline(h=0,v=0,col="grey")

arrows(0, 0, r1[1], r1[2])

arrows(0, 0, r2[1], r2[2])

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

screen(2)

plot(Xs %*% R, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')

abline(h=0,v=0,col="grey")

arrows(0,0,1,0)

arrows(0,0,0,1)

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

[pic]

7. Matrix Multiplication: Streching and Shrinking

sd(Z)

[1] 1.3664086 0.3645923

Dinv = diag(1/sd(Z))

Dinv

[,1] [,2]

[1,] 0.7318455 0.00000

[2,] 0.0000000 2.74279

Zs = Z %*% Dinv

screen(1)

plot(Z, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')

abline(h=0,v=0,col="grey")

arrows(0,0,1,0)

arrows(0,0,0,1)

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

screen(2)

plot(Zs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='Z1', ylab='Z2')

abline(h=0,v=0,col="grey")

arrows(0,0,1,0)

arrows(0,0,0,1)

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

[pic]

8. Singular Value Decomposition

D = diag(sd(Z))

D %*% Dinv

[,1] [,2]

[1,] 1 0

[2,] 0 1

screen(2)

screen(1)

plot(Xs, xlim=c(-3,3), ylim=c(-3,3), pch=16, xlab='X1', ylab='X2')

abline(h=0,v=0,col="grey")

arrows(0, 0, w1[1], w1[2])

arrows(0, 0, w2[1], w2[2])

symbols(rep(0,3),rep(0,3),circ=1:3,fg="grey",add=T,inches=F)

X2 = Zs %*% D %*% t(W)

D;Zs;t(W) #print out the values

points(X2, col="red")

Xsvd = svd(Xs)

Xsvd

$d

[1] 5.956037 1.589221

$u

[,1] [,2]

[1,] -0.4439494504 0.084935083

[2,] -0.2783275263 -0.265883024

: : :

[20,] 0.4608511785 -0.067310382

$v

[,1] [,2]

[1,] 0.7071068 0.7071068

[2,] 0.7071068 -0.7071068

X2p = Xsvd$u %*% diag(Xsvd$d) %*% t(Xsvd$v)

# X2p identical with X2

# X2 is such that Zs has sd=1 and D = diag(sd(Z))

# X2p is such that Xsvd$u is orthonormal (i.e., col length=1) and the singular # values are constant multiples of D = diag(sd(Z))

# (i.e., diag(Xsvd$d) = constant multiples * D )

# of course Xsvd$u %*% diag(Xsvd$d) %*% t(Xsvd$v) = Zs %*% D %*% t(W)

U=Xsvd$u

screen(2)

plot(U, pch=16, xlab='U1', ylab='U2')

abline(h=0,v=0,col="grey")

cov(U)

[,1] [,2]

[1,] 5.263158e-02 3.801841e-19

[2,] 3.801841e-19 5.263158e-02

t(U) %*% U

[,1] [,2]

[1,] 1.000000e+00 4.948366e-18

[2,] 4.948366e-18 1.000000e+00

[pic]

9. Matrix Computation of Covariance

Xd# Start with mean-centered matrix

V1 V2

1 -5.85 -30.6

2 -4.85 -13.6

: : :

20 6.15 31.4

cov(Xd)

V1 V2

V1 10.87105 44.51579

V2 44.51579 242.46316

n = nrow(Xd)

S = 1/(n-1) * t(Xd) %*% Xd

S

V1 V2

V1 10.87105 44.51579

V2 44.51579 242.46316

10. Matrix Determinant

det(S)

[1] 654.1742

screen(2)

screen(1)

plot(0, 0, xlim=c(0, 290), ylim=c(0,290), type="n", xlab='S1', ylab='S2')

abline(h=0,v=0,col="grey")

polygon(c(0,S[1,1],S[1,1]+S[1,2],S[1,2]),c(0,S[2,1],S[2,1]+S[2,2],S[2,2]),

col="red")

arrows(0, 0, S[1,1], S[2,1])

arrows(0, 0, S[1,2], S[2,2])

arrows(S[1,1], S[2,1], S[1,1]+S[1,2], S[2,1]+S[2,2])

arrows(S[1,2], S[2,2], S[1,1]+S[1,2], S[2,1]+S[2,2])

Xd.svd = svd(Xd)

d = Xd.svd$d

d

[1] 69.020117 7.040842

D = diag(d)

D

[,1] [,2]

[1,] 69.02012 0.000000

[2,] 0.00000 7.040842

SD = 1/(n-1) * t(D) %*% D

SD

[,1] [,2]

[1,] 250.7251 0.000000

[2,] 0.0000 2.609130

det(SD)

[1] 654.1742

prod(d^2/(n-1))

[1] 654.1742

screen(2)

plot(0, 0, xlim=c(0, 290), ylim=c(0,290), type="n", xlab='S1', ylab='S2')

abline(h=0,v=0,col="grey")

polygon(c(0,SD[1,1],SD[1,1]+SD[1,2],SD[1,2]),

c(0,SD[2,1],SD[2,1]+SD[2,2],SD[2,2]),col="red")

arrows(0, 0, SD[1,1], SD[2,1])

arrows(0, 0, SD[1,2], SD[2,2])

arrows(SD[1,1], SD[2,1], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])

arrows(SD[1,2], SD[2,2], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])

[pic]

screen(2)

screen(1)

plot(0, 0, xlim=c(0, 55), ylim=c(0,290), type="n", xlab='S1', ylab='S2')

abline(h=0,v=0,col="grey")

polygon(c(0,S[1,1],S[1,1]+S[1,2],S[1,2]),c(0,S[2,1],S[2,1]+S[2,2],S[2,2]),

col="red")

arrows(0, 0, S[1,1], S[2,1])

arrows(0, 0, S[1,2], S[2,2])

arrows(S[1,1], S[2,1], S[1,1]+S[1,2], S[2,1]+S[2,2])

arrows(S[1,2], S[2,2], S[1,1]+S[1,2], S[2,1]+S[2,2])

screen(2)

plot(0, 0, xlim=c(0, 255), ylim=c(0,3), type="n", xlab='S1', ylab='S2')

abline(h=0,v=0,col="grey")

polygon(c(0,SD[1,1],SD[1,1]+SD[1,2],SD[1,2]),

c(0,SD[2,1],SD[2,1]+SD[2,2],SD[2,2]),col="red")

arrows(0, 0, SD[1,1], SD[2,1])

arrows(0, 0, SD[1,2], SD[2,2])

arrows(SD[1,1], SD[2,1], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])

arrows(SD[1,2], SD[2,2], SD[1,1]+SD[1,2], SD[2,1]+SD[2,2])

# R codes for Vectors and Matrices

split.screen(c(1,2)) # Divide the graphics device into two parts horizontally

par(bg="white", pty="s") # set background color (ensures proper erasure)

screen(2)

par(pty="s") # and square plot regions

# Vector representation

a ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches