<file_sep>#' @title DDT Data Set
#'
#' @description A data set collected by the US forces from the Tennessee River and its tributaries
#'
#' @format a list with 6 variables, which are:
#' \describe{
#' \item{MILE}{ the mile of the river at which the fish was taken}
#' \item{LENGTH}{ the length of the wish}
#' \item{WEIGHT}{ the weight of the fish}
#' \item{DDT}{ the DDT concentration in the fish}
#' \item{RIVER}{ the river from which the fish was sampled}
#' \item{SPECIES}{ the species of fish}
#' }
"ddt"
<file_sep>#' Shiny Plotting Function
#'
#' @param plotType the type of plot to be made
#' @param mile whether or not to include the MILE variable
#' @param length whether or not to include the LENGTH variable
#' @param weight whether or not to include the WEIGHT variable
#' @param ddt whether or not to include the DDT variable
#' @param FCM whether or not to include the FCM River
#' @param LCM whether or not to include the LCM River
#' @param SCM whether or not to include the SCM River
#' @param TRM whether or not to include the TRM River
#' @param catfish whether or not to include the CCATFISH Species
#' @param buffalo whether or not to include the SMBUFFALO Species
#' @param bass whether or not to includ ethe LMBASS Species
#' @param pcaType whether to use the covariance or correlation matrix for PCA
#' @param col variable to use to differentiate points in scatterplot
#'
#' @return a plot of data
#' @export
#'
#' @examples plotter("scatterplot",length=FALSE,buffalo=FALSE)
plotter=function(plotType="screeplot",mile=TRUE,length=TRUE,weight=TRUE,ddt=TRUE,
                 FCM=TRUE,LCM=TRUE,SCM=TRUE,TRM=TRUE,
                 catfish=TRUE,buffalo=TRUE,bass=TRUE,
                 pcaType="covariance",col="RIVER"){

  # Exit if not enough data is being used to conduct analysis.

  # For inadequate PCA variables
  if (mile+length+weight+ddt<2){
    stop("At least quantiative variables must be input.")
  }

  # Simplify descriptions of plots if needed.
  if (plotType=="scatterplot (1st two variables)"){
    plotType="scatterplot"
  }
  if (plotType=="boxplot (1st variable)"){
    plotType="boxplot"
  }
  if (plotType=="connected dot plot (1st two variables)"){
    plotType="connected"
  }

  # Determine what will be needed from the constructor.

  if (pcaType=="covariance"){
    cor=FALSE
  }
  else {
    cor=TRUE
  }

  # Create the inf object.
  infObj=const(plotType,cor,mile,length,weight,ddt,FCM,LCM,SCM,TRM,catfish,
               buffalo,bass,swap)

  # Plotting function for inf objects
  plot.inf=function(obj,type="scatterplot",col="RIVER",
                    prop=FALSE,cumProp=FALSE,eig=FALSE,screeVar=FALSE){

    if (plotType=="scatterplot"){
      dat=obj$dat

      lab=c(length=2)
      if (colnames(dat)[1]=="MILE"){
        lab[1]="Mile"
      }
      if (colnames(dat)[2]=="MILE"){
        lab[2]="Mile"
      }
      if (colnames(dat)[1]=="LENGTH"){
        lab[1]="Length"
      }
      if (colnames(dat)[2]=="LENGTH"){
        lab[2]="Length"
      }
      if (colnames(dat)[1]=="WEIGHT"){
        lab[1]="Weight"
      }
      if (colnames(dat)[2]=="WEIGHT"){
        lab[2]="Weight"
      }
      if (colnames(dat)[1]=="DDT"){
        lab[1]="DDT Level"
      }
      if (colnames(dat)[2]=="DDT"){
        lab[2]="DDT Level"
      }

      library(ggplot2)

      # Create a ggplot object.
      cn=colnames(dat)
      if (col=="RIVER"){
        if (lab[1]=="Mile"){
          if (lab[2]=="Length"){
            g=ggplot(dat,aes(x=MILE,y=LENGTH,col=RIVER))
          }
        }
        if (lab[1]=="Mile"){
          if (lab[2]=="Weight"){
            g=ggplot(dat,aes(x=MILE,y=WEIGHT,col=RIVER))
          }
        }
        if (lab[1]=="Mile"){
          if (lab[2]=="DDT Level"){
            g=ggplot(dat,aes(x=MILE,y=DDT,col=RIVER))
          }
        }
        if (lab[1]=="Length"){
          if (lab[2]=="Mile"){
            g=ggplot(dat,aes(x=LENGTH,y=MILE,col=RIVER))
          }
        }
        if (lab[1]=="Length"){
          if (lab[2]=="Weight"){
            g=ggplot(dat,aes(x=LENGTH,y=WEIGHT,col=RIVER))
          }
        }
        if (lab[1]=="Length"){
          if (lab[2]=="DDT Level"){
            g=ggplot(dat,aes(x=LENGTH,y=DDT,col=RIVER))
          }
        }
        if (lab[1]=="Weight"){
          if (lab[2]=="Mile"){
            g=ggplot(dat,aes(x=WEIGHT,y=MILE,col=RIVER))
          }
        }
        if (lab[1]=="Weight"){
          if (lab[2]=="Length"){
            g=ggplot(dat,aes(x=WEIGHT,y=LENGTH,col=RIVER))
          }
        }
        if (lab[1]=="Weight"){
          if (lab[2]=="DDT Level"){
            g=ggplot(dat,aes(x=WEIGHT,y=DDT,col=RIVER))
          }
        }
        if (lab[1]=="DDT Level"){
          if (lab[2]=="Mile"){
            g=ggplot(dat,aes(x=DDT,y=MILE,col=RIVER))
          }
        }
        if (lab[1]=="DDT Level"){
          if (lab[2]=="Length"){
            g=ggplot(dat,aes(x=DDT,y=LENGTH,col=RIVER))
          }
        }
        if (lab[1]=="DDT Level"){
          if (lab[2]=="Weight"){
            g=ggplot(dat,aes(x=DDT,y=WEIGHT,col=RIVER))
          }
        }
      }

      if (col=="SPECIES"){
        if (lab[1]=="Mile"){
          if (lab[2]=="Length"){
            g=ggplot(dat,aes(x=MILE,y=LENGTH,col=SPECIES))
          }
        }
        if (lab[1]=="Mile"){
          if (lab[2]=="Weight"){
            g=ggplot(dat,aes(x=MILE,y=WEIGHT,col=SPECIES))
          }
        }
        if (lab[1]=="Mile"){
          if (lab[2]=="DDT Level"){
            g=ggplot(dat,aes(x=MILE,y=DDT,col=SPECIES))
          }
        }
        if (lab[1]=="Length"){
          if (lab[2]=="Mile"){
            g=ggplot(dat,aes(x=LENGTH,y=MILE,col=SPECIES))
          }
        }
        if (lab[1]=="Length"){
          if (lab[2]=="Weight"){
            g=ggplot(dat,aes(x=LENGTH,y=WEIGHT,col=SPECIES))
          }
        }
        if (lab[1]=="Length"){
          if (lab[2]=="DDT Level"){
            g=ggplot(dat,aes(x=LENGTH,y=DDT,col=SPECIES))
          }
        }
        if (lab[1]=="Weight"){
          if (lab[2]=="Mile"){
            g=ggplot(dat,aes(x=WEIGHT,y=MILE,col=SPECIES))
          }
        }
        if (lab[1]=="Weight"){
          if (lab[2]=="Length"){
            g=ggplot(dat,aes(x=WEIGHT,y=LENGTH,col=SPECIES))
          }
        }
        if (lab[1]=="Weight"){
          if (lab[2]=="DDT Level"){
            g=ggplot(dat,aes(x=WEIGHT,y=DDT,col=SPECIES))
          }
        }
        if (lab[1]=="DDT Level"){
          if (lab[2]=="Mile"){
            g=ggplot(dat,aes(x=DDT,y=MILE,col=SPECIES))
          }
        }
        if (lab[1]=="DDT Level"){
          if (lab[2]=="Length"){
            g=ggplot(dat,aes(x=DDT,y=LENGTH,col=SPECIES))
          }
        }
        if (lab[1]=="DDT Level"){
          if (lab[2]=="Weight"){
            g=ggplot(dat,aes(x=DDT,y=WEIGHT,col=SPECIES))
          }
        }
      }

      g=g+coord_equal()


      if (plotType=="scatterplot"){
        sg=g+geom_point()+ggtitle("Scatterplot of DDT Data")
      }

    }

    if (plotType=="screeplot"){
      pc=obj$pc
      len=length(pc$sdev)+1
      screeplot(pc,xlim=c(0,len),ylim=c(0,max(pc$sdev)^2+1000),
                main="Principal Components of DDT Data")
    }

    if (plotType=="biplot"){
      pc=obj$pc
      biplot(pc,main="Biplot of DDT Data")
    }

    if (plotType=="boxplot"){
      if (mile==TRUE){
        b=dat$MILE
        lab="Mile"
      }
      else if (length==TRUE){
        b=dat$LENGTH
        lab="Length"
      }
      else if (weight==TRUE){
        b=dat$WEIGHT
        lab="Weight"
      }
      else if (ddt==TRUE){
        b=dat$DDT
        lab="DDT Concentration"
      }

      boxplot(b,main=substitute(paste("Boxplot of ",lab)))

    }

    if (plotType=="scatterplot"){
      sg
    }

  }

  plot.inf(infObj,type=plotType,col=col,prop=prop,
           cumProp=cumProp,eig=eig,screeVar=screeVar)

}
<file_sep>library(shinydashboard)
library(shinyWidgets)

