---
title: "Lab 8: Multiple Groups"
author:  "ADD YOUR NAME HERE"
date: now
date-format: "DD/MM/YYYY HH:MM"
format:
    pdf
#    html:
#      theme: a11y
#      highlight-style: a11y
#      self-contained: true
---
 
# Setup

## Load R libraries

```{r}
#| warning: false
#| message: false
library(mosaic)
library(knitr)
library(abd)
library(emmeans)
```

## Setting the seed of the random number generator

Use the **set.seed()** function in R to initialize the random number generator.


```{r}
set.seed(2041971)

```
# $\chi^2$ Test of Association:  Black Headed Gulls

## Exercise 1
 

QUESTION:  State the null and alternative hypothesis, and use the chisq.test  function to perform the test.

ANSWER: 
Null Hypothesis:

Alternative Hypothesis:

```{r}
eggdat<-data.frame(group=rep(c("15cm", "100cm", "200cm"), 
                             c(150,150, 150)),
 predated=rep(c("Yes","No","Yes","No","Yes","No"),
              c(63,87,48,102,32,118)))
eggdat$group<-factor(eggdat$group, levels=c("15cm", "100cm","200cm"))
table.egg1<-tally(predated~group, data=eggdat,format="count")
 (egg.test<-xchisq.test(table.egg1))
```

## Exercise 2

QUESTION: The xchisq.test function will also let you conduct the test using a randomization procedure if you use the argument simulate.p.value = TRUE.  When would you want to use this option?

ANSWER:  
 
## Exercise 3

QUESTION: Look at the residuals and expected values for each cell.  In what way do the data deviate from what you would expect IF the Null  Hypothesis were true?

ANSWER: 
 


# ANOVA

Read in Collar Test data...

```{r}
CollarTest<-read.csv("CollarTest.csv")
```

## GPS Errors

### Exercise 1

Use dim(CollarTest) to determine how many cases are in the data dataset. Use names to determine the variable names.

```{r}



```

 

### Exercise 2

QUESTION: How many locations are there in each of the 4 cover types?

```{r}
favstats(Error~CoverType, data=CollarTest)
```

  
### Exercise 3

Construct a plot to explore the distribution of errors in each of the 4 cover types. Also, find the mean error in each cover type. 


```{r}
 


```

QUESTION: Which cover type resulted in the largest errors on average?

ANSWER:  

## Anova Assumptions

### Exercise 1

```{r}
 


```
QUESTION:  Are the sample sizes large enough in each group and/or are the data approximately normally distributed? Is the equal variability condition satisfied?

ANSWER: 



#### Exercise 2

I have also added code, below, to compare the standard deviations of the  log-transformed error values.  As rough rule of thumb, the constant variance assumption is violated if the standard deviation of one group is more than double the standard deviation of another group. 
```{r}
sd(log(Error)~CoverType, data=CollarTest)
```

QUESTION: Is the assumption likely to be met for the log-transformed values?

ANSWER:


## Anova Table and F-test

### Exercise 1

QUESTION:  State the null and alternative hypotheses for the ANOVA test.

ANSWER:  

Null Hypothesis:

Alternative Hypothesis:

### Exercise 2

Use lm to to fit the model and assign the result to a name, say: 

anova.fit<-lm(log(Error)~CoverType,data=CollarTest). 

To produce the ANOVA table, type: anova(anova.fit).

```{r}
anova.fit<-lm(log(Error)~CoverType, data=CollarTest)
anova(anova.fit)
```

QUESTION:  What is the p-value? What does this result tell you about location errors in the different habitat types?

Answer:  

## Pairwise Comparisons

### Exercise 1

Calculate the means of the log location errors within each cover type

```{r}



```
 



### Exercise 2

Use emmeans(anova.fit, list(pairwise ∼ CoverType), adjust="none") to compute all pairwise differences

```{r}



```

QUESTION:  Use the p-values associated with these pairwise tests to summarize the pairwise comparisons (as in the lab intro slides).

ANSWER:  

 
### Exercise 3

Use emmeans(anova.fit, list(pairwise ∼ CoverType), adjust="tukey") to compute
all pairwise differences.

```{r}


```

QUESTION:  Use the p-values associated with these pairwise tests to summarize the pairwise comparisons (as in the lab intro slides).

ANSWER: 


### Exercise 4


QUESTION:  What effect did adjust="tukey" have on the results?

ANSWER: 



## Inference By Simualtion

### Exercise 1


Create a bootstrap distribution of mean values for each group using the example code in the instructions. Calculate a 90% confidence interval for the mean Error value in each cover type. 

```{r}



```

### Exercise 2

Calculate a 90% confidence interval for the difference in mean Error values for EarlyBrush - HeavyBrush.

```{r}


```
