Primary exercises

Extra exercises

  1. Correlated vectors.
    Let’s define a parameter size <- 12. Later this will be the number of rows of the matrix.
    From normal distribution let’s create a random vector x <- rnorm( size ).
    Now let’s create another vector x1 by adding (on average 10 times smaller) noise to x: x1 <- x + rnorm( size )/10.
    Correlation coefficient of x and x1 should be close to 1.0: check this with function cor.
    Finally, create similarly vectors x2 and x3 by adding (other) noise to x.
size <- 12
x <- rnorm( size )
x1 <- x + rnorm( size )/10
cor( x, x1 )
[1] 0.9948405
x2 <- x + rnorm( size )/10
x3 <- x + rnorm( size )/10
  1. Matrix from vectors; matrix heatmap.
    Let’s merge x1, x2 and x3 column-wise into a matrix using m <- cbind( x1, x2, x3 ).
    Check class of m.
    Show the first few rows of m.
    Try a simple heatmap visualisation of the matrix: heatmap( m, Colv = NA, Rowv = NA, scale = "none" ).
    Which colors correspond to lowest/highest matrix values?
    Do the vectos appear correlated?
m <- cbind( x1, x2, x3 )
class( m )
[1] "matrix" "array" 
head( m )
              x1         x2          x3
[1,] -0.79804191 -0.6968063 -0.86069255
[2,] -0.53651567 -0.4317393 -0.50454893
[3,] -1.47253036 -1.3537469 -1.44586472
[4,] -2.38396163 -2.4480630 -2.44236755
[5,] -0.77022463 -0.7512547 -0.89459390
[6,] -0.03357812 -0.2959338  0.02053955
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
  1. Matrix of correlated and uncorrelated vectors.
    Repeat the first exercise and create several additional correlated vectors y1y4 (but not correlated with x), of the same length size.
    Now build again m from columns x1x3,y1y4 in some random order.
    Show again the heatmap; you should see similarity between some columns.
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

  1. Matrix of correlations.
    Use cc <- cor( m ) to build the matrix of correlation coefficients between columns of m.
    Use round( cc, 3 ) to show this matrix with 3 digits precision.
    Can you interpret the values?
cc <- cor( m )
round( cc, 3 ) # 
       y4     y3     x2     y1     x1     x3     y2
y4  1.000  0.990 -0.681  0.971 -0.689 -0.647  0.959
y3  0.990  1.000 -0.668  0.981 -0.671 -0.630  0.967
x2 -0.681 -0.668  1.000 -0.679  0.992  0.989 -0.620
y1  0.971  0.981 -0.679  1.000 -0.677 -0.636  0.975
x1 -0.689 -0.671  0.992 -0.677  1.000  0.993 -0.624
x3 -0.647 -0.630  0.989 -0.636  0.993  1.000 -0.585
y2  0.959  0.967 -0.620  0.975 -0.624 -0.585  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