knitr::opts_chunk$set(echo = TRUE)

#important packages! 
library(tidyverse)
library(car)
dataset <- read_csv("dataset_124.csv")

Part 1: Select and understand your data

Part 1 Questions

1. Upload a csv file of the dataset you are going to use to the Final Project Check-in portal on Canvas. Also load the dataset into R in the code chunk below to use in your analysis.

## Load in your data here
ta_dataset <- read_csv("dataset_124.csv")
## Rows: 300 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (1): Treatment
## dbl  (13): Lizard, year, Aggression, Boldness, sex, avgmass, trialnum, ticks...
## date  (2): date, date_infected
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
rawdata <- read_csv("JRN007001_lizard_pitfall_data_89-06.csv")
## Rows: 4091 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (7): zone, site, plot, spp, sex, rcap, tail
## dbl  (6): pit, toe_num, SV_length, total_length, weight, pc
## date (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

2. Briefly describe this dataset in 1-2 sentences. Who is the author/creator of this dataset? How, when and why were these data collected?

This dataset was created by David Lightfoot and Walter Whitford. It contains Lizard pitfall live trap data from 11 NPP study locations checked weekly for 2 weeks four a year (February/March, May/June), August, October/November). This study took place at the Jornada Basin LTER site from the years 2016 to 2017 The data was collected to observe health of lizards in the area.

3. What is your outcome (y) of interest in this dataset? This should be your numeric, (ideally) normally distributed y variable.

SV ( Snout-vent) length

4. What are your inputs (x) of interest in this dataset?

chr: tail, site

numeric: weight


Part 2: Explore and visualize data

Part 2 Questions

1. Is your untransformed y variable normally distributed? Show a histogram, qqPlot, and shapiro wilks test.

[your answer: The untransformed y data is ____ .]

# Histogram of y-variable
wtail_hist<- ggplot(data= withtail, aes(x = SV_length)) +
  geom_histogram(fill = "darkgreen", border = "grey")+
  theme_bw()+
  labs(x= "SV Length", y= "Frequency")+
  theme((axis.title= element_text(face="bold", size = 12))) #rv
## Warning in geom_histogram(fill = "darkgreen", border = "grey"): Ignoring
## unknown parameters: `border`
wotail_hist<- ggplot(data= withoutail, aes(x = SV_length)) +
  geom_histogram(fill = "darkgreen", border = "grey")+
  theme_bw()+
  labs(x= "SV Length", y= "Frequency")+
  theme((axis.title= element_text(face="bold", size = 12))) #rv
## Warning in geom_histogram(fill = "darkgreen", border = "grey"): Ignoring
## unknown parameters: `border`
wtail_hist
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

wotail_hist
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#doesnt look horrible actually....

# qqPlot of y-variable
qqPlot(withtail$SV_length)

## [1] 1178  237
qqPlot(withoutail$SV_length)

## [1] 361 117
#could be worse but a lot is still def out of bounds


# shapiro-wilks test of y-variable
shapiro.test(withtail$SV_length)
## 
##  Shapiro-Wilk normality test
## 
## data:  withtail$SV_length
## W = 0.97058, p-value = 1.014e-15
shapiro.test(withoutail$SV_length)
## 
##  Shapiro-Wilk normality test
## 
## data:  withoutail$SV_length
## W = 0.94236, p-value = 2.815e-11
#both have p<0.05, indicated the data is NOT normally distributed 

2. If your y-variable is NOT normal, try 2 transformations to make it normal and state if either transformation made the data more normal, and if so, which transformation will you use?

[your answer: After a log transformation, the transformed y data is NOT normal. After a square root transformation, the transformed y data is NOT normal.]

Transformation 1

## Hint: Check if there are 0s or negatives using range() before transforming
min(withtail$SV_length) # not zeros!
## [1] 19
min(withoutail$SV_length)
## [1] 5
withtail$log_SV <- log(withtail$SV_length) #log transformation
withoutail$log_SV <- log(withoutail$SV_length)


## Histogram of transformed y-variable
logwtail_hist<- ggplot(data= withtail, aes(x = log_SV)) +
  geom_histogram(fill = "darkgreen", border = "grey")+
  theme_bw()+
  labs(x= "Log SV Length", y= "Frequency")+
  theme((axis.title= element_text(face="bold", size = 12))) #rv
## Warning in geom_histogram(fill = "darkgreen", border = "grey"): Ignoring
## unknown parameters: `border`
logwotail_hist<- ggplot(data= withoutail, aes(x = log_SV)) +
  geom_histogram(fill = "darkgreen", border = "grey")+
  theme_bw()+
  labs(x= "Log SV Length", y= "Frequency")+
  theme((axis.title= element_text(face="bold", size = 12))) #rv
## Warning in geom_histogram(fill = "darkgreen", border = "grey"): Ignoring
## unknown parameters: `border`
logwtail_hist
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

