Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you help oon this ? Given the following data points, calculate its covarianc

ID: 3358505 • Letter: C

Question

Can you help oon this ?

Given the following data points, calculate its covariance matrix's eigenvector that corresponds to the first principal component. Write down the first component of the eigenvector.

(0, 2)

(2, 6)

(1, 4)

(2, 5)

(-1, -1)

You can calculate them by hand, but numpy also provides functions for calculating the covariance matrix and eigenvector & eigenvalues.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.cov.html (Links to an external site.)Links to an external site.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.linalg.eig.html (Links to an external site.)Links to an external site.

Explanation / Answer

we can do this in R as follows

x<- c(0,2,1,2,-1)
y<- c(2,6,4,5,-1)

df <- data.frame(x,y)

## covariance
cov(df)


# 1. Correlation matrix
df.scaled <- scale(df, center = TRUE, scale = TRUE)
res.cor <- cor(df.scaled)
round(res.cor, 2)


# 2. Calculate eigenvectors/eigenvalues
res.eig <- eigen(res.cor)
res.eig


## principal components
p <- prcomp(df, scale=T)
p

The results are

cov(df)
x y
x 1.70 3.55
y 3.55 7.70

> round(res.cor, 2)
x y
x 1.00 0.98
y 0.98 1.00
> res.eig
eigen() decomposition
$values
[1] 1.98120224 0.01879776

$vectors
[,1] [,2]
[1,] 0.7071068 -0.7071068
[2,] 0.7071068 0.7071068

> p
Standard deviations (1, .., p=2):
[1] 1.4075519 0.1371049

Rotation (n x k) = (2 x 2):
PC1 PC2
x 0.7071068 0.7071068
y 0.7071068 -0.7071068