# This tutorial shows you how to conduct a simple correlation analysis.
# Correlation analysis examines the association between two numeric variables.
# The Pearson product-moment correlation coeficient (symbolized as r) varies between +1 and -1, with r = 1 indicating a perfect positive association, r=-1 indicating a perfect negative association, and r = 0 indicating no association between the two variables.
# A statistical test is performed to examine whether r is significantly different from 0 (statistically significant).
# The data in this tutorial are standard length (SL) and head length (HL) data collected from an anadromous population of threespine stickleback called Rabbit Slough.
# The data file (data_RS_M_SL_HL.txt) is available at: https://github.com/aguirrelab/r-tutorials


# To begin, open the ggplot2 package, which will be used to plot the data

library(ggplot2)

# Set the working directory. 
# Note that you will have to change the path to the appropriate directory for your computer.

setwd("C:/1awinz/R_work/correl_tutorial")

# Open data file called "data_RS_M_SL_HL.txt". 

data=read.table("data_RS_M_SL_HL.txt", header=T)
attach(data)

# Plot the data to see what it looks like by creating a scatter plot in ggplot2

ggplot(data, aes(x=SL, y=HL)) + geom_point() + theme_classic()

#Based on the scatterplot, it looks like SL and HL are positively correlated.

# Conduct the correlation analysis to test whether standard length and head length are significantly correlated.
# Use the cor.test() command to calculate the Pearson correlation coefficient, the 95% confidence intervals, and conduct a significance test
# The significance test is a t-test of whether the correlation coefficient differs significantly from 0.

cor.test(SL, HL)
## 
##  Pearson's product-moment correlation
## 
## data:  SL and HL
## t = 7.2424, df = 48, p-value = 3.128e-09
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.5561122 0.8333393
## sample estimates:
##       cor 
## 0.7226059
# The results indicate that SL and HL are highly correlated (P=3.128e-09, which is much less than the P<0.05 standard)
# The correlation coefficient is 0.723 with 95% CI of 0.556 and 0.833
# 
#
#
#
# The end.