logwotail_hist #looks worse....
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#doesnt look horrible actually....

# qqPlot of y-variable
qqPlot(withtail$log_SV)

## [1] 1178   62
qqPlot(withoutail$log_SV)

## [1] 361  34
#still not v normal...


## shapiro-wilks test of transformed y-variable
shapiro.test(withtail$log_SV)
## 
##  Shapiro-Wilk normality test
## 
## data:  withtail$log_SV
## W = 0.94122, p-value < 2.2e-16
shapiro.test(withoutail$log_SV) #both STILL not normal!
## 
##  Shapiro-Wilk normality test
## 
## data:  withoutail$log_SV
## W = 0.81697, p-value < 2.2e-16

Transformation 2

#####SQRT TRANSFORM
withtail$sqrt_SV <- sqrt(withtail$SV_length) #sqrt transformation
withoutail$sqrt_SV <-sqrt(withoutail$SV_length)

sqrtwtail_hist<- ggplot(data= withtail, aes(x = sqrt_SV)) +
  geom_histogram(fill = "darkgreen", border = "grey")+
  theme_bw()+
  labs(x= "Log SV Length", y= "Frequency")+
  theme((axis.title= element_text(face="bold", size = 12))) #rv
## Warning in geom_histogram(fill = "darkgreen", border = "grey"): Ignoring
## unknown parameters: `border`
sqrtwotail_hist<- ggplot(data= withoutail, aes(x = sqrt_SV)) +
  geom_histogram(fill = "darkgreen", border = "grey")+
  theme_bw()+
  labs(x= "Log SV Length", y= "Frequency")+
  theme((axis.title= element_text(face="bold", size = 12))) #rv
## Warning in geom_histogram(fill = "darkgreen", border = "grey"): Ignoring
## unknown parameters: `border`
sqrtwtail_hist
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

sqrtwotail_hist #hmmm....
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# qqPlot of y-variable
qqPlot(withtail$sqrt_SV)

## [1] 1178   62
qqPlot(withoutail$sqrt_SV)

## [1] 361 117
#still not normal uhh...


## shapiro-wilks test of transformed y-variable
shapiro.test(withtail$sqrt_SV)
## 
##  Shapiro-Wilk normality test
## 
## data:  withtail$sqrt_SV
## W = 0.96048, p-value < 2.2e-16
shapiro.test(withoutail$sqrt_SV) #better but STILL not normal!
## 
##  Shapiro-Wilk normality test
## 
## data:  withoutail$sqrt_SV
## W = 0.90267, p-value = 3.12e-15

3. Plot your y (numeric) ~ x (numeric) relationship.

Use your transformed y-variable data here if you chose to use it in step 2

wtail_scatter<- ggplot(data= withtail, aes(x= weight, y = SV_length)) +
  geom_point()+
  theme_bw()+
  labs(x= "Weight", y= "SV Length")+
  theme((axis.title= element_text(face="bold", size = 12))) 

wotail_scatter<- ggplot(data= withoutail, aes(x= weight, y = SV_length)) +
  geom_point()+
  theme_bw()+
  labs(x= "Weight", y= "SV Length")+
  theme((axis.title= element_text(face="bold", size = 12))) 


wtail_scatter

wotail_scatter

4A. Plot your first y (numeric) ~ x (categorical) relationship.

Use your transformed y-variable data here if you chose to use it in step 2

##
wboxplot<- ggplot(data = withtail, aes(x = site, y = SV_length))+ #outcome of interest ALWAYS on Y axis
  geom_boxplot(fill = "#7bbfc9")+ #remember that 1 categorical factor/chr = 1boxplot
  theme_classic()+
  labs(x= "Sight", y = "SV Length")+
  theme(axis.title = element_text(size = 12, face= "bold"))


woutboxplot<- ggplot(data = withoutail, aes(x = site, y = SV_length))+ #outcome of interest ALWAYS on Y axis
  geom_boxplot(fill = "#7bbfc9")+ 
  theme_classic()+
  labs(x= "Sight", y = "SV Length")+
  theme(axis.title = element_text(size = 12, face= "bold"))

wboxplot

woutboxplot #appears to be a large diff btw those w and wo tail, though not much variabtion between those of different sites

4B. Plot your second y (numeric) ~ x (categorical) relationship.

Use your transformed y-variable data here if you chose to use it in step 2

tailboxplot<- ggplot(data = utst_data, aes(x = tail, y = SV_length))+ #outcome of interest ALWAYS on Y axis
  geom_boxplot(fill = "#7bbfc9")+ #remember that 1 categorical factor/chr = 1boxplot
  theme_classic()+
  labs(x= "Tail Status", y = "SV Length")+
  theme(axis.title = element_text(size = 12, face= "bold"))




tailboxplot #some variation