Facebook
From Sole Kitten, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 36
  1. ### --- SGTA Week 10 --- ###
  2.  
  3. ## Question 1
  4. turtles <- read.table("data/turtles.csv",
  5.                       sep = ",",
  6.                       header = TRUE)
  7.  
  8. # a
  9. turtles_lm <- lm(eggs ~ carapace, data = turtles)
  10. plot(turtles_lm, which = 1) # variance ok, clear nonlinearity
  11. plot(turtles_lm, which = 2) # lots of points far away from the line, doesnt look normal
  12.  
  13. # b
  14. turtles_quad <- lm(eggs ~ carapace + I(carapace^2), data = turtles)
  15. turtles_cube <- lm(eggs ~ carapace + I(carapace^2) + I(carapace^3), data = turtles)
  16.  
  17. # c
  18. anova(turtles_quad)
  19. anova(turtles_cube)
  20. # cubic effect is not a significant improvement to quadratic model
  21.  
  22. # d
  23. summary(turtles_quad)
  24. plot(turtles_quad, which = 1)
  25. plot(turtles_quad, which = 2)
  26. # diagnostics are better, because:
  27. #
  28. #
  29.  
  30. # e
  31. plot(eggs ~ carapace, data = turtles)
  32. abline(turtles_lm, col = "firebrick") # THIS IS NOT THE CORRECT MODEL
  33. x <- seq(min(turtles$carapace), max(turtles$carapace), length.out = 100)
  34. y <- predict(turtles_quad, newdata = data.frame(carapace = x))
  35. lines(x, y, col = "cornflowerblue")
  36. # yhat = -899.9 + 5.871 * carap. -0.0094 * carap.^2
  37.