knitr::opts_chunk$set(echo = TRUE)

#important packages! 
library(tidyverse)
library(car)

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
dataset <- read_csv("dataset_123.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.

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

mass of lizard

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

categorical- aggression, treatment

numerical- avgbothNymph_larva


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 NOT normal .]

# Histogram of y-variable
mass_hist<- ggplot(data= cleandata, aes(x = avgmass)) + #checking normality visually of avg mass of lizard
  geom_histogram(fill = "darkgreen", border = "grey")+ #picking colors- green bc lizards!
  theme_bw()+ #simple theme
  labs(x= "Average Mass", y= "Frequency")+ #axis lables
  theme((axis.title= element_text(face="bold", size = 12))) #font size
## Warning in geom_histogram(fill = "darkgreen", border = "grey"): Ignoring
## unknown parameters: `border`
mass_hist #looks fairly normal but has some dips
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# qqPlot of y-variable
qqPlot(cleandata$avgmass) #almost all points fall in boundary line!

## [1] 255 295
# shapiro-wilks test of y-variable
shapiro.test(cleandata$avgmass) #P is less than 0.05 (alpha), must reject the null that the data is normally distributed
## 
##  Shapiro-Wilk normality test
## 
## data:  cleandata$avgmass
## W = 0.98855, p-value = 0.01819

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?

Data NOT normal after either transformation… will stick with origonal data and use nonparametric tests

Transformation 1

## TRANSFORMATIONS
min(cleandata$avgmass) # not zeros! good to take log
## [1] 445
cleandata$log_avgmass <- log(cleandata$avgmass) #log transformation
## Transformation 1 code 
logmass_hist<- ggplot(data= cleandata, aes(x = log_avgmass)) + #checking normality visually of avg mass of lizard
  geom_histogram(fill = "darkgreen", border = "grey")+ #picking colors- green bc lizards!
  theme_bw()+ #simple theme
  labs(x= "Log Average Mass", y= "Frequency")+ #axis lables
  theme((axis.title= element_text(face="bold", size = 12))) #font size
## Warning in geom_histogram(fill = "darkgreen", border = "grey"): Ignoring
## unknown parameters: `border`
logmass_hist #looks like data is now skewed right... less normal than before
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qqPlot(cleandata$log_avgmass)#definitely some points out of bounds

## [1] 255 295
shapiro.test(cleandata$log_avgmass) #NOPE! still not normal p<0.05
## 
##  Shapiro-Wilk normality test
## 
## data:  cleandata$log_avgmass
## W = 0.96993, p-value = 6.507e-06

Transformation 2

## Transformation 2 code 
cleandata$sqrt_avgmass <- sqrt(cleandata$avgmass) #log transformation
## Transformation 1 code 
sqrtmass_hist<- ggplot(data= cleandata, aes(x = sqrt_avgmass)) + #checking normality visually of avg mass of lizard
  geom_histogram(fill = "darkgreen", border = "grey")+ #picking colors- green bc lizards!
  theme_bw()+ #simple theme
  labs(x= "Square Root Average Mass", y= "Frequency")+ #axis lables
  theme((axis.title= element_text(face="bold", size = 12))) #font size
## Warning in geom_histogram(fill = "darkgreen", border = "grey"): Ignoring
## unknown parameters: `border`
sqrtmass_hist #also looks like data is now skewed right... less normal than before
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qqPlot(cleandata$sqrt_avgmass)#definitely some points out of bounds

## [1] 255 295
shapiro.test(cleandata$sqrt_avgmass) #NOPE! still not normal p<0.05
## 
##  Shapiro-Wilk normality test
## 
## data:  cleandata$sqrt_avgmass
## W = 0.98102, p-value = 0.0005206

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

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

## plot y~x(numeric)
numscatter <-ggplot(data= cleandata, aes(x= avgtotal, y = avgmass)) +
  geom_point()+
  theme_bw()+
  labs(x= "Average Total Ticks", y= "Average Mass")+
  theme(axis.title= element_text(face="bold", size = 12)) 

numscatter

###plot y~x(categorical)
agro_scatter<-ggplot(data= cleandata, aes(x= Aggression, y = avgmass)) +
  geom_boxplot(fill = "#7bbfc9")+
  aes(group = Aggression)+
  theme_bw()+
  labs(x= "Aggression Score", y= "Average Mass")+
  theme(axis.title= element_text(face="bold", size = 12)) 

agro_scatter

###plot y~x(categorical)
treat_scatter<-ggplot(data= cleandata, aes(x= Treatment, y = avgmass)) +
  geom_boxplot(fill = "#7bbfc9")+
  theme_bw()+
  labs(x= "Treatment", y= "Average Mass")+
  theme(axis.title= element_text(face="bold", size = 12)) 

treat_scatter