Chapter 19 Model testing

It is critically important to test the assumptions of your model as well as quantify the model uncertainity.

data(mtcars)
fit <- lm(mpg ~ poly(disp,2) + wt*disp, data = mtcars)
plot(fit)

library(arm)
display(fit)
## lm(formula = mpg ~ poly(disp, 2) + wt * disp, data = mtcars)
##                coef.est coef.se
## (Intercept)     31.09     3.16
## poly(disp, 2)1 -13.75    22.64
## poly(disp, 2)2   8.74     7.21
## wt              -3.63     2.70
## wt:disp          0.00     0.01
## ---
## n = 32, k = 5
## residual sd = 2.43, R-Squared = 0.86

Now that we have our model we car see the individual coefficient values. But what now if we want to explore our model? The best way to do this is through simulation

n_tilde <- nrow(mtcars)
x_tilde <- cbind(rep(1,n_tilde), mtcars$mpg, mtcars$mpg * mtcars$mpg, mtcars$disp, mtcars$mpg*mtcars$disp)
n_sims <- 1000
my_sim <- sim(fit, n_sims)
y_tilde <- array(NA, c(n_sims, n_tilde))
my_coef <-coef(my_sim)
my_sigma <- sigma.hat(my_sim)

for (s in 1:n_sims){
y_tilde[s,] <- rnorm(n_tilde, x_tilde %*% my_coef[s,], my_sigma[s])
}