始める無料で始める

分散・共分散の計算

前の演習では、combine データを前処理して行列 A を作成しました。ここでは A を使って、このデータセットの分散・共分散行列を作成します。

この演習はコースの一部です

Rで学ぶデータサイエンスのための線形代数

コースを見る

演習の手順

  • 行列 \(\frac{A^TA}{n-1}\) を作成し、B という名前を付けます。
  • B[1,1] 要素が、A の第1列の分散と同じであることを確認します。
  • B[1,2] および [2,1] 要素が、A の第1列と第2列の共分散と同じであることを確認します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Create matrix B from equation in instructions
B <- ___(A)%*%A/(___(A) - 1)

# Compare 1st element of the 1st column of B to the variance of the first column of A
B[1,1]
____(A[, 1])

# Compare 1st element of 2nd column of B to the 1st element of the 2nd row of B to the covariance between the first two columns of A
B[1, 2]
B[2, 1]
___(A[, 1], A[, 2])
コードを編集して実行