关键词 > STAT3064/STAT5061

STAT3064/STAT5061 Statistical Learning/Statistical Data Science Computer Lab 2

发布时间:2022-08-22

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

STAT3064/STAT5061 Statistical Learning/Statistical Data Science

Computer Lab 2, Semester 2 2022

Things you may need to know/do.

Relates to lecture 2.

●  Libraries ggplot2, MASS, tidyverse, GGally may be useful.  Others may be mentioned below in the hints.

For Q3 you will also need the library mvtnorm.

● You might like to set up a project for each lab if you are using RStudio. Then you can copy a .Rmd file into that directory and write your answers in that le.

Q1

The dataframe aircraft refers to the aircraft data from the previous lab, the version with the logged variables. In this lab we work only with the logged variables plus Year and Period.

Use prcomp for this question.

(a)  Conduct a PCA of the raw (unscaled) data, that is, the six logged variables. Report the eigenvectors and the eigenvalues.

(b)  Apply print, summary and str to the object produced by prcomp and work out the correspondence

between these outputs and the terminology of PCA concepts used in the lecture notes.  Note that,

unlike the other two commands, str shows what the object actually is (i.e. its structure).

(c)  Make a scree plot and a plot of the cumulative contribution to total variance.  (Hint:  cumsum.)

(d)  Plot a smoothed histogram (density) of the PC1 scores and separately of the PC2 scores.

(e)  Make a scatterplot of the PC1 scores (x-axis) against the PC2 scores (y-axis) and colour the points

according to the value of Period.  Overlay contours of a 2D density estimate.

(f)  Make a 2D biplot.  (Hint: biplot, with col  =  c("white",  "blue").)

Q2

Repeat Q1 but with scaled log data. How do the two analyses dier? Why do we see these dierences?


Q3

We work with the matrix Σ0  from Q4 of Lab 1 which is given again below. As in Q5 of Lab 1 we generate random samples, with n = 250, from each of the following distributions (i) the multivariate normal distribution N(0, Σ0 ) (ii) the multivariate t-distribution t6 (0, Σ0 ) in ν = 6 degrees of freedom.

(a)  Start with the random sample of the N(0, Σ0 ) distribution and calculate the covariance matrix of the random sample.

(b)  Calculate the eigenvalues and eigenvectors of the covariance matrix and compare to those of the population matrix Σ0  (from Lab 1).

(c)  Show a graph of the eigenvalues and of the cumulative contribution to total variance against the index.

(d)  Plot a smoothed histogram (density) of all six PC scores.

(e)  Make a scatterplot of the PC1 scores (x-axis) against the PC2 scores (y-axis). Overlay contours of a 2D density estimate.

(f)  Repeat parts (a) to (e) for the random sample of the t6 (0, Σ0 ) distribution.

(g)  Compare Σ0  with the two sample covariance matrices: how dierent are the eigenvalues and eigenvectors

of the three matrices? Which are most similar? Why do they dier?

Sigma0  = matrix(c (

3.1386518 ,  0.38872659 ,  0.6178228 ,  1.7576847 ,  0.77433973 ,  0.7508789 ,

0.3887266 ,  1.21417949 ,  0.1941453 ,  0.4518920 ,  0.01236855 ,  0.2155466 ,

0.6178228 ,  0.19414529 ,  1.2437919 ,  0.5970320 ,  0.15151088 ,  0.2665018 ,

1.7576847 ,  0.45189196 ,  0.5970320 ,  1.7083497 ,  0.52685668 ,  0.7109476 ,

0.7743397 ,  0.01236855 ,  0.1515109 ,  0.5268567 ,  0.53406192 ,  0.2299193 ,

0.7508789 ,  0.21554658 ,  0.2665018 ,  0.7109476 ,  0.22991933 ,  0.6642375),

byrow  =  TRUE ,  nrow  =  6  )

Code chunk for generating random samples from the t-distribution in df degrees of freedom.

Remember that you will need the library mvtnorm here.

Sigma0sym  =  0.5* (Sigma0 + t(Sigma0))

Xmat  = rmvt(100 ,  sigma  =  Sigma0sym,  df  =  6)