knitr::opts_chunk$set(echo = TRUE)

Installation

Load in common libraries used by this package.

library(ggplot2)
library(tidyverse)

Now, we install my R package:

devtools::install_github("ThomasDodson/R_package_Dodson")
library("MorphoMussel")

Let us download some data for our package test:

download.file(url= "https://raw.githubusercontent.com/ThomasDodson/R_package_Dodson/master/data/SH16_Mussels.csv", destfile = "data/SH_16Mussels.csv")

Most analyses use dataframes. Read in and save SH16Mussels to a dataframe.

SH16_Mussels <- read_csv("/cloud/project/data/SH16_Mussels.csv")

ScatterPlotDepthLength

' ScatterPlotDepthLength takes in Depth and Length variables of mussels from a dataframe and plots a scatter plot with a linear model. Example: ScatterPlotDepthLength(Mussel_data, Depth Variable, Length Variable)

' For the test data Mussel_data is the source data (SH16_Mussels), SH16_Mussels$Depth is the depth variable, and SH16_Mussels$Length is the length variable.

Plotting depth and length of mussels and analyzing the model visually allows for better understanding of the relationship between the variables. This can be used for for a variety of purposes including quantifying this relationship in different age classes of mussels and quantifying expected normal (healthy) relationships between depth and length. For example, you may expect unhealthy mussels to develop less depth in their shell. In addition, female mussels increase shell depth when gravid so examining this relationship compared to length can be used to determine what you'd expect for normal (non-gravid) depth to length ratios and outliers (gravid mussels).

The output should be a scatterplot with a linear model plotted.

ScatterPlotDepthLength <- function(Mussel_data, Depth_Variable, Length_Variable){
  enquo(Depth_Variable)
  enquo(Length_Variable)
  enquo(Mussel_data)
    Plot <- ggplot(Mussel_data, mapping= aes(x=Depth_Variable, y= Length_Variable)) + geom_point() + geom_smooth(method = "lm", color= "navy", size=0.5, fill="deeppink4") + labs(x="X_axis_label", y= "Y_axis_label", title= "Title")
    if (is.ggplot(Plot) == FALSE)
      return("Error; plot not made")
      return(Plot)
}

ScatterPlotDepthWidth

' ScatterPlotDepthWidth, like ScatterPlotDepthLength, takes in variables (depth and width in this case) of mussels from a dataframe and plots a scatter plot with a linear model. Example: ScatterPlotDepthwidth(Mussel_data, Depth Variable, Width Variable)

' For the test data Mussel_data is the source data (SH16_Mussels), SH16_Mussels$Depth is the depth variable, and SH16_Mussels$Width is the width variable.

Plotting depth and width of mussels and analyzing the model visually allows for better understanding of the relationship between the variables. This can be used for for a variety of purposes including quantifying this relationship in different age classes of mussels and quantifying expected normal (healthy) relationships between depth and width For example, you may expect unhealthy mussels to develop less depth in their shell. In addition, female mussels increase shell depth when gravid so examining this relationship compared to width can be used to determine what you'd expect for normal (non-gravid) depth to width ratios and outliers (gravid mussels).

The output should be a scatterplot with a linear model plotted.

ScatterPlotDepthWidth <- function (Mussel_data, Depth_Variable, Width_Variable){
  enquo(Depth_Variable)
  enquo(Width_Variable)
  enquo(Mussel_data)
  Plot <- ggplot(Mussel_data, mapping= aes(x=Depth_Variable, y= Width_Variable)) + geom_point() + geom_smooth(method = "lm", color= "navy", size=0.5, fill="deeppink4")
  if (is.ggplot(Plot) == FALSE)
    return("Error; plot not made")
  return(Plot)
}

LinearModelLength

LinearModelLength takes in a dataframe of mussel metrics and 2 variables from within the dataframe, one of which is meant to be length. It then generates a linear model for analysis.

' Example: LinearModelLength(Mussel_data, Mussel_data$Variable_1, Mussel_data$Variable_2)

' Example with test data examining variables width and depth:

' LinearModelLength(SH16_Mussels, SH16_Mussels$Length, SH16_Mussels$Depth)

The importance of these variables is explained in the scatter plot explanations. However, the exact statistical information is important for examining the robustness of analysis so this will give you all necessary statistical information on the linear model.

Output is the statistical analyses of the linear model.

LinearModelLength <- function(Mussel_data, Variable_1, Variable_2) {
   if (is.data.frame(LinearModelLength) == TRUE){
     return("This can not be a dataframe")
   }
   LinearModel <- lm(Variable_1 ~ Variable_2, data=Mussel_data)
    return(LinearModel)
  }

CleanNAs

CleanNAs is a function designed to ensure all NAs are dropped from a dataset to ensure proper use of the package.

Example: CleanNAs(dataframe) Where the dataframe in the test is SH16_Mussels.

NAs can cause some functions to work improperly so it is important to remove them before analysis.

Output is a dataframe (free of NAs).

CleanNAs <- function(Dataframe) {
  Dataframe <- drop_na(Dataframe)
  if (is.data.frame(Dataframe) == FALSE){
    return("This must be a dataframe")
  }
}

ExcavatedTtest

Excavated Ttest is a function that takes in 2 separate lists of variables and runs a student's T-test for statistical significance. In this case it was designed to examine statistical significane between sizes of excavated (buried) and non-excavated (surface) mussel individuals.

Example: ExcavatedTtest(Nonexcavated_list, Excavated_list) where Nonexcavated mussels is a list of mussel lengths of nonexcavated mussels and Excavated mussels is a list of lengths of excavated mussels.

Note that for the test data, two separate lists must be created through filters. Example: Nonexcavated_mussels <- filter(SH16_Mussels, Excavated == "N") %>% + select(Length)

Excavated_mussels <- filter(SH16_Mussels, Excavated == "Y") %>% + select(Length)

Output is the result of the Student's T-test.

 ExcavatedTtest <- function(Nonexcavated_list, Excavated_list) {
   Dataframe <- t.test(Nonexcavated_list, Excavated_list)
   if (is.data.frame(Nonexcavated_list) == FALSE){
     return("This must be a dataframe")
   }
   return(Dataframe)
 }


ThomasDodson/R_package_Dodson documentation built on Dec. 18, 2021, 4:13 p.m.