### MASM22/FMSN30/FMSN40 spring 2019. Anna Lindgren ###
### lecture 1, 25/3-19. AL
# put ice-cream data from lecture 1 in a data frame:
icecream <- data.frame(weeks = c(25, 26, 27, 30, 31, 32, 35),
loss = c(28, 28.3, 29.7, 35.3, 36.4, 37.0, 40.2))
# and plot it:
with(icecream,
plot(loss ~ weeks,
xlab = "time (weeks)",
ylab = "weight loss (g)",
xlim = c(23, 38),
ylim = c(25, 45),
main = "Ice cream weight loss"))
### alternative, but longer version:
plot(icecream$loss ~ icecream$weeks,
xlab = "time (weeks)",
ylab = "weight loss (g)",
xlim = c(23, 38),
ylim = c(25, 45),
main = "Ice cream weight loss")
# yet another version with x, y instead of y ~ x:
plot(icecream$weeks, icecream$loss,
xlab = "time (weeks)",
ylab = "weight loss (g)",
xlim = c(23, 38),
ylim = c(25, 45),
main = "Ice cream weight loss")
# compute regression estimates "by hand"
mx <- mean(icecream$weeks)
my <- mean(icecream$loss)
beta1 <- sum((icecream$weeks - mx) * (icecream$loss - my)) /
sum((icecream$weeks - mx)^2)
beta0 <- my - beta1 * mx
beta0
beta1
# add the estimated straight line to the plot:
abline(a = beta0, b = beta1)
# estimated average weight loss at 34 weeks, by hand
y0 <- beta0 + beta1 * 34
y0
# add it to the plot;
points(34, y0, col = "black", pch = 16)
# residual standard error by hand:
yhat <- beta0 + beta1 * icecream$weeks
e <- icecream$loss - yhat
n <- nrow(icecream)
s2 <- sum(e^2) / (n - 2)
s <- sqrt(s2)
s
# now using lm(): define an "object" named 'model' which can be reused later on
model <- lm(loss ~ weeks, data = icecream)
model
summary(model)
# add the estimated model to the plot (again)
abline(model, col = "blue")
# prediction using the predict function
x0 <- 34
y0 <- predict(model, data.frame(weeks = x0))
y0
points(x0, y0, col = "blue", pch = 16)