---
title: "SS_numeric_08.04.17"
output: html_notebook
---
Summary Statistics for numeric variables 
Moore, T.P. (2017)

 Input: columns = numeric variables; rows = sites
Output: box.plot, stat.table, cor.table, principle.component.analysis

Functions: ipak, cor.test.
 Packages: install (TRUE/FALSE)
```{r, message=FALSE, warning=FALSE}
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)}

cor.mtest <- function(mat, ...) {
  mat <- as.matrix(mat)
  n <- ncol(mat)
  p.mat<- matrix(NA, n, n)
  diag(p.mat) <- 0
  for (i in 1:(n - 1)) {
    for (j in (i + 1):n) {
      tmp <- cor.test(mat[, i], mat[, j], ...)
      p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
    }
  }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  p.mat
}
ipak(c("readr","plotrix","ggplot2","vegan","moments","corrplot","RGraphics",
       "gridExtra","lawstat","Hmisc", "car","maptools"))
```

Working directory, import dataset, & Remove/transform variables. 
```{r, message=FALSE, warning=FALSE}
setwd("~/Documents/Desktop") 
df.csv.1 <- read_csv("./data/iris.csv") 

#df.csv.1 <- log(df.csv.1) # Transform dataset 
#df.csv.1$variable <- NULL # Remove collumn 
```
box.plot & stat.table
```{r, fig.width=5, message=FALSE, warning=FALSE}
#order.col & rm.characters
df.csv.1 <- df.csv.1[,order(names(df.csv.1))][,!sapply(df.csv.1,is.character)]

#summary.statistics.table
df.csv.1.SS <-cbind(
              sapply(df.csv.1,length),
              sapply(df.csv.1,function(x) sum(is.na(x))),
              format(sapply(df.csv.1,mean),digits=2,nsmall=2),
              format(sapply(df.csv.1,std.error),digits=2,nsmall=2),
              format(sapply(df.csv.1,skewness),digits=2,nsmall= 2),
              ifelse(data.frame(t(sapply(df.csv.1,shapiro.test)))$p.value < 0.05,"","NORM"))

          colnames(df.csv.1.SS) <- c("n","n/a", "mean","SEM","skew","shapiro")
t1 <- tableGrob(df.csv.1.SS[ order(row.names(df.csv.1.SS)), ])

#boxplot
p1 <- ggplot(stack(decostand(df.csv.1,"range")),
      aes(x = ind, y = values)) + coord_flip() + 
          geom_violin(aes(group=ind,colour=ind, fill=ind,alpha=0.2)) +
          geom_boxplot(aes(group=ind,colour=ind, fill=ind),alpha=0.5) +
          geom_dotplot(binwidth = 0.02, dotsize= 0.4, stackdir = "center", binaxis ="y",alpha=0.3) +
          labs(y ="standardized values", title = "") +
          scale_x_discrete(limits = rev(levels((stack(decostand(df.csv.1, "range")))$ind))) +
      theme(axis.title.y = element_blank(),
      axis.ticks.y = element_blank(),
      axis.ticks.x = element_blank(),
      legend.position="none")

#combine.plots
grid.arrange(p1, arrangeGrob(t1), ncol = 2)  
```
corr.table
```{r,message=FALSE, warning=FALSE}
corrplot(cor(df.csv.1[,!sapply(df.csv.1,is.character)]),
         method="color", col=colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))(200),  
         type="upper", order="hclust", addCoef.col = "black", tl.col="black", tl.srt=45, 
         p.mat = cor.mtest(df.csv.1[,!sapply(df.csv.1,is.character)]), sig.level = 0.01,
         insig = "blank",diag=FALSE)
```
Principle Component Analysis (PCA)
```{r}
#PCA
ord.pca <-rda(df.csv.1, scale=TRUE)
summary(ord.pca)
#PLOT
#par(pty = "s") # SquarePlot
ordiplot(ord.pca, type = "n", display = "sites", xaxt='n',yaxt='n',ann=FALSE)
  points(ord.pca, display = "sites", col = "black")
    ef <- envfit(ord.pca,df.csv.1)
    plot(ef, col="black")
    ef

    
mtext('PCA1 axis (%)', side=1, line=2,cex=1.1)
mtext('PCA2 axis (%)', side=2, line=2, cex=1.1)
```