開始使用免費開始

二維的多重對應縮放(MDS)

在這個練習中,你會對 state.x77 進行多重對應縮放,並在二維空間中繪製得到的物件位置。

本練習屬於課程

R 的多變量機率分配

檢視課程

練習說明

  • 計算距離矩陣,並指定給物件 state.dist
  • 在該距離矩陣上計算資料的多重對應縮放。
  • 使用 ggplot() 繪製 state.x77 在二維中的表示。使用向量 state.region 依地區為州名著色。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Calculate distance 
state.dist <- dist(___)

# Perform multidimensional scaling 
mds.state <- cmdscale(___) 

# Change mds.state to a data frame for use in ggplot
mds.state_df <- data.frame(mds.state)

# Plot the representation of the data in two dimensions 
ggplot(data = ___, aes(x = X1, y = X2, label = rownames(mds.state), color = ____)) + 
    geom_text(alpha = 0.8, size = 3) 
編輯並執行程式碼