-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEIGEN
More file actions
144 lines (106 loc) · 3.3 KB
/
EIGEN
File metadata and controls
144 lines (106 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
## Function for arranging ggplots. use png(); arrange(p1, p2, ncol=1); dev.off() to save.
require(grid)
vp.layout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y)
arrange_ggplot2 <- function(..., nrow=NULL, ncol=NULL, as.table=FALSE) {
dots <- list(...)
n <- length(dots)
if(is.null(nrow) & is.null(ncol)) { nrow = floor(n/2) ; ncol = ceiling(n/nrow)}
if(is.null(nrow)) { nrow = ceiling(n/ncol)}
if(is.null(ncol)) { ncol = ceiling(n/nrow)}
## NOTE see n2mfrow in grDevices for possible alternative
grid.newpage()
pushViewport(viewport(layout=grid.layout(nrow,ncol) ) )
ii.p <- 1
for(ii.row in seq(1, nrow)){
ii.table.row <- ii.row
if(as.table) {ii.table.row <- nrow - ii.table.row + 1}
for(ii.col in seq(1, ncol)){
ii.table <- ii.p
if(ii.p > n) break
print(dots[[ii.table]], vp=vp.layout(ii.table.row, ii.col))
ii.p <- ii.p + 1
}
}
}
# Generating Random Vectors from a bivariate standard
# normal, and a made-up covariance matrix:
require(ggplot2)
v1 <- rnorm(1e5)
v2 <- rnorm(1e5)
X <- rbind(v1,v2)
p1 <- qplot(v1,v2) +
geom_point(col='firebrick') +
geom_point(alpha = 0.01) +
xlim(c(-20,20)) + ylim(c(-20,20))+
geom_hline(yintercept=0, col='orange') +
geom_vline(xintercept=0, col='orange')
C <- matrix(c(20,8,8,20), nrow=2)
eival <- eigen(C)$values
Lambda <- matrix(c(sqrt(eival[1]),0,0,sqrt(eival[2])), nrow=2)
Lambda
Stretch <- Lambda%*%X
p2 <- qplot(Stretch[1,],Stretch[2,]) +
geom_point(col='firebrick') +
geom_point(alpha = 0.01) +
xlim(c(-20,20)) + ylim(c(-20,20))+
geom_hline(yintercept=0, col='orange') +
geom_vline(xintercept=0, col='orange')
eivec <- eigen(C)$vectors
Rot <- eivec%*%Stretch
p3 <- qplot(Rot[1,],Rot[2,]) +
geom_point(col='firebrick') +
geom_point(alpha = 0.01) +
xlim(c(-20,20)) + ylim(c(-20,20))+
geom_hline(yintercept=0, col= 'orange') +
geom_vline(xintercept=0, col='orange')
mu <- c(3,-5)
Rand <- Rot + mu
p4 <- qplot(Rand[1,],Rand[2,]) +
geom_point(col='firebrick') +
geom_point(alpha = 0.01) +
xlim(c(-20,20)) + ylim(c(-23,20)) +
geom_hline(yintercept=0, col='orange') +
geom_vline(xintercept=0, col='orange')
arrange_ggplot2(p1,p2,p3,p4, ncol=1)
# And the final Random Vector is:
V1 <- Rand[1,]
V2 <- Rand[2,]
V <- rbind(V1,V2)
# ALTERNATIVE PLOTTING:
# Generating Random Vectors from a bivariate standard
# normal, and a made-up covariance matrix:
v1 <- rnorm(1e5)
v2 <- rnorm(1e5)
X <- rbind(v1,v2)
C <- matrix(c(20,8,8,20), nrow=2)
eival <- eigen(C)$values
Lambda <- matrix(c(sqrt(eival[1]),0,0,sqrt(eival[2])), nrow=2)
Lambda
Stretch <- Lambda%*%X
eivec <- eigen(C)$vectors
Rot <- eivec%*%Stretch
mu <- c(3,-7)
Rand <- Rot + mu
library(RColorBrewer)
par(mfrow=c(4,1))
smoothScatter(v1,v2,
colramp =
colorRampPalette(c("white","red","purple4")),
xlim=c(-20,20),ylim=c(-20,20))
abline(h=0); abline(v=0)
smoothScatter(Stretch[1,],Stretch[2,],
colramp =
colorRampPalette(c("white","red","purple4")))
abline(h=0); abline(v=0)
smoothScatter(Rot[1,],Rot[2,],
colramp =
colorRampPalette(c("white","red","purple4")))
abline(h=0); abline(v=0)
smoothScatter(Rand[1,],Rand[2,],
colramp =
colorRampPalette(c("white","red","purple4")))
abline(h=0); abline(v=0)
# And the final Random Vector is:
V1 <- Rand[1,]
V2 <- Rand[2,]
V <- rbind(V1,V2)