--- title: "pulse" author: "Chris Parrish" date: "January 23, 2016" output: pdf_document --- pulse reference: - Cannon, et al., Stat2, chapter 07, example 7.20 ## data Import the data. ```{r} data <- read.csv("Pulse.csv", header=TRUE) head(data, 3) dim(data) ``` Scatterplot matrix. ```{r} pairs(~ Active + Rest + Smoke + Gender + Exercise + Hgt + Wgt, data=data, col="darkred") ``` ## ANOVA: Active ~ Exercise ANOVA: Active ~ Exercise ```{r} data\$Exercise <- factor(data\$Exercise, levels=1:3) # Exercise is a factor with 3 levels pulse.aov <- aov(Active ~ Exercise, data=data) options(show.signif.stars=FALSE) summary(pulse.aov) pulse.aov ``` Residuals. ```{r fig.width=6, fig.height=4.2} qqnorm(resid(pulse.aov), col="cadetblue") qqline(resid(pulse.aov), col="orange") plot(predict(pulse.aov), resid(pulse.aov), pch=20, col="darkred") ``` Levene's test. ```{r} library(car) leveneTest(Active ~ Exercise, data=data) ``` ## ANCOVA with resting pulse rate as covariate. Response ~ covariate ```{r} plot(Active ~ jitter(Rest), data=data, # jitter the points a bit pch=20, col="darkred") # to avoid overlap ``` ANCOVA with resting pulse rate as covariate. ```{r} pulse.lm <- lm(Active ~ Exercise + Rest, data=data) summary(pulse.lm) anova(pulse.lm) ``` Residuals. ```{r fig.width=6, fig.height=4.2} qqnorm(resid(pulse.lm), col="cadetblue") qqline(resid(pulse.lm), col="orange") plot(predict(pulse.lm), resid(pulse.lm), pch=20, col="darkred") ``` ## ANOVA with log transformation: log(Active) ~ Exercise Response ~ covariate: log(Active) ~ log(Rest) ```{r} plot(log(Active) ~ log(Rest), data=data, pch=20, col="darkred") ``` ANOVA with log transformation: log(Active) ~ Exercise ```{r} pulse.aov2 <- aov(log(Active) ~ Exercise, data=data) summary(pulse.aov2) pulse.aov2 ``` ## ANCOVA with log transformation: log(Active) ~ log(Rest) + Exercise ANCOVA with log transformation: log(Active) ~ log(Rest) + Exercise ```{r} pulse.lm2 <- lm(log(Active) ~ log(Rest) + Exercise, data=data) summary(pulse.lm2) anova(pulse.lm2) ``` Residuals. ```{r} qqnorm(resid(pulse.lm2), col="cadetblue") qqline(resid(pulse.lm2), col="orange") plot(predict(pulse.lm2), resid(pulse.lm2), pch=20, col="darkred") ``` Final model: log(Active) ~ log(Rest) | Exercise ```{r} plot(log(Active) ~ log(Rest), data=data, xlab="log(Rest)", ylab="log(Active)", type="n") exercise1 <- data[data\$Exercise==1, 2:1] exercise2 <- data[data\$Exercise==2, 2:1] exercise3 <- data[data\$Exercise==3, 2:1] points(log(exercise1), pch=20, col="darkred") points(log(exercise2), pch=20, col="orange") points(log(exercise3), pch=20, col="steelblue") legend("topleft", c("Ex1","Ex2","Ex3"), pch=20, inset=0.02, col=c("darkred", "orange", "steelblue")) abline(lm(log(Active) ~ log(Rest), data=exercise1), lty=1, col="darkred") abline(lm(log(Active) ~ log(Rest), data=exercise2), lty=2, col="orange") abline(lm(log(Active) ~ log(Rest), data=exercise3), lty=3, col="steelblue") ``` Main effects. ```{r message=FALSE} library(alr4) plot(allEffects(pulse.lm2)) ```