โมเดลเบื้องต้น
คุณได้รับชุดข้อมูลที่บันทึกค่าแรงโน้มถ่วงระหว่างวัตถุสองชิ้นที่ระยะต่างๆ และมีโจทย์ให้สร้างโมเดลอย่างง่ายเพื่อพยากรณ์แรงดังกล่าวจากระยะทางที่กำหนด ในขั้นแรกนี้จะใช้การถดถอยเชิงเส้นแบบง่าย ข้อมูลประกอบด้วยคู่ distance และ force จำนวน 120 คู่ และถูกโหลดให้แล้วในชื่อ newton
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Feature Engineering in R
คำแนะนำการฝึกหัด
- สร้างโมเดลเชิงเส้นจากข้อมูล
newtonโดยใช้ฟังก์ชัน linear model จาก baseRและกำหนดผลลัพธ์ให้กับตัวแปรlr_force - สร้าง data frame ใหม่ชื่อ
dfโดยผูกค่าพยากรณ์เข้ากับข้อมูลเดิมในnewton - สร้าง scatterplot ของ
forceversusdistanceโดยใช้ggplot() - เพิ่มเส้นถดถอยลงบน scatterplot โดยใช้ค่าที่ฟิตได้จากโมเดล
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Build a linear model for the newton the data and assign it to lr_force
lr_force <- ___(force ~ distance, data = ___)
# Create a new data frame by binding the prediction values to the original data
df <- newton %>% ___(lr_pred = predict(lr_force))
# Generate a scatterplot of force vs. distance
df %>%
ggplot(aes(x = distance, y = force)) +
geom____() +
# Add a regression line with the fitted values
geom_line(aes(y = ___), color = "blue", lwd = .75) +
ggtitle("Linear regression of force vs. distance") +
theme_classic()