For this exam you will be using the file “tick.csv” found on canvas:

Ticks use large mammals as hosts and the presence of mammals represents an important vector for tick-borne diseases and humans. Tick presence may also be influenced by environmental factors. The tick.csv file has a number of different variables that can be analyzed, below are a list of the important variables for this study:

Treatment - what type of animals were allowed within the plot? CONT (control, no exclosure), LMH (total exclosure, no mammals), MESO (exclosion of large herbivores >15kg), and MEGA (exclusion of Elephants and Giraffes)

Rain - average rainfall (mm/year)

Total - total number of ticks found across 3 study species

PART 1a: How does treatment of mammals influence the total number of ticks found? Answer the questions below:

NOTE: for each prompt, I need to see code in order to give you credit! NOTE: you can perform all of these tasks in one code chunk or many code chunks, this is up to you

  1. Code: bring in the dataset and name it “tick”
tick <- read.csv("~/Desktop/BIN510-files/ticks.csv")
head(tick)
##   Period       Date Month Level Replicate Treatment Total RHPU RHPR RHPV Rain
## 1     11 20/08/2014   Aug     2         4      CONT     1    0    0    1  580
## 2     11 20/08/2014   Aug     2         5      CONT     1    0    0    1  580
## 3     11 20/08/2014   Aug     2         6      CONT     1    0    0    1  580
## 4     11 21/08/2014   Aug     3         7      CONT     0    0    0    0  440
## 5     11 21/08/2014   Aug     3         8      CONT     0    0    0    0  440
## 6     11 21/08/2014   Aug     3         9      CONT     5    4    0    1  440
str(tick)
## 'data.frame':    180 obs. of  11 variables:
##  $ Period   : num  11 11 11 11 11 11 11 11 11 11 ...
##  $ Date     : chr  "20/08/2014" "20/08/2014" "20/08/2014" "21/08/2014" ...
##  $ Month    : chr  "Aug" "Aug" "Aug" "Aug" ...
##  $ Level    : int  2 2 2 3 3 3 1 1 1 2 ...
##  $ Replicate: int  4 5 6 7 8 9 1 2 3 4 ...
##  $ Treatment: chr  "CONT" "CONT" "CONT" "CONT" ...
##  $ Total    : int  1 1 1 0 0 5 4 0 1 37 ...
##  $ RHPU     : int  0 0 0 0 0 4 3 0 0 0 ...
##  $ RHPR     : int  0 0 0 0 0 0 0 0 0 1 ...
##  $ RHPV     : int  1 1 1 0 0 1 1 0 1 36 ...
##  $ Rain     : int  580 580 580 440 440 440 640 640 640 580 ...
  1. Question: what type of test would be appropriate to run in order to compare total ticks by treatment type? Why? Answer: A one-way ANOVA would be good for this, because we are comparing a mean number(of ticks) amoung 4 different groups.
  2. Question: what assumptions need to be tested before running the test? Answer: Normality, Equal Variance, and Independence.
  3. Code: test all assumptions from 3
boxplot(Total ~ Treatment,
        data = tick,
        main = "Total Ticks & Treatment",
        xlab = "Treatment",
        ylab = "Total Ticks",
        col= c("lightblue","blue","darkblue","royalblue"))

by(tick$Total, tick$Treatment, shapiro.test)
## tick$Treatment: CONT
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.77549, p-value = 7.169e-07
## 
## ------------------------------------------------------------ 
## tick$Treatment: LMH
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.73066, p-value = 9.644e-08
## 
## ------------------------------------------------------------ 
## tick$Treatment: MEGA
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.902, p-value = 0.001094
## 
## ------------------------------------------------------------ 
## tick$Treatment: MESO
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.81293, p-value = 4.633e-06
library(car)
## Warning: package 'car' was built under R version 4.4.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.4.3
leveneTest(Total ~ Treatment, data = tick)
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value    Pr(>F)    
## group   3   7.506 9.346e-05 ***
##       176                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  1. Question: interpret the results of 4. (tell me if assumptions are met or not) Answer: The assumptions were not met, The Shapiro-Wilk tests showed that the data were not normall distributed since the p-value was lower than 0.05. Levene’s test showed that the variances were not equal.

  2. Code: Regardless of your answer to 5*, run the test you propose in 2

tick_aov <- aov(Total ~ Treatment, data = tick)
summary(tick_aov)
##              Df Sum Sq Mean Sq F value   Pr(>F)    
## Treatment     3   3387  1128.9    8.67 2.13e-05 ***
## Residuals   176  22915   130.2                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  1. Question: what does the output of 6 tell you about ticks and treatment? Answer: The ANOVA showed a significant difference in the number of ticks amoung the 4 groups. The p-value was less than 0.05, which means that treatment type has an effect on number of ticks.
  2. Question: do you need to run post-hoc analysis? Why or why not? Answer: Yes, a post-hoc test would be needed since the ANOVA was proven to be significant. This will show which groups are significant.
  3. Code: if you answered YES to 8, run that analysis here
