# Please use the Lab 1 tutorial located here:
# https://bookdown.org/djc426/behaviouR-R-package-tutorials/
# Hello and welcome to your first R session! Remember any lines that include a #
# symbol will not be read by R, but provides information to the user (you!)
# First we load the relevant packages
library(behaviouR)
library(ggpubr)
library(ggfortify)
## Lab 1a. Categorical data Here we create a simulated population with four
## categories (Infant, Juvenile, AdultFemale and AdultMale)
DeerPopulationDF <- data.frame(DevelopmentStage = c("Infant", "Juvenile", "AdultFemale",
"AdultMale"), NumberOfIndividuals = c(15, 50, 125, 200))
# We then print the object so that we can see the output
DeerPopulationDF
# Now we want to plot the data. We will use a simple barplot to start.
ggbarplot(DeerPopulationDF, x = "DevelopmentStage", y = "NumberOfIndividuals")
## Lab 1b. Categorical and continuous data Load the dataset so that we can use it
data("MaleDeerRoarDF")
# We can check the structure of the dataframe by using the command 'head'
head(MaleDeerRoarDF)
# Now we will plot the categorical data with standard deviations.
ggbarplot(MaleDeerRoarDF, x = "MaleCategory", y = "RoarsPerMinute", add = c("mean_sd"))
# Now we will plot the categorical data with colors for each category.
ggbarplot(MaleDeerRoarDF, x = "MaleCategory", y = "RoarsPerMinute", fill = "MaleCategory",
add = c("mean_sd")) + theme(legend.position = "none")
# Now we will plot the categorical data with user-specified colors for each
# category.
ggbarplot(MaleDeerRoarDF, x = "MaleCategory", y = "RoarsPerMinute", fill = "MaleCategory",
palette = c("red", "gray", "white", "black"), add = c("mean_se")) + theme(legend.position = "none")
## Lab 1c. Categorical and continuous data We will simulate a dataset for males of
## different weight and harem size.
# The function below simulates our data. N is the number of individuals,
# CorrelationCoefficient tells us how correlated our data are, MaleMeanBodyWeight
# is the mean body weight of males in our population and MaleReproductiveSuccess
# is the mean number of females in the harem.
MaleRedDeerDF <- CorrelatedDataSimulationFunction(N = 100, CorrelationCoefficient = 0.45,
MaleMeanBodyWeight = 125, MaleReproductiveSuccess = 3)
# We can check the output
head(MaleRedDeerDF)
# Make a scatterplot of the data.
ggscatter(data = MaleRedDeerDF, x = "MaleBodyWeight", y = "MaleReproductiveSuccess")
# Make a scatterplot with a trendline.
ggscatter(data = MaleRedDeerDF, x = "MaleBodyWeight", y = "MaleReproductiveSuccess",
add = "reg.line")
# Create a linear model where MaleBodyWeight is the independent variable and
# ReproductiveSuccess is the dependent variable.
MaleDeerModel <- lm(MaleReproductiveSuccess ~ MaleBodyWeight, data = MaleRedDeerDF)
# We can look at the output of the model
MaleDeerModel
# Create a null model and a model with MaleBodyWeight as a predictor.
MaleDeerNull <- lm(MaleReproductiveSuccess ~ 1, data = MaleRedDeerDF)
MaleDeerModel <- lm(MaleReproductiveSuccess ~ MaleBodyWeight, data = MaleRedDeerDF)
# Compare models using AIC.
bbmle::AICctab(MaleDeerModel, MaleDeerNull, weights = T)
## Lab 1d. Multivariate data Just as before we load our data
data("DeerSpeciesAcousticFeatures")
# Check the structure
head(DeerSpeciesAcousticFeatures)
# Here is our modified dataset that we will use for PCA
DeerSpeciesAcousticFeatures[, -c(4)]
# Run the PCA using the 'princomp' function
DeerSpeciesAcousticFeaturesPCA <- princomp(DeerSpeciesAcousticFeatures[, -c(4)])
# Plot the results of our PCA.
ggplot2::autoplot(DeerSpeciesAcousticFeaturesPCA, data = DeerSpeciesAcousticFeatures,
colour = "Class", loadings = FALSE) + theme_bw()
# Plot the PCA with arrows indicating which features are important for
# distinguishing between groups
ggplot2::autoplot(DeerSpeciesAcousticFeaturesPCA, data = DeerSpeciesAcousticFeatures,
colour = "Class", loadings = TRUE, loadings.colour = "red", loadings.label = TRUE,
loadings.label.size = 5) + theme_bw()