---
title: "Tick trapping - An observational study incorporating experimental design"
author: "Duncan Golicher"
date: "3/6/2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Introduction

The presence of ticks in grassland and heathland is an important public health issue in many reserves in the South of England.

A variety of methods are available to trap ticks. A useful summary (used in Africa, but relevant for any area with ticks) is available here.

http://www.afrivip.org/sites/default/files/Ticks_surveillance/free_living.html

You have been given the task by Dorset wildlife trust of designing a tick monitoring program run initially over two years (but potentially extended into the future) to establish the effectiveness of deer culling as a means of reducing tick density. Culling is going to take place at the end of the first year. This will affect the deer population throughout the Purbeck area, resulting in an 80% fall in deer numbers. DWT wish to select one out of three different trapping techniques to be used in a consistent long term study based on a consideration of cost and efficiency. You are working with a group of ten volounteers at five sites within Purbeck. Your trial will take place during the year prior to the cull and the year following the cull.

At each site there are two broad vegetation classes. Grassland and heathland.

Design a study to establish the effectiveness of both the deer cull and the effectiveness of the trapping techniques that will be used in the monitoring.

Advice: This is a potentially complex analysis which can be approached in a variety of different ways. There will be elements of sub-sampling and blocking involved. It is also a repeat measures design. Think carefully about how the principles of experimental design and robust statistical analysis involving fixed and random effects can be used to address the questions.  There is also a spatial element involved, which you can ignore for the purposes of this statistical design, but should consider when discussing the study.

## Suggested solution

Martin suggested a repeat measures design with paired trapping methods within subplots. Each of the five sites were split into two habitat types. Within each habitat type four subplots were established through some form of stratified sampling in order to maintain a minimum distance between them. One trap of each type (two types used in this example) were placed close togther within each subplot.

This led to a total of *4*  x 2 x 5 = 40 subplots and *8* x 2 x 5 = 80 traps each year.

To simplify things a little let's define the response as the total number of ticks recorded in each trap summed over all the visits to the traps. The true raw data would of course have even more replication due to temporal sub sampling, which could be simulated, but we'll leave that out to keep things within reason.

## Simulate year

Each year repeated 80 times. 

```{r}
year<-rep(c("2018","2019"),each=80)
```


## Simulate traps and subplots

Code the subplots 40 times and repeat each of these codes twice, as two traps are placed within them. Then need repeat the whole design twice, once for each year.

Code the traps twice and repeat 40 times (for each subplot).
Again, repeat the whole vector twice, once for each year.


```{r}
subplot_id<-rep(paste("p",1:40,sep="_"),each=2)
subplot_id<- rep(subplot_id,times=2)


trap_id<-rep(paste("1",1:80,sep="_"),times=2)


trap_type<-rep(c("A","B"),times=40)
trap_type<-rep(trap_type,times=2)



```

Check the design by scanning the data frame.

```{r}
d<-data.frame(year,subplot_id,trap_id,trap_type)
DT::datatable(d)
```


## Simulate site and vegetation type 

We've got 5 sites with 16 traps within each one.

Again repeat the whole design for each year.

```{r}
site_id<-rep(paste("s",1:5,sep="_"),each=16)
site_id<-rep(site_id,times=2)

veg_type<-rep(rep(c("Heath","Grass"),each=8),times=5)
veg_type<-rep(veg_type,times=2)

d<-data.frame(d,site_id,veg_type)
DT::datatable(d)
```


## Effects

Let's make the effects simply additive and use a poisson distribution again to simulate tick counts.


```{r}
set.seed(20)
year_effect<-rep(c(10,3),each=80)

subplot_effect<-rep(rnorm(40,mean=0,sd=4),each=2) ## This can go negative
subplot_effect<-rep(subplot_effect,times=2)
trap_effect<-rep(c(3,8),times=40)
trap_effect<-rep(trap_effect,times=2)

site_effect<-rep(rnorm(5,mean=2,sd=4),each=16)
site_effect<-rep(site_effect,times=2)

veg_effect<-rep(rep(c(5,10),each=8),times=5)
veg_effect<-rep(veg_effect,times=2)

mean_ticks<-year_effect+subplot_effect+trap_effect+site_effect+veg_effect

tick_count<-unlist(lapply(mean_ticks,function(x)rpois(1,x)))

```

## Make a data frame

```{r}
d<-data.frame(d,tick_count)
```

## Analysis

Now summarise using dplyr and run appropriate linear mixed effects models.

