Multidimensional Scaling ในสองมิติ
ในแบบฝึกหัดนี้ จะได้ทำ multidimensional scaling กับข้อมูล state.x77 แล้วพล็อตตำแหน่งของวัตถุที่ได้ในพื้นที่สองมิติ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การแจกแจงความน่าจะเป็นหลายตัวแปรใน R
คำแนะนำการฝึกหัด
- คำนวณ distance matrix และกำหนดให้กับออบเจกต์ชื่อ
state.dist - คำนวณ multidimensional scaling ของข้อมูลจาก distance matrix
- ใช้ฟังก์ชัน
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)