ทำความเข้าใจโครงสร้างของ input ประเภท categorical
ในแบบฝึกหัดนี้ จะเรียกใช้ model.matrix() (docs) เพื่อดูว่า R แทนข้อมูลที่มีทั้ง input ประเภท categorical และ numerical อย่างไรในการสร้างโมเดล
โหลดชุดข้อมูล flowers (ซึ่งได้มาจากแพ็กเกจ Sleuth3) ให้แล้ว โดยมีคอลัมน์ดังต่อไปนี้:
Flowers: จำนวนดอกไม้เฉลี่ยต่อต้นของพืช meadowfoamIntensity: ความเข้มของแสงที่ใช้กับต้นพืชTime: ตัวแปรประเภท categorical — ระบุว่าการรักษาด้วยแสงเกิดขึ้นช่วงใด (LateหรือEarly) ในวงจรชีวิตของพืช
เป้าหมายสุดท้ายคือการทำนาย Flowers จาก Time และ Intensity
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Supervised Learning ใน R: การถดถอย
คำแนะนำการฝึกหัด
- เรียกใช้ฟังก์ชัน
str()กับflowersเพื่อดูประเภทของแต่ละคอลัมน์ - ใช้ฟังก์ชัน
unique()กับคอลัมน์flowers$Timeเพื่อดูค่าที่เป็นไปได้ของTimeมีค่าที่ไม่ซ้ำกันทั้งหมดกี่ค่า? - สร้างสูตรเพื่อแสดง
Flowersเป็นฟังก์ชันของIntensityและTimeกำหนดให้กับตัวแปรfmlaแล้วพิมพ์ออกมา - ใช้
fmlaและmodel.matrix()เพื่อสร้าง model matrix สำหรับ data frameflowersกำหนดให้กับตัวแปรmmat - ใช้
head()เพื่อดู 20 แถวแรกของflowers - จากนั้นดู 20 แถวแรกของ
mmat- คอลัมน์ตัวเลข
Intensityเปลี่ยนแปลงไปหรือไม่? - คอลัมน์ประเภท categorical อย่าง
Timeจากflowersเปลี่ยนไปอย่างไร? Time == 'Early'ถูกแทนด้วยค่าอะไร? แล้วTime == 'Late'ล่ะ?
- คอลัมน์ตัวเลข
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Call str on flowers to see the types of each column
___
# Use unique() to see how many possible values Time takes
___
# Build and print a formula to express Flowers as a function of Intensity and Time: fmla
(fmla <- ___("Flowers ~ Intensity + Time"))
# Use fmla and model.matrix to see how the data is represented for modeling
mmat <- ___
# Examine the first 20 lines of flowers
___
# Examine the first 20 lines of mmat
___