knitr::opts_chunk$set(echo = TRUE)
#important packages!
library(tidyverse)
library(car)
## 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.
mass of lizard
categorical- aggression, treatment
numerical- avgbothNymph_larva
#using the select function to pull specific columnsinto a NEW dataframe
cleandata<- dataset %>%
select(Aggression, avgmass, Treatment, avgtotal) %>% #select all columns included in analysis
drop_na() # remove all rows with NAs
as.character(cleandata$Aggression)
## [1] "2" "5" "2" "2" "5" "11" "4" "5" "1" "2" "5" "3" "4" "5" "2"
## [16] "5" "1" "11" "5" "5" "11" "5" "5" "7" "5" "7" "1" "2" "5" "5"
## [31] "3" "4" "5" "3" "2" "5" "3" "2" "8" "11" "5" "5" "5" "5" "5"
## [46] "3" "11" "5" "5" "5" "2" "5" "5" "5" "5" "2" "8" "11" "3" "5"
## [61] "2" "5" "11" "2" "2" "2" "5" "5" "11" "5" "2" "8" "5" "2" "1"
## [76] "3" "2" "2" "8" "5" "5" "1" "3" "7" "11" "2" "1" "4" "2" "2"
## [91] "4" "1" "5" "5" "5" "5" "11" "3" "5" "5" "5" "11" "11" "2" "2"
## [106] "5" "5" "5" "3" "5" "2" "5" "5" "8" "3" "3" "2" "5" "11" "2"
## [121] "5" "11" "5" "5" "11" "3" "8" "2" "1" "5" "5" "5" "5" "5" "5"
## [136] "11" "5" "2" "8" "4" "7" "3" "11" "2" "4" "11" "5" "5" "3" "4"
## [151] "11" "5" "5" "5" "2" "1" "2" "2" "5" "10" "2" "2" "10" "2" "11"
## [166] "7" "11" "5" "8" "5" "2" "2" "8" "5" "2" "5" "1" "5" "4" "4"
## [181] "5" "10" "11" "2" "11" "5" "11" "2" "1" "11" "5" "5" "1" "1" "2"
## [196] "1" "2" "4" "11" "2" "3" "8" "2" "2" "2" "2" "5" "5" "3" "5"
## [211] "10" "5" "3" "11" "3" "5" "11" "11" "5" "5" "5" "5" "2" "2" "5"
## [226] "3" "5" "2" "2" "5" "7" "5" "5" "8" "5" "2" "2" "5" "5" "11"
## [241] "3" "10" "5" "5" "5" "2" "5" "1" "9" "2" "2" "5" "1" "2" "5"
## [256] "5" "5" "5" "5" "2" "2" "1" "1" "2" "3" "5" "5" "3" "11" "3"
## [271] "2" "5" "11" "2" "5" "2" "11" "2" "2" "11" "5" "1" "2" "5" "3"
## [286] "8" "5" "5" "1" "2" "5" "5" "5" "5" "4" "3" "5" "2" "1" "3"
#infestation <- cleandata%>%
# filter(Treatment == "infestation")
#no_treatment<- cleandata%>%
# filter(Treatment == "no_treatment")
[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
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
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