ui <- dashboardPage(
  dashboardHeader(title="Analysis of DDT Data",titleWidth=500),
  dashboardSidebar(disable=TRUE),
  dashboardBody(
    fluidRow(
      box(plotOutput("plot1",height=500)),

      # Box 1: Quantitative Variable Restrictions
      box(
        status="warning",
        solidHeader=TRUE,
        collapsible=TRUE,
        width=3,
        background="red",
        title="Quantitative Variables",
        prettySwitch("mile","Include Mile?",TRUE),
        prettySwitch("length","Include Length?",TRUE),
        prettySwitch("weight","Include Weight?",TRUE),
        prettySwitch("ddt","Include DDT Level?",TRUE)
      ),

      # Box 2: Qualitative Variable Restrictions
      box(
        status="warning",
        solidHeader=TRUE,
        collapsible=TRUE,
        width=3,
        background="blue",
        title="Qualitative Variable Limitations",
        prettySwitch("FCM","Include the FCM River?",TRUE),
        prettySwitch("LCM","Include the LCM River?",TRUE),
        prettySwitch("SCM","Include the SCM River?",TRUE),
        prettySwitch("TRM","Include the TRM River?",TRUE),
        prettySwitch("catfish","Include CCatfish?",TRUE),
        prettySwitch("buffalo","Include SMBuffalo?",TRUE),
        prettySwitch("bass","Include LMBass?",TRUE)
      ),

      # Box 3: Type of Output Plot
      box(
        status="warning",
        solidHeader=TRUE,
        collapsible=TRUE,
        width=3,
        background="orange",
        title="Type of Plot",
        selectInput("plotType","Type of Output",
                    list("scatterplot (1st two variables)","screeplot","biplot",
                         "boxplot (1st variable)"),
                    "screeplot")
      ),

      # Box 4: Plot Settings
      box(
        status="warning",
        solidHeader=TRUE,
        collapsible=TRUE,
        width=3,
        background="purple",
        title="PCA Settings",
        selectInput("pcaType","Type of Matrix for PCA",list("covariance","correlation"),"covariance"),
        selectInput("col","Color Data by?",list("RIVER","SPECIES"),"RIVER")
      )



    )
  )
)
<file_sep>library(shinydashboard)
library(shinyWidgets)
library(Project2AndrewMitchell)

