## ----echo=FALSE, results='hide'------------------------------------------ source(".Rprofile") ## ------------------------------------------------------------------------ data(Draha, package = "mffSM") head(Draha) dim(Draha) summary(Draha) ## ----fig-CheckModelAssumpt-04-01, fig.ext='png', fig.path=FIGDIRPNG, fig.width=9, fig.height=6---- par(mfrow = c(1, 1), bty = BTY, mar = c(5, 4, 1, 2) + 0.1) plot(breakd ~ veloc, data = Draha, xlab = "Velocity [km/h]", ylab = "Breaking distance [m]", pch = PCH, col = COL, bg = BGC) ## ------------------------------------------------------------------------ m1 <- lm(breakd ~ veloc + I(veloc^2) - 1, data = Draha, x = TRUE) summary(m1) vpred <- seq(0, 45, length = 250) p1 <- predict(m1, newdata = data.frame(veloc = vpred)) ## ----fig-CheckModelAssumpt-04-02, fig.ext='png', fig.path=FIGDIRPNG, fig.width=9, fig.height=6---- par(mfrow = c(1, 1), bty = BTY, mar = c(5, 4, 1, 2) + 0.1) plot(breakd ~ veloc, data = Draha, xlim = range(vpred), xlab = "Velocity [km/h]", ylab = "Breaking distance [m]", pch = PCH, col = COL2, bg = BGC2) lines(vpred, p1, col = "red3", lwd = 2) ## ----fig-CheckModelAssumpt-04-03, fig.ext='png', fig.path=FIGDIRPNG, fig.width=9, fig.height=6---- par(mfrow = c(1, 2), bty = BTY, mar = c(5, 4, 3, 2) + 0.1) plot(m1, which = 1, pch = 21, col = "blue4", bg = "skyblue", lwd = 2) # plot(m1[["x"]][, 1], residuals(m1), pch = 21, col = "blue4", bg = "skyblue", xlab = "Velocity [km/h]", ylab = "Residuals", main = "Residuals vs Covariate") lines(lowess(m1[["x"]][, 1], residuals(m1)), col = "red3", lwd = 2) ## ----fig-CheckModelAssumpt-04-04, fig.ext='png', fig.path=FIGDIRPNG, fig.width=9, fig.height=6---- par(mfrow = c(1, 2), bty = BTY, mar = c(5, 4, 3, 2) + 0.1) plot(m1, which = 3, pch = 21, col = "blue4", bg = "skyblue", lwd = 2) # plot(m1[["x"]][, 1], sqrt(abs(rstandard(m1))), pch = 21, col = "blue4", bg = "skyblue", xlab = "Velocity [km/h]", ylab = as.expression(substitute(sqrt(abs(yL)), list(yL = as.name("Standardized residuals")))), main = "Scale-Location (Velocity)") lines(lowess(m1[["x"]][, 1], sqrt(abs(rstandard(m1)))), col = "red3", lwd = 2) ## ------------------------------------------------------------------------ library("car") ncvTest(m1) ## ------------------------------------------------------------------------ library("lmtest") bptest(m1, varformula = ~fitted(m1), studentize = FALSE) ## ------------------------------------------------------------------------ bptest(m1, varformula = ~fitted(m1)) ## ------------------------------------------------------------------------ ncvTest(m1, var.formula = ~veloc, data = Draha) ## ------------------------------------------------------------------------ bptest(m1, varformula = ~veloc, studentize = FALSE, data = Draha) ## ------------------------------------------------------------------------ bptest(m1, varformula = ~veloc, data = Draha)