knitr::opts_chunk$set(echo = TRUE)
#important packages!
library(tidyverse)
library(car)
dataset <- read_csv("dataset_124.csv")
## 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.
SV ( Snout-vent) length
chr: tail, site
numeric: weight
Selecting or removing columns to clean your dataset
Note: a lot of your datasets will have data that you won’t use, feel free to tidy up your data and only include variables/columns of interest
#using the select function to pull specific columnsinto a NEW dataframe
new_data <- rawdata %>%
select(tail, site, SV_length, spp, weight)
str(new_data)
## tibble [4,091 × 5] (S3: tbl_df/tbl/data.frame)
## $ tail : chr [1:4091] "B" "B" "W" "W" ...
## $ site : chr [1:4091] "CALI" "CALI" "SAND" "SAND" ...
## $ SV_length: num [1:4091] 92 49 91 93 94 90 42 72 86 70 ...
## $ spp : chr [1:4091] "CNTI" "UTST" "CNTI" "CNTI" ...
## $ weight : num [1:4091] 26 6 24 25 24 22 3 9 16 9 ...
#if all of your columns are in the same range (i.e. you want to keep the first 5 columns) you can use the : operator
# new_data <- dataframe %>%
# select(column_1:column_5)
# The - operator negates a selection
# new_data <- dataframe %>%
# select(-(column_6:column_9))
#if you want to remove non-sequential columns
# new_data <- dataframe %>%
# select(-column_4, -column_8, -column_12)
Removing NAs
## code for removing N/As
clean_data <- new_data %>%
select(site, SV_length, tail, weight, spp) %>% #select all columns included in analysis
drop_na() # remove all rows with NAs
Selecting observations with a biological reason
For example, if you have a dataset looking at frog weight among species
(red-eyed treefrog, glass frog, and western toads) but your weight is
not normally distributed (even after logging) you can choose just to
include the tree frogs and remove toads from your analysis because the
toads are the least related phylogenetically, or because they live in
different habitats.
utst_data <- clean_data %>% #filter just UTST species
filter(spp == "UTST")
withoutail <- utst_data%>% # group into ones with and without tail
filter(tail == "B")
withtail <- utst_data %>%
filter(tail == "W")
[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
[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
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
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
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