### --- SGTA Week 10 --- ###
## Question 1
turtles <- read.table("data/turtles.csv",
sep = ",",
header = TRUE)
# a
turtles_lm <- lm(eggs ~ carapace, data = turtles)
plot(turtles_lm, which = 1) # variance ok, clear nonlinearity
plot(turtles_lm, which = 2) # lots of points far away from the line, doesnt look normal
# b
turtles_quad <- lm(eggs ~ carapace + I(carapace^2), data = turtles)
turtles_cube <- lm(eggs ~ carapace + I(carapace^2) + I(carapace^3), data = turtles)
# c
anova(turtles_quad)
anova(turtles_cube)
# cubic effect is not a significant improvement to quadratic model
# d
summary(turtles_quad)
plot(turtles_quad, which = 1)
plot(turtles_quad, which = 2)
# diagnostics are better, because:
#
#
# e
plot(eggs ~ carapace, data = turtles)
abline(turtles_lm, col = "firebrick") # THIS IS NOT THE CORRECT MODEL
x <- seq(min(turtles$carapace), max(turtles$carapace), length.out = 100)
y <- predict(turtles_quad, newdata = data.frame(carapace = x))
lines(x, y, col = "cornflowerblue")
# yhat = -899.9 + 5.871 * carap. -0.0094 * carap.^2
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}