#' Shiny Server Function
#'
#' @param input the input of the function
#' @param output the output of the function
#'
#' @return a shiny server
#' @export
#'
#' @examples server()
server<-function(input,output){

  output$plot1<-renderPlot({

    plotter(plotType=input$plotType,
            mile=input$mile,length=input$length,weight=input$weight,ddt=input$ddt,
            FCM=input$FCM,LCM=input$LCM,SCM=input$SCM,TRM=input$TRM,
            catfish=input$catfish,buffalo=input$buffalo,bass=input$bass,
            pcaType=input$pcaType,col=input$col)

  })
}
<file_sep>#' Constructor Function
#'
#' @param plotType the type of plot to be made
#' @param cor whether or not to use the correlation matrix for PCA
#' @param mile whether or not to include the mile variable
#' @param length whether or not to include the length variable
#' @param weight whether or not to include the weight variable
#' @param ddt whether or not to include the DDT variable
#' @param FCM whether or not to include the FCM River
#' @param LCM whether or not to include the LCM River
#' @param SCM whether or not to include the SCM River
#' @param TRM whether or not to include the TRM River
#' @param catfish whether or not to include the CCATFISH species
#' @param buffalo whether or not to include the SMBUFFALO species
#' @param bass whether or not to include the LMBASS species
#'
#' @return
#' @export
#'
#' @examples
const=function(plotType="scatterplot",cor=FALSE,mile=TRUE,length=TRUE,weight=TRUE,ddt=TRUE,
                FCM=TRUE,LCM=TRUE,SCM=TRUE,TRM=TRUE,
                catfish=TRUE,buffalo=TRUE,bass=TRUE,swap=FALSE){



  # Create the DDT data set.
  x=Project2AndrewMitchell::ddt

  # Remove subsets of the data if need be.

  # Rivers not to be included

  if (FCM==FALSE){
    x=subset(x,RIVER!="FCM")
  }

  if (LCM==FALSE){
    x=subset(x,RIVER!="LCM")
  }

  if (SCM==FALSE){
    x=subset(x,RIVER!="SCM")
  }

  if (TRM==FALSE){
    x=subset(x,RIVER!="TRM")
  }

  # Species to not be used

  if (catfish==FALSE){
    x=subset(x,SPECIES!="CCATFISH")
  }

  if (buffalo==FALSE){
    x=subset(x,SPECIES!="SMBUFFALO")
  }

  if (bass==FALSE){
    x=subset(x,SPECIES!="LMBASS")
  }

  # Remove quantitative variables if need be.
  if (ddt==FALSE){
    x=x[,-4]
  }
  if (weight==FALSE){
    x=x[,-3]
  }
  if (length==FALSE){
    x=x[,-2]
  }
  if (mile==FALSE){
    x=x[,-1]
  }

  # Conduct PCA on the data.
  xPC=x
  # Remove the quantitative variables from the data.
  rem=c()
  for (i in 1:ncol(xPC)){
    c=ncol(xPC)+1-i
    if (class(xPC[1,c])=="factor"){
      rem[i]=c
    }
  }
  if (length(rem)>=1){
    for (i in 1:length(rem)){
      xPC=xPC[,-rem[i]]
    }
  }

  # Conduct PCA.
  pc=princomp(xPC,cor=cor)

  # Find the eigenvalues and eigenvectors for the chosen matrix.

  # Covariance matrix
  if (cor==FALSE){
    ei=eigen(cov(xPC))
  }
  # Correlation matrix
  else {
    ei=eigen(cor(xPC))
  }
  # Store the values and vectors.
  eiVal=ei$values
  eiVec=ei$vectors

  obj=list(dat=x,pc=pc,eiVal=eiVal,eiVec=eiVec)

  class(obj)="inf"

  obj

}

<file_sep>---
title: "AndrewMitchellProject2"
author: "<NAME>"
output: 
  rmarkdown::html_vignette:
    toc: yes
vignette: >
  %\VignetteIndexEntry{AndrewMitchellProject2}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(AndrewMitchellProject2)
```

# DDT Data Set

The DDT data set within this package details observations taken on fish in areas in and in tributaries of the Tennessee River, where researchers were measuring the concentration of DDT present in each fish.  The following measurements were taken on each fish:

* Mile - the mile of the river at which the fish was caught to be examined

* Length - the length of the fish

* Weight - the weight of the fish

* DDT -  the concentration of DDT measured within the fish

* River - the section of the river in which the fish was caught

* Species - the species of the fish

This data set is the basis of the functions within this package.

# plotter() function

The plotter() function takes in a variety of variables designed to function alongside the Shiny server included in the package.  The function can create the following types of plots.

* Scatterplots of two variables

* Biplots of principal components of data

* Boxplots of one variable

* Screeplots of principal components of data

Transformations for the data are done with in the const() function, which releases a constructed object of type inf, which contains the following:

* The original data set, possibly rearranged or with parts removed (i.e. certain variables or subsets of the data that have a particular value for a given variable)

* A principal component object created for a specified subset of the data or the entire data set

* The eigenvalues of the covariance or correlation matrix for the quantitative variables from the data

* The eigenvectors corresponding to the above eigenvalues

Each of these portions of the returned data are used in the creation of the plots being studied.

## Scatterplot

Scatterplots can be created by the plotter() function for any combination of two of the quantitative variables in the DDT data set.  In addition, the data within the plot can be color-coded to indicate either the river from which they were taken or the species of fish depending on the user's choice.  The scatterplot indicates a significant difference in fish concentrations between the Tennessee River and its tributaries, with the fish the furthest distances nearly all coming from this river.

## Biplot

Biplots plot the most significant two principal components of the data set, i.e. those corresponding to the largest eigenvalues.  The individual data are plotted, and arrows are placed on the plot for each variable being used in PCA with a magnitude and direction.  Arrows pointing to the right have a positive correlation with the first principal component, and pointing up have a positive correlation with the second principal component.  The opposite can be said for arrows pointing to the left and downward respectively.  An arrow with a larger magnitude in a particular direction indicates a stronger correlation, no matter whether positive or negative.  The principal components of the data are defined as follows, where $\{y_i\}_{i=1}^p$ are the principal components (with $p$ being the number of variables being used), and $\{e_i\}_{i=1}^p$ are the eigenvectors of whichever matrix is being used:

$$
\begin{eqnarray}
y_1&=&e_{11}x_1+e_{12}x_2+...+e_{1p}x_p\\
y_2&=&e_{21}x_1+e_{22}x_2+...+e_{2p}x_p\\
\vdots\\
y_p&=&e_{p1}x_1+e_{p2}x_2+...+e_{pp}x_p
\end{eqnarray}
$$

From this construction, one can see how since the largest eigenvalue has large effect on the principal component variance, the variable whose influence matches the first principal component would have the largest effect overall.

## Boxplot

Boxplots are used primarily as an indication of spread of data or comparison between sets of data.  The lines on the box on a boxplot indicate the quartiles of the data, and the end of the "whiskers" indicate the last data point before the others are considered outliers (typically defined in this situation by not within 1.5 times the interquartile range of the data from the middle 50% of data). Boxplots also indicate the median, which can be a useful indicator of centrality due to its resistance to outliers.

## Screeplot

The screeplot shows the variances of the principal components in the order of importance of the components.  The most common use of the screeplot is to determine at which point to cut off which principal components are being examined: only those who have significant impact on the data will be used.  The plot does not give any exact numbers to calculate, but researchers use the change in slopes of lines connecting the components' variances to make subjective decisions.  Such decisions can alleviate some of the challenges that arise when dealing with vast numbers of variables.

# Shiny Server

The server in this package is comprised of the server.R and ui.R files.  These files together provide the following options for the user of the server:

* Removal of quantitative variables (up to the point where they can still be used)

* Removal of data of a particular species or a particular river

* Changing the method by which PCA is carried out (i.e. correlation or covariance matrix)

* Determining which variables will be used in plots

* Determining what plot will be produced at any given time