TukeyHSD(tick_aov)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Total ~ Treatment, data = tick)
## 
## $Treatment
##                diff         lwr       upr     p adj
## LMH-CONT  11.844444   5.6050266 18.083862 0.0000115
## MEGA-CONT  3.177778  -3.0616401  9.417196 0.5508095
## MESO-CONT  5.355556  -0.8838623 11.594973 0.1201486
## MEGA-LMH  -8.666667 -14.9060845 -2.427249 0.0022968
## MESO-LMH  -6.488889 -12.7283067 -0.249471 0.0380749
## MESO-MEGA  2.177778  -4.0616401  8.417196 0.8020299
  1. Question: if you ran 9, tell me what the output tells us about the treatments Answer: The post-hoc test showed significant differences between LMH and CONT, LMH and MEGA, and LMH and MESO. This shows that LMH had a significantly different total number of ticks compared with the other treatments.
  2. Create a visual that helps tell the story of how treatment influences ticks
boxplot(Total ~ Treatment,
        data = tick ,
        main = "Total Number of Ticks by Treatment",
        xlab = "Treatment",
        ylab = "Total Number of Ticks",
        col= c("red","pink","hotpink","darkred"))

PART 1b: violations of assumptions. Answer the questions below:

  1. Code: choose a transformation method of your choice and apply it to the “total” column
tick$logTotal <- log1p(tick$Total)
by(tick$logTotal, tick$Treatment, shapiro.test)
## tick$Treatment: CONT
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.96219, p-value = 0.1481
## 
## ------------------------------------------------------------ 
## tick$Treatment: LMH
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.96636, p-value = 0.2126
## 
## ------------------------------------------------------------ 
## tick$Treatment: MEGA
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.96382, p-value = 0.1707
## 
## ------------------------------------------------------------ 
## tick$Treatment: MESO
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.97739, p-value = 0.5179
leveneTest(logTotal ~ Treatment, data = tick)
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)  
## group   3  2.3584 0.0733 .
##       176                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  1. Code: re-test the assumptions from Part 1a Q4
by(tick$logTotal, tick$Treatment, shapiro.test)
## tick$Treatment: CONT
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.96219, p-value = 0.1481
## 
## ------------------------------------------------------------ 
## tick$Treatment: LMH
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.96636, p-value = 0.2126
## 
## ------------------------------------------------------------ 
## tick$Treatment: MEGA
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.96382, p-value = 0.1707
## 
## ------------------------------------------------------------ 
## tick$Treatment: MESO
## 
##  Shapiro-Wilk normality test
## 
## data:  dd[x, ]
## W = 0.97739, p-value = 0.5179
leveneTest(logTotal ~ Treatment, data = tick)
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)  
## group   3  2.3584 0.0733 .
##       176                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  1. Question: are the assumptions met? Answer: After the log transformation, the assumptions were met. The Shapiro-Wilk tests had p-values greater than 0.05 for all treatment groups. Which meant that the data was normally distributed. Levene’s test was also greater than 0.05 (p = 0.0733). Which meant that the variances were equal.

  2. Code: Run the alternative non-parametric test appropriate for this data

kruskal.test(Total ~ Treatment, data = tick)
## 
##  Kruskal-Wallis rank sum test
## 
## data:  Total by Treatment
## Kruskal-Wallis chi-squared = 23.869, df = 3, p-value = 2.66e-05
  1. Question: what does the output of 4 tell us about ticks and treatment? Answer: The Kruskal-Wallis test showed a significant difference in the total number of ticks amoung the treatments.This means that the treatments are associated with differences in the total number of ticks found.

PART 2: does environment influence ticks?

  1. Question: what type of test would be appropriate to run in order to compare total ticks by rain? Why? Answer: A linear regression would be best because both are numerical variables and we want to see if one effects the others’ outcome.
  2. Code: run the test
tick2 <- lm(Total ~ Rain, data = tick)
summary(tick2)
## 
## Call:
## lm(formula = Total ~ Rain, data = tick)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -12.471  -6.593  -2.959   2.163  76.041 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 23.50781    5.95874   3.945 0.000115 ***
## Rain        -0.02508    0.01065  -2.356 0.019565 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11.97 on 178 degrees of freedom
## Multiple R-squared:  0.03024,    Adjusted R-squared:  0.02479 
## F-statistic:  5.55 on 1 and 178 DF,  p-value: 0.01957
  1. Question: using the output from 2, tell me what the model says about ticks and rainfall. Include in your answer an interpretation of the p-value and an interpretation of the R^2 value Answer: The regression model showed that rainfall was significant to the total number of ticks p = 0.0196. The R2 value was 0.03024, which means that rainfall explains approximately 3.02% of the variation in the total number of ticks.

  2. Code: create a visual that shows the relationship between rainfall and ticks

tick_plot <- lm(Total ~ Rain, data = tick)
plot(tick$Rain,
     tick$Total,
     main = "Rainfall versus Total Number of Ticks",
     xlab = "Mean Rainfall (mm/year)",
     ylab = "Total Number of Ticks")

  1. Code: add a best fit line to help visualize the pattern
tick_plot <- lm(Total ~ Rain, data = tick)
plot(tick$Rain,
     tick$Total,
     main = "Rainfall vs. Total Number of Ticks",
     xlab = "Mean Rainfall (mm/year)",
     ylab = "Total Number of Ticks")

abline(tick_plot, col = "blue")

  1. Question: Do ticks prefer wet or dry climates? Answer: Since there is a negative relationship between rainfall and total ticks, the data suggests that ticks are more common in drier climates.

  2. Code: predict the number of ticks observed in a region that experiences 500 mm/year mean rainfall

predict(tick_plot, newdata = data.frame(Rain = 500))
##        1 
## 10.96561