size <- 12
. Later this will be
the number of rows of the matrix.x <- rnorm( size )
.x1
by adding (on average 10
times smaller) noise to x
:
x1 <- x + rnorm( size )/10
.x
and x1
should be
close to 1.0: check this with function cor
.x2
and x3
by
adding (other) noise to x
.size <- 12
x <- rnorm( size )
x1 <- x + rnorm( size )/10
cor( x, x1 )
[1] 0.9959
x2 <- x + rnorm( size )/10
x3 <- x + rnorm( size )/10
x1
, x2
and x3
column-wise into a matrix using
m <- cbind( x1, x2, x3 )
.m
.m
.heatmap( m, Colv = NA, Rowv = NA, scale = "none" )
.m <- cbind( x1, x2, x3 )
class( m )
[1] "matrix" "array"
head( m )
x1 x2 x3
[1,] 0.08324609 -0.09236818 0.1360844
[2,] 0.95217717 0.97401499 1.0644680
[3,] 1.05493810 0.86536330 1.1609078
[4,] 3.03369476 3.17010449 3.2256516
[5,] -0.32176015 -0.25070969 -0.1153440
[6,] 0.75190513 0.95236714 0.9025836
heatmap( m, Colv = NA, Rowv = NA, scale = "none" ) # high is dark red, low is yellow
# x1, x2, x3 follow similar color pattern, they should be correlated
y1
…y4
(but not correlated with
x
), of the same length size
.m
from columns
x1
…x3
,y1
…y4
in some
random order.y <- rnorm( size )
y1 <- y + rnorm( size )/10
y2 <- y + rnorm( size )/10
y3 <- y + rnorm( size )/10
y4 <- y + rnorm( size )/10
m <- cbind( y4, y3, x2, y1, x1, x3, y2 )
heatmap( m, Colv = NA, Rowv = NA, scale = "none" ) # high is dark red, low is yellow
cc <- cor( m )
to build the matrix of correlation
coefficients between columns of m
.round( cc, 3 )
to show this matrix with 3 digits
precision.cc <- cor( m )
round( cc, 3 ) #
y4 y3 x2 y1 x1 x3 y2
y4 1.000 0.972 -0.134 0.982 -0.087 -0.162 0.984
y3 0.972 1.000 -0.083 0.969 -0.029 -0.105 0.976
x2 -0.134 -0.083 1.000 -0.071 0.992 0.991 -0.124
y1 0.982 0.969 -0.071 1.000 -0.019 -0.090 0.985
x1 -0.087 -0.029 0.992 -0.019 1.000 0.992 -0.067
x3 -0.162 -0.105 0.991 -0.090 0.992 1.000 -0.142
y2 0.984 0.976 -0.124 0.985 -0.067 -0.142 1.000
heatmap( cc, symm = TRUE, scale = "none" )
# E.g. value for (row: x1, col: y1) is the corerlation of vectors x1, y1.
# Values of 1.0 are on the diagonal: e.g. x1 is perfectly correlated with x1.
# Correlations between x, x vectors are close to 1.0.
# Correlations between y, y vectors are close to 1.0.
# Correlations between x, y vectors are close to 0.0.
Copyright © 2023 Biomedical Data Sciences (BDS) | LUMC