Created
October 29, 2015 00:23
-
-
Save t-redactyl/00a4ff629bf06a989e29 to your computer and use it in GitHub Desktop.
Code associated with blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2); library(gridExtra) | |
g1 <- ggplot(data=mtcars, aes(x=wt, y=mpg)) + | |
geom_point(alpha = 0.7, colour = "#0971B2") + | |
ylab("Miles per gallon") + | |
ylim(10, 35) + | |
xlab("Weight (`000 lbs)") + | |
ggtitle("Untransformed Weight") + | |
geom_vline(xintercept = 0) + | |
theme_bw() | |
mtcars$centred.wt <- mtcars$wt - mean(mtcars$wt) | |
g2 <- ggplot(data=mtcars, aes(x=centred.wt, y=mpg)) + | |
geom_point(alpha = 0.7, colour = "#0971B2") + | |
ylab("Miles per gallon") + | |
ylim(10, 35) + | |
xlab("Centred Weight (`000 lbs)") + | |
ggtitle("Centred Weight") + | |
geom_vline(xintercept = 0) + | |
theme_bw() | |
grid.arrange(g1, g2, nrow = 1, ncol = 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
centred.model.auto <- lm(mpg ~ I(wt - mean(mtcars$wt)), data = mtcars[mtcars$am == 0, ]) | |
centred.model.manual <- lm(mpg ~ I(wt - mean(mtcars$wt)), data = mtcars[mtcars$am == 1, ]) | |
manual.car <- data.frame(wt = 2.620) | |
auto.car <- data.frame(wt = 3.570) | |
# CI for mean | |
manual.ci <- predict(centred.model.manual, newdata = manual.car, | |
interval = ("confidence"), level = 0.95) | |
auto.ci <- predict(centred.model.auto, newdata = auto.car, | |
interval = ("confidence"), level = 0.95) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
gp <- ggplot(data=mtcars, aes(x=centred.wt, y=mpg, colour=am.f)) + | |
geom_point(alpha = 0.7) + | |
geom_abline(intercept = coef(centred.model)[1], slope = coef(centred.model)[3], | |
size = 1, color = "#B21212") + | |
geom_abline(intercept = coef(centred.model)[1] + coef(centred.model)[2], | |
slope = coef(centred.model)[3] + coef(centred.model)[4], | |
size = 1, color = "#0971B2") + | |
scale_colour_manual(name="Transmission", values =c("#B21212", "#0971B2")) + | |
ylab("Miles per gallon") + | |
xlab("Centred Weight (`000 lbs)") + | |
theme_bw() | |
gp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new.cars <- data.frame(wt = 2) | |
# Prediction interval for new values | |
manual.predict.ci <- predict(centred.model.manual, newdata = new.cars, | |
interval = ("prediction"), level = 0.95) | |
auto.predict.ci <- predict(centred.model.auto, newdata = new.cars, | |
interval = ("prediction"), level = 0.95) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
centred.model <- lm(mpg ~ I(wt - mean(wt)) + am.f + I(wt - mean(wt)) * am.f, data = mtcars) | |
summary(centred.model)$coef |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data(mtcars) | |
mtcars$am.f <- as.factor(mtcars$am); levels(mtcars$am.f) <- c("Automatic", "Manual") | |
# Build the model | |
model <- lm(mpg ~ wt + am.f + wt * am.f, data = mtcars) | |
summary(model)$coef |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment