解釋變異數
在這個練習中,你要產生 scree 圖,顯示隨著主成分數量增加時,各主成分所解釋的變異比例。必須先把 PCA 的輸出整理成能用於繪圖的資料,因為 R 並沒有可直接由 PCA 模型產生這些圖的內建函式。
在閱讀這些圖時,請思考解釋變異是否出現「手肘」轉折,藉此選擇一個自然的主成分數量。若沒有明顯的手肘(這在真實世界的資料集很常見),就要考慮還有哪些方式,能根據 scree 圖來決定要保留多少個主成分。
本練習屬於課程
R 的無監督式學習
練習說明
你先前建立的變數 wisc.data、diagnosis 和 wisc.pr 仍可使用。
- 將
wisc.pr的sdev元件平方,計算每個主成分的變異數,並將結果儲存為pr.var。 - 將每個主成分的變異數除以所有主成分變異數的總和,計算各主成分的解釋變異比例,指定為變數
pve。 - 繪製各主成分之解釋變異比例的圖。
- 使用
cumsum()函式,繪製解釋變異比例的累積曲線圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Set up 1 x 2 plotting grid
par(mfrow = c(1, 2))
# Calculate variability of each component
# Variance explained by each principal component: pve
# Plot variance explained for each principal component
plot(___, xlab = "Principal Component",
ylab = "Proportion of Variance Explained",
ylim = c(0, 1), type = "b")
# Plot cumulative proportion of variance explained
plot(___, xlab = "Principal Component",
ylab = "Cumulative Proportion of Variance Explained",
ylim = c(0, 1), type = "b")