PLOT CORRELATION
How is correlation calculated R?
Correlation is a statistical tool to get a comparison between two and more stocks, commodities prices, etc.
seq(as.POSIXct("2003-04-23"), by = "month", length = 12)
[1] "2003-04-23 IST" "2003-05-23 IST"
[3] "2003-06-23 IST" "2003-07-23 IST"
[5] "2003-08-23 IST" "2003-09-23 IST"
[7] "2003-10-23 IST" "2003-11-23 IST"
[9] "2003-12-23 IST" "2004-01-23 IST"
[11] "2004-02-23 IST" "2004-03-23 IST"
> seq(as.POSIXct("2003-04-23"), by = "year", length = 12)
[1] "2003-04-23 IST" "2004-04-23 IST"
[3] "2005-04-23 IST" "2006-04-23 IST"
[5] "2007-04-23 IST" "2008-04-23 IST"
[7] "2009-04-23 IST" "2010-04-23 IST"
[9] "2011-04-23 IST" "2012-04-23 IST"
[11] "2013-04-23 IST" "2014-04-23 IST"
> seq(as.POSIXct("2003-04-23"), by = "weekly", length = 12)
Error in seq.POSIXt(as.POSIXct("2003-04-23"), by = "weekly", length = 12) :
invalid string for 'by'
> seq(as.POSIXct("2003-04-23"), by = "week", length = 12)
[1] "2003-04-23 IST" "2003-04-30 IST"
[3] "2003-05-07 IST" "2003-05-14 IST"
[5] "2003-05-21 IST" "2003-05-28 IST"
[7] "2003-06-04 IST" "2003-06-11 IST"
[9] "2003-06-18 IST" "2003-06-25 IST"
[11] "2003-07-02 IST" "2003-07-09 IST"
> seq(as.POSIXct("2003-04-23"), by = "daily", length = 12)
Error in seq.POSIXt(as.POSIXct("2003-04-23"), by = "daily", length = 12) :
invalid string for 'by'
> seq(as.POSIXct("2003-04-23"), by = "day", length = 12)
[1] "2003-04-23 IST" "2003-04-24 IST"
[3] "2003-04-25 IST" "2003-04-26 IST"
[5] "2003-04-27 IST" "2003-04-28 IST"
[7] "2003-04-29 IST" "2003-04-30 IST"
[9] "2003-05-01 IST" "2003-05-02 IST"
[11] "2003-05-03 IST" "2003-05-04 IST"
> rep(1:4, 4)
[1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
> rep(1:4, 5)
[1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
> rep(1:5, c(3, 2, 2, 2))
Error in rep(1:5, c(3, 2, 2, 2)) : invalid 'times' argument
>
>
> rep(1:4, c(3, 2, 2, 2))
[1] 1 1 1 2 2 3 3 4 4
>
> rep(1:4, 1:4)
[1] 1 2 2 3 3 3 4 4 4 4
>
> x <- rnorm(10)
>
> y <- runif(10, 4, 7)
>
> plot(y)
> plot(x)
> x <- 1:8
> dim(x) <- c(2, 4)
>
> x
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
> x <- matrix(1:8, 2, 4)
> x
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
> x <- matrix(1:8, 2, 4, byrow = TRUE)
> x
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
> cbind(c(1, 2, 3), c(4, 5, 6))
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
>
> rbind(c(1, 2, 3), c(4, 5, 6))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
>
> x * x^2
[,1] [,2] [,3] [,4]
[1,] 1 8 27 64
[2,] 125 216 343 512
> max(x)
[1] 8
>
> x <- matrix(1:16, ncol = 4)
> x
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
> t(x)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
> x*t(x)
[,1] [,2] [,3] [,4]
[1,] 1 10 27 52
[2,] 10 36 70 112
[3,] 27 70 121 180
[4,] 52 112 180 256
> plot(x)
> plot(t(x))
> y <- 7:10
> y
[1] 7 8 9 10
> x * y
[,1] [,2] [,3] [,4]
[1,] 7 35 63 91
[2,] 16 48 80 112
[3,] 27 63 99 135
[4,] 40 80 120 160
> x %*% t(x)
[,1] [,2] [,3] [,4]
[1,] 276 304 332 360
[2,] 304 336 368 400
[3,] 332 368 404 440
[4,] 360 400 440 480
> chol(x)
Error in chol.default(x) :
the leading minor of order 2 is not positive definite
> diag(x)
[1] 1 6 11 16
> qr(x)
$qr
[,1] [,2] [,3]
[1,] -5.4772256 -12.7801930 -2.008316e+01
[2,] 0.3651484 -3.2659863 -6.531973e+00
[3,] 0.5477226 -0.3781696 1.601186e-15
[4,] 0.7302967 -0.9124744 -5.547002e-01
[,4]
[1,] -2.738613e+01
[2,] -9.797959e+00
[3,] 2.217027e-15
[4,] -1.478018e-15
$rank
[1] 2
$qraux
[1] 1.182574e+00 1.156135e+00 1.832050e+00
[4] 1.478018e-15
$pivot
[1] 1 2 3 4
attr(,"class")
[1] "qr"
> solve(x)
Error in solve.default(x) :
Lapack routine dgesv: system is exactly singular: U[3,3] = 0
> svd(x)
$d
[1] 3.862266e+01 2.071323e+00 2.076990e-15
[4] 4.119458e-16
$u
[,1] [,2] [,3] [,4]
[1,] -0.4284124 -0.7186535 0.43803202 0.3288281
[2,] -0.4743725 -0.2738078 -0.82913672 -0.1119477
[3,] -0.5203326 0.1710379 0.34417739 -0.7625890
[4,] -0.5662928 0.6158835 0.04692732 0.5457086
$v
[,1] [,2] [,3] [,4]
[1,] -0.1347221 0.82574206 0.5322301 -0.1293488
[2,] -0.3407577 0.42881720 -0.6132292 0.5691660
[3,] -0.5467933 0.03189234 -0.3702319 -0.7502855
[4,] -0.7528288 -0.36503251 0.4512310 0.3104683
> var(x)
[,1] [,2] [,3] [,4]
[1,] 1.666667 1.666667 1.666667 1.666667
[2,] 1.666667 1.666667 1.666667 1.666667
[3,] 1.666667 1.666667 1.666667 1.666667
[4,] 1.666667 1.666667 1.666667 1.666667
> x <- 1:8
> dim(x) <- c(2, 2, 2)
> x
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
>
> dim(iris3)
[1] 50 4 3
> test <- iris + 2 * iris
Warning messages:
1: In Ops.factor(left, right) : ‘*’ not meaningful for factors
2: In Ops.factor(left, right) : ‘+’ not meaningful for factors
>
> test
Sepal.Length Sepal.Width Petal.Length
1 15.3 10.5 4.2
2 14.7 9.0 4.2
3 14.1 9.6 3.9
4 13.8 9.3 4.5
5 15.0 10.8 4.2
6 16.2 11.7 5.1
7 13.8 10.2 4.2
8 15.0 10.2 4.5
9 13.2 8.7 4.2
10 14.7 9.3 4.5
11 16.2 11.1 4.5
12 14.4 10.2 4.8
13 14.4 9.0 4.2
14 12.9 9.0 3.3
15 17.4 12.0 3.6
16 17.1 13.2 4.5
17 16.2 11.7 3.9
18 15.3 10.5 4.2
19 17.1 11.4 5.1
20 15.3 11.4 4.5
21 16.2 10.2 5.1
22 15.3 11.1 4.5
23 13.8 10.8 3.0
24 15.3 9.9 5.1
25 14.4 10.2 5.7
26 15.0 9.0 4.8
27 15.0 10.2 4.8
28 15.6 10.5 4.5
29 15.6 10.2 4.2
30 14.1 9.6 4.8
31 14.4 9.3 4.8
32 16.2 10.2 4.5
33 15.6 12.3 4.5
34 16.5 12.6 4.2
35 14.7 9.3 4.5
36 15.0 9.6 3.6
37 16.5 10.5 3.9
38 14.7 10.8 4.2
39 13.2 9.0 3.9
40 15.3 10.2 4.5
41 15.0 10.5 3.9
42 13.5 6.9 3.9
43 13.2 9.6 3.9
44 15.0 10.5 4.8
45 15.3 11.4 5.7
46 14.4 9.0 4.2
47 15.3 11.4 4.8
48 13.8 9.6 4.2
49 15.9 11.1 4.5
50 15.0 9.9 4.2
51 21.0 9.6 14.1
52 19.2 9.6 13.5
53 20.7 9.3 14.7
54 16.5 6.9 12.0
55 19.5 8.4 13.8
56 17.1 8.4 13.5
57 18.9 9.9 14.1
58 14.7 7.2 9.9
59 19.8 8.7 13.8
60 15.6 8.1 11.7
61 15.0 6.0 10.5
62 17.7 9.0 12.6
63 18.0 6.6 12.0
64 18.3 8.7 14.1
65 16.8 8.7 10.8
66 20.1 9.3 13.2
67 16.8 9.0 13.5
68 17.4 8.1 12.3
69 18.6 6.6 13.5
70 16.8 7.5 11.7
71 17.7 9.6 14.4
72 18.3 8.4 12.0
73 18.9 7.5 14.7
74 18.3 8.4 14.1
75 19.2 8.7 12.9
76 19.8 9.0 13.2
77 20.4 8.4 14.4
78 20.1 9.0 15.0
79 18.0 8.7 13.5
80 17.1 7.8 10.5
81 16.5 7.2 11.4
82 16.5 7.2 11.1
83 17.4 8.1 11.7
84 18.0 8.1 15.3
85 16.2 9.0 13.5
86 18.0 10.2 13.5
87 20.1 9.3 14.1
88 18.9 6.9 13.2
89 16.8 9.0 12.3
90 16.5 7.5 12.0
91 16.5 7.8 13.2
92 18.3 9.0 13.8
93 17.4 7.8 12.0
94 15.0 6.9 9.9
95 16.8 8.1 12.6
96 17.1 9.0 12.6
97 17.1 8.7 12.6
98 18.6 8.7 12.9
99 15.3 7.5 9.0
100 17.1 8.4 12.3
101 18.9 9.9 18.0
102 17.4 8.1 15.3
103 21.3 9.0 17.7
104 18.9 8.7 16.8
105 19.5 9.0 17.4
106 22.8 9.0 19.8
107 14.7 7.5 13.5
108 21.9 8.7 18.9
109 20.1 7.5 17.4
110 21.6 10.8 18.3
111 19.5 9.6 15.3
112 19.2 8.1 15.9
113 20.4 9.0 16.5
114 17.1 7.5 15.0
115 17.4 8.4 15.3
116 19.2 9.6 15.9
117 19.5 9.0 16.5
118 23.1 11.4 20.1
119 23.1 7.8 20.7
120 18.0 6.6 15.0
121 20.7 9.6 17.1
122 16.8 8.4 14.7
123 23.1 8.4 20.1
124 18.9 8.1 14.7
125 20.1 9.9 17.1
126 21.6 9.6 18.0
127 18.6 8.4 14.4
128 18.3 9.0 14.7
129 19.2 8.4 16.8
130 21.6 9.0 17.4
131 22.2 8.4 18.3
132 23.7 11.4 19.2
133 19.2 8.4 16.8
134 18.9 8.4 15.3
135 18.3 7.8 16.8
136 23.1 9.0 18.3
137 18.9 10.2 16.8
138 19.2 9.3 16.5
139 18.0 9.0 14.4
140 20.7 9.3 16.2
141 20.1 9.3 16.8
142 20.7 9.3 15.3
143 17.4 8.1 15.3
144 20.4 9.6 17.7
145 20.1 9.9 17.1
146 20.1 9.0 15.6
147 18.9 7.5 15.0
148 19.5 9.0 15.6
149 18.6 10.2 16.2
150 17.7 9.0 15.3
Petal.Width Species
1 0.6 NA
2 0.6 NA
3 0.6 NA
4 0.6 NA
5 0.6 NA
6 1.2 NA
7 0.9 NA
8 0.6 NA
9 0.6 NA
10 0.3 NA
11 0.6 NA
12 0.6 NA
13 0.3 NA
14 0.3 NA
15 0.6 NA
16 1.2 NA
17 1.2 NA
18 0.9 NA
19 0.9 NA
20 0.9 NA
21 0.6 NA
22 1.2 NA
23 0.6 NA
24 1.5 NA
25 0.6 NA
26 0.6 NA
27 1.2 NA
28 0.6 NA
29 0.6 NA
30 0.6 NA
31 0.6 NA
32 1.2 NA
33 0.3 NA
34 0.6 NA
35 0.6 NA
36 0.6 NA
37 0.6 NA
38 0.3 NA
39 0.6 NA
40 0.6 NA
41 0.9 NA
42 0.9 NA
43 0.6 NA
44 1.8 NA
45 1.2 NA
46 0.9 NA
47 0.6 NA
48 0.6 NA
49 0.6 NA
50 0.6 NA
51 4.2 NA
52 4.5 NA
53 4.5 NA
54 3.9 NA
55 4.5 NA
56 3.9 NA
57 4.8 NA
58 3.0 NA
59 3.9 NA
60 4.2 NA
61 3.0 NA
62 4.5 NA
63 3.0 NA
64 4.2 NA
65 3.9 NA
66 4.2 NA
67 4.5 NA
68 3.0 NA
69 4.5 NA
70 3.3 NA
71 5.4 NA
72 3.9 NA
73 4.5 NA
74 3.6 NA
75 3.9 NA
76 4.2 NA
77 4.2 NA
78 5.1 NA
79 4.5 NA
80 3.0 NA
81 3.3 NA
82 3.0 NA
83 3.6 NA
84 4.8 NA
85 4.5 NA
86 4.8 NA
87 4.5 NA
88 3.9 NA
89 3.9 NA
90 3.9 NA
91 3.6 NA
92 4.2 NA
93 3.6 NA
94 3.0 NA
95 3.9 NA
96 3.6 NA
97 3.9 NA
98 3.9 NA
99 3.3 NA
100 3.9 NA
101 7.5 NA
102 5.7 NA
103 6.3 NA
104 5.4 NA
105 6.6 NA
106 6.3 NA
107 5.1 NA
108 5.4 NA
109 5.4 NA
110 7.5 NA
111 6.0 NA
112 5.7 NA
113 6.3 NA
114 6.0 NA
115 7.2 NA
116 6.9 NA
117 5.4 NA
118 6.6 NA
119 6.9 NA
120 4.5 NA
121 6.9 NA
122 6.0 NA
123 6.0 NA
124 5.4 NA
125 6.3 NA
126 5.4 NA
127 5.4 NA
128 5.4 NA
129 6.3 NA
130 4.8 NA
131 5.7 NA
132 6.0 NA
133 6.6 NA
134 4.5 NA
135 4.2 NA
136 6.9 NA
137 7.2 NA
138 5.4 NA
139 5.4 NA
140 6.3 NA
141 7.2 NA
142 6.9 NA
143 5.7 NA
144 6.9 NA
145 7.5 NA
146 6.9 NA
147 5.7 NA
148 6.0 NA
149 6.9 NA
150 5.4 NA
> newarray <- array(c(1:8, 11:18, 111:118), dim = c(2, 4, 3))
> newarray
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
, , 2
[,1] [,2] [,3] [,4]
[1,] 11 13 15 17
[2,] 12 14 16 18
, , 3
[,1] [,2] [,3] [,4]
[1,] 111 113 115 117
[2,] 112 114 116 118
> data(longley)
> longley
GNP.deflator GNP Unemployed Armed.Forces
1947 83.0 234.289 235.6 159.0
1948 88.5 259.426 232.5 145.6
1949 88.2 258.054 368.2 161.6
1950 89.5 284.599 335.1 165.0
1951 96.2 328.975 209.9 309.9
1952 98.1 346.999 193.2 359.4
1953 99.0 365.385 187.0 354.7
1954 100.0 363.112 357.8 335.0
1955 101.2 397.469 290.4 304.8
1956 104.6 419.180 282.2 285.7
1957 108.4 442.769 293.6 279.8
1958 110.8 444.546 468.1 263.7
1959 112.6 482.704 381.3 255.2
1960 114.2 502.601 393.1 251.4
1961 115.7 518.173 480.6 257.2
1962 116.9 554.894 400.7 282.7
Population Year Employed
1947 107.608 1947 60.323
1948 108.632 1948 61.122
1949 109.773 1949 60.171
1950 110.929 1950 61.187
1951 112.075 1951 63.221
1952 113.270 1952 63.639
1953 115.094 1953 64.989
1954 116.219 1954 63.761
1955 117.388 1955 66.019
1956 118.734 1956 67.857
1957 120.445 1957 68.169
1958 121.950 1958 66.513
1959 123.366 1959 68.655
1960 125.368 1960 69.564
1961 127.852 1961 69.331
1962 130.081 1962 70.551
> myLogical<-sample(c(TRUE,FALSE),size = 10,replace = TRUE)
> myNumeric <- rnorm(10)
> myCharacter <- sample(c("AA", "A", "B", "BB"), size = 10, replace = TRUE)
> myDataFrame <- data.frame(myLogical, myNumeric, myCharacter)
>
> myDataFrame
myLogical myNumeric myCharacter
1 TRUE -1.85368938 BB
2 TRUE 0.42231442 AA
3 FALSE 1.86732013 B
4 TRUE 1.41980104 BB
5 TRUE -1.61678860 A
6 TRUE 0.97970745 A
7 TRUE 0.56190766 A
8 TRUE 0.36667516 AA
9 TRUE 1.15804502 AA
10 TRUE 0.05526391 BB
> args(ts)
function (data = NA, start = 1, end = numeric(), frequency = 1,
deltat = 1, ts.eps = getOption("ts.eps"), class = if (nseries >
1) c("mts", "ts", "matrix") else "ts",
names = if (!is.null(dimnames(data))) colnames(data) else paste("Series",
seq(nseries)))
NULL
>
> args(ts)
function (data = NA, start = 1, end = numeric(), frequency = 1,
deltat = 1, ts.eps = getOption("ts.eps"), class = if (nseries >
1) c("mts", "ts", "matrix") else "ts",
names = if (!is.null(dimnames(data))) colnames(data) else paste("Series",
seq(nseries)))
NULL
>
> Caps = Capitalization/1000
> rownames(Caps) = abbreviate(rownames(Caps), 6)
> Caps = ts(t(Caps), start = 2003, frequency = 1)
>
> Caps[, 1:7]
Time Series:
Start = 2003
End = 2008
Frequency = 1
ErnxUS TSXGrp AstrSE BmbySE
2003 1328.953 888.677 585.431 278.662
2004 12707.578 1177.517 776.402 386.321
2005 3632.303 1482.184 804.014 553.073
2006 15421.167 1700.708 1095.858 818.878
2007 15650.832 2186.550 1298.315 1819.100
2008 9208.934 1033.448 683.871 647.204
HngKSE NSEInd ShngSE
2003 714.597 252.893 360.106
2004 861.462 363.276 314.315
2005 1054.999 515.972 286.190
2006 1714.953 774.115 917.507
2007 2654.416 1660.096 3694.348
2008 1328.768 600.281 1425.354
> Caps[, 8:13]
Time Series:
Start = 2003
End = 2008
Frequency = 1
TokySE BMESSE DtschB LndnSE ErnxEU
2003 2953.098 726.243 1079.026 2460.064 2076.410
2004 3557.674 940.672 1194.516 2865.243 2441.261
2005 4572.901 959.910 1221.106 3058.182 2706.803
2006 4614.068 1322.915 1637.609 3794.310 3712.680
2007 4330.921 1781.132 2105.197 3851.705 4222.679
2008 3115.803 948.352 1110.579 1868.153 2101.745
SIX SE
2003 727.103
2004 826.040
2005 935.448
2006 1212.308
2007 1271.047
2008 857.306
>
> cor(Caps)
ErnxUS TSXGrp AstrSE BmbySE
ErnxUS 1.0000000 0.6630393 0.7865287 0.6418233
TSXGrp 0.6630393 1.0000000 0.9762190 0.8980776
AstrSE 0.7865287 0.9762190 1.0000000 0.8976500
BmbySE 0.6418233 0.8980776 0.8976500 1.0000000
HngKSE 0.7146096 0.9001868 0.9240435 0.9827704
NSEInd 0.6520491 0.9034089 0.9043544 0.9998054
ShngSE 0.5758457 0.7402908 0.7583782 0.9603068
TokySE 0.4356998 0.8147396 0.7482590 0.4966642
BMESSE 0.7729512 0.9543665 0.9786878 0.9656325
DtschB 0.7224010 0.9546141 0.9786540 0.9442181
LndnSE 0.5973448 0.8842631 0.8868782 0.6422512
ErnxEU 0.7243395 0.9720113 0.9905155 0.8643875
SIX SE 0.7537005 0.9497060 0.9746579 0.8531154
HngKSE NSEInd ShngSE TokySE
ErnxUS 0.7146096 0.6520491 0.5758457 0.4356998
TSXGrp 0.9001868 0.9034089 0.7402908 0.8147396
AstrSE 0.9240435 0.9043544 0.7583782 0.7482590
BmbySE 0.9827704 0.9998054 0.9603068 0.4966642
HngKSE 1.0000000 0.9856577 0.9324460 0.5338580
NSEInd 0.9856577 1.0000000 0.9566535 0.5084025
ShngSE 0.9324460 0.9566535 1.0000000 0.2391869
TokySE 0.5338580 0.5084025 0.2391869 1.0000000
BMESSE 0.9777935 0.9696532 0.8753545 0.6299205
DtschB 0.9508990 0.9474317 0.8429870 0.6344255
LndnSE 0.6631623 0.6500483 0.4274252 0.8452199
ErnxEU 0.8942630 0.8712478 0.7089918 0.7777069
SIX SE 0.9112086 0.8624268 0.7031102 0.7898999
BMESSE DtschB LndnSE ErnxEU
ErnxUS 0.7729512 0.7224010 0.5973448 0.7243395
TSXGrp 0.9543665 0.9546141 0.8842631 0.9720113
AstrSE 0.9786878 0.9786540 0.8868782 0.9905155
BmbySE 0.9656325 0.9442181 0.6422512 0.8643875
HngKSE 0.9777935 0.9508990 0.6631623 0.8942630
NSEInd 0.9696532 0.9474317 0.6500483 0.8712478
ShngSE 0.8753545 0.8429870 0.4274252 0.7089918
TokySE 0.6299205 0.6344255 0.8452199 0.7777069
BMESSE 1.0000000 0.9866516 0.7858692 0.9538240
DtschB 0.9866516 1.0000000 0.8432133 0.9726564
LndnSE 0.7858692 0.8432133 1.0000000 0.9260487
ErnxEU 0.9538240 0.9726564 0.9260487 1.0000000
SIX SE 0.9432906 0.9359378 0.8567163 0.9755160
SIX SE
ErnxUS 0.7537005
TSXGrp 0.9497060
AstrSE 0.9746579
BmbySE 0.8531154
HngKSE 0.9112086
NSEInd 0.8624268
ShngSE 0.7031102
TokySE 0.7898999
BMESSE 0.9432906
DtschB 0.9359378
LndnSE 0.8567163
ErnxEU 0.9755160
SIX SE 1.0000000
>
> pairs(Caps, pch = 19, cex = 0.8)
> R = as.matrix(Caps)
> n = ncol(R)
> Names = abbreviate(colnames(R), 4)
>
> n
[1] 13
> Names
ErnxUS TSXGrp AstrSE BmbySE HngKSE NSEInd
"ErUS" "TSXG" "AsSE" "BmSE" "HKSE" "NSEI"
ShngSE TokySE BMESSE DtschB LndnSE ErnxEU
"ShSE" "TkSE" "BMES" "DtsB" "LnSE" "ErEU"
SIX SE
"SIXS"
> corr <- cor(R)
>
> ncolors <- 10 * length(unique(as.vector(corr)))
> k <- round(ncolors/2)
>
> r <- c(rep(0, k), seq(0, 1, length = k))
>
> g <- c(rev(seq(0, 1, length = k)), rep(0, k))
>
> b <- rep(0, 2 * k)
>
> corrColorMatrix <- (rgb(r, g, b))
>
> > image(x = 1:n, y = 1:n, z = corr[, n:1], col = corrColorMatrix,
Error: unexpected '>' in ">"
> axes = FALSE, main = "", xlab = "", ylab = "")
Error: unexpected ',' in " axes = FALSE,"
>
> > image(x = 1:n, y = 1:n, z = corr[, n:1], col = corrColorMatrix,axes = FALSE, main = "", xlab = "", ylab = "")
Error: unexpected '>' in ">"
>
> image(x = 1:n, y = 1:n, z = corr[, n:1], col = corrColorMatrix,axes = FALSE, main = "", xlab = "", ylab = "")
> axis(2, at = n:1, labels = colnames(R), las = 2)
>
> axis(1, at = 1:n, labels = colnames(R), las = 2)
>
> title(main = "Pearson Correlation Image Matrix")
> box()
>
> x = y = 1:n
> nx = ny = length(y)
> xoy = cbind(rep(x, ny), as.vector(matrix(y, nx, ny, byrow = TRUE)))
> coord = matrix(xoy, nx * ny, 2, byrow = FALSE)
> X = t(corr)
> for (i in 1:(n * n)) {
+ text(coord[i, 1], coord[n * n + 1 - i, 2], round(X[coord[i,1], coord[i, 2]], digits = 2), col = "white", cex = 0.7)
+ }


0 Comments