#######################################
######       R-Commands - Basics   ####
######          Spring 2018        ####
#######################################

##########################
### Loading libraries ####
##########################

# Modules (packages or libraries) must be downloaded and installed
# Before a package can be used, it must be "started up"
# the command to start up a package is library
# Examples of libraries include:

library(MASS)
library (car)
library(rms)
library(cluster)
library(pvclust)
library (ca)
library (FactoMineR)
library(anacor)
library(dynGraph) (MacOSX only)
...

#################
####  DATA   ####
#################

####################
### Loading data ###
####################

# Step one - Copy data from Excel into notatnik (not Word Pad)
# Step two - Load data into R from PB file

df <- read.table(file.choose(), header=T, sep="\t")

# If the data are in the form of a numerical table, you must add row.names= 1
# Loading contingency table from a text file (copied from Excel)

xtab <- read.table(file.choose(), header=T, sep="\t", row.names= 1)

#######################
### Looking at data ###
#######################

# Summary of dataframe
summary(df)

# First rows of data
head(df)

# Summary of one 'column' of a dataframe
table(df$NameOfColumn)

# Summary of contingency table
xtab

############################
### Manipulating at data ###
############################

# Change the order of features in a variable 
# (important in Logistic Regression)
df $ ColumnName = relevel(df $ ColumnName, ref= "FeatureName")

# Remove Columns
df $ NameOfColumn=NULL

# Creating a contingency table from
# For Cluster Analysis and Binary Correspondence Analysis

xtab = stack.fnc(df)

# Turn contingency table around (transpose)
xtab.t = t(xtab)
xtab.t

# Telling R that a given variable is a factor 
# (non numeric variable, often important for dates)
df $ ColumnName = as.factor(df $ ColumnName)

# Telling R that a given variable is ordinal, not nominal 
# (essential for ordinal regression)
df $ ColumnName = as.ordered(df $ ColumnName)

# Telling R that a given variable is numeric not categorical 
# (not sure why you would need this :)
df $ ColumnName = as.numeric(df $ ColumnName)

# Telling R not to ignore NA
df $ ColumnName = addNA(df $ ColumnName)

######################
###  SIGNIFICANCE  ###
######################

# Tests to see if difference between two sets of numbers is chance

# load the data
xtab <- read.table(file.choose(), header=T, sep="\t", row.names = 1)

# convert the data to cross-table 
# make sure you have run the script at the end of this file
xtab = stack.fnc(df)

# check data
xtab

#####################
#  Chi-Squared Test #
#####################

# This is the most common test in corpus linguistics / sociology
# It tests to see if the variation / differences could be chance.
# It gives you a p-value

CHI = chisq.test(xtab)
CHI

# Observed vs. Expected outcomes
O = CHI$observed
E = CHI$expected
E
O
(O-E)^2/E

# Pearson's residuals
# These show which cells are responsible for differences between the sets
RES= CHI$res
RES

###########################
###  Association Plots  ###
###########################
library(vcd)

d <- read.table(file.choose(), header=T, sep="\t")
## Load data

d $ V1 = factor(d $ V1, levels = c("level3", "level1", "level5"))
d $ V2 = factor(d $ V2, levels = c("level a", "level d", "c"))
## structure data - if needed

dt=table(d)
# convert to table object

dtt=t(dt)
transpose data - if needed

assoc(dtt, compress=T, spacing_equal(sp = unit(0.8, "lines")), residuals_type = "pearson", gp_varnames = gpar(fontsize = 10, fontfamily = "Times New Roman"), gp_labels = gpar(fontsize = 8, fontfamily = "Times New Roman", keep_aspect_ratio = F),  xscale = 0.9, yspace = unit(0.4, "lines"))
# make plot

#######################################################
### Converting flat data frame to contingency table ###
#######################################################
# Step 1: Load the data frame
# Important! Make sure the first column in the data frame 
# (in excel) is the the column you want as row names 

df <- read.table(file.choose(), header=T, sep="\t")

# Step 2: Run this script 
# Copy / paste into R and enter. Only do once per session.

stack.fnc=function(df.flat){
 for(i in 1:ncol(df.flat))
 df.flat[,i]=factor(df.flat[,i])
 df.stacked.nrow=length(levels(df.flat[,1]))
 df.stacked.ncol=0
 for(i in 2:ncol(df.flat))
 df.stacked.ncol=df.stacked.ncol+length(levels(df.flat[,i]))
 k=1
 df.stacked.colnames=rep("X",df.stacked.ncol)
  for(i in 2:ncol(df.flat))
  for(j in 1:length(levels(df.flat[,i]))) {df.stacked.colnames[k]=paste(colnames(df.flat[i]),levels(df.flat[,i])[j],sep=".")
  k=k+1}
df.stacked=data.frame(matrix(nrow=df.stacked.nrow,ncol=df.stacked.ncol))
df.stacked=matrix(data=rep(0,df.stacked.nrow*df.stacked.ncol),nrow=df.stacked.nrow,ncol=df.stacked.ncol,dimnames=list(levels(df.flat[,1]),df.stacked.colnames))
  for(i in 2:ncol(df.flat))
  for(j in 1:nrow(df.flat))
  df.stacked[as.character(df.flat[j,1]),paste(colnames(df.flat)[i],df.flat[j,i], sep=".")]=
  df.stacked[as.character(df.flat[j,1]),paste(colnames(df.flat)[i],df.flat[j,i], sep=".")]+1
  rm(list=c("df.stacked.ncol","df.stacked.nrow","i","j","k"))
  return(data.frame(df.stacked))
  }

# Step 3: This creates your contingency table called "xtab"
xtab = stack.fnc(df)
# Step 4: Check it has worked
xtab