---
title: "Project 5 - Key"
output: word_document
---

```{r message=FALSE, warning=FALSE,echo=FALSE}
library(readr)
library(mosaic)
```

```{r}
#adjust path to read in a .csv file with a row for each student including the four student names and car models
Groups <- read_csv("C:/Users/Robin/Dropbox/Robin/Courses/!Stat213/RProjects/Project5/Project5Groups.csv",show_col_types = FALSE)
#Project5Groupd.csv file should have variable names Group1, Group2, Group3, Group4 for the student names (assuming data sets are stored with their name.csv) and Model1, ... Model4 for the names of the car models
```


```{r echo=F, messages=FALSE, warnings=FALSE}
#Choose a row from Project5Groups.cs for the cars assigned to that student
student=52
```
Key: `r as.character(Groups$Group1[student])`   `r as.character(Groups$Model1[student])`
```{r echo=F, message=FALSE, warning=FALSE}
#adjust the path to where the student datasets are
path="C:/Users/Robin/Dropbox/Robin/Courses/!Stat213/Data/Project1/"
#read the data for each of the four students and put in the model names
A=read_csv(paste(path,as.character(Groups$Group1[student]),".csv",sep=""),show_col_types = FALSE)
  A$model=as.factor(as.character(Groups$Model1[student]))
B=read_csv(paste(path,as.character(Groups$Group2[student]),".csv",sep=""),show_col_types = FALSE)
  B$model=as.factor(as.character(Groups$Model2[student]))
C=read_csv(paste(path,as.character(Groups$Group3[student]),".csv",sep=""),show_col_types = FALSE)
  C$model=as.factor(as.character(Groups$Model3[student]))
D=read_csv(paste(path,as.character(Groups$Group4[student]),".csv",sep=""),show_col_types = FALSE)
  D$model=as.factor(as.character(Groups$Model4[student]))
data4=rbind(A,B,C,D)
```
Count=`r length(data4$price)`
```{r echo=F}
tapply(data4$price,data4$model,length)
```
Mean = `r round(mean(data4$price),2)`
```{r echo=F}
round(tapply(data4$price,data4$model,mean),2)
```
Std. Dev.=`r round(sd(data4$price),2)`
```{r echo=F}
round(tapply(data4$price,data4$model,sd),2)
```
```{r echo=F,fig.height=3,fig.width=9}
par(mar=c(4,8,1,1))
boxplot(price~model,data=data4,horizontal=TRUE,las=1,ylab="")
par(mar=c(5,4,2,2))
```

ANOVA
```{r echo=F}
mod=aov(price~model,data=data4)
summary(mod)
all=summary(mod)
```
```{r echo=F,fig.height=2.5,fig.width=9}
par(mar=c(4,3,1,1))
par(mfrow=c(1,2))
plot(mod$resid~mod$fitted,ylab="Residuals",xlab="Fitted"); abline(0,0)
qqnorm(mod$resid); qqline(mod$resid)
par(mar=c(5,4,2,2))
```

***
```{r echo=F}
LSD=qt(0.975,length(data4$model)-4)*sqrt(anova(mod)$"Mean Sq"[2])*sqrt(2/50)
HSD=qtukey(0.95,4,length(data4$model)-4)*sqrt(anova(mod)$"Mean Sq"[2])*sqrt(1/50)
```


LSD - Pairwise differences p-values  LSD=`r round(LSD,3)`
```{r echo=F}
pairwise.t.test(data4$price,data4$model,p.adj="none")$p.value
```
HSD - Pairwise CI's and p-values HSD=`r round(HSD,3)`
```{r echo=F, fig.height=2.5,fig.width=6}
hsd=TukeyHSD(mod)
hsd$model
par(mar=c(4,9,2,1))
plot(hsd,las=2)
par(mar=c(5,4,2,2))
```

Ordinary regression
```{r echo=F}
modreg=lm(price~model,data=data4)
sreg=summary(modreg)
sreg$coeff
anova(modreg)
```
