# lessRstats.com/lessR.r
# tour of lessR functions for data analysis

# Purpose: Provide basic statistical computations for the analyses presented
#          in intro stat texts and more

# Get R
# http://r-project.org

# one time only, to get the lessR functions onto your computer
# if asked to install into a personal library, say Yes
# install.packages("lessR")

# Begin each R session by loading the lessR functions
library(lessR)

# help
# ------------------------------------------------------------------------
Help()  # list the lessR help values by subject
Help(lessR)  # access to the full lessR manual and News items
Help(Read)


# read data from one of many different formats into a data table called mydata
#   with the same Read statement: csv, tab-delimited text, Excel, SPSS, and SAS
# ------------------------------------------------------------------------
# Categorical: Gender coded M, F;  Dept coded ACCT, ADMN, FINC, MKTG, SALE
# Numeric: Salary and Years
#mydata <- Read()  # or rd(), browse for text, Excel, SPSS, SAS, or R data file
#head(mydata)  # R function, see variable names and first 6 rows of data
#mydata <- Read("http://lessRstats.com/data/employee.csv")  # read data from web
#mydata <- Read("http://lessRstats.com/data/employee.csv", row.names=1)  # row names

# data included as part of lessR
mydata <- rd("Employee", format="lessR")  # this example includes variable labels
details()  # provides full details of the data frame, defaults to mydata
db()  # brief version, same as provided by Read

# can also read a text file o fixed width formatted data
# example provided later

# distribution of a categorical variable
# ------------------------------------------------------------------------
# all the same
BarChart(Dept)   # or bc(Dept)
bc(Dept)  #  all function calls have a 2 or 3 digit abbreviation
BarChart(Dept, data=mydata)   # mydata is the default data frame (table)
bc(Dept, fill="colors")  # specify a more colorful display
bc(Dept, rotate.x=45, offset=1)  # value labels rotated and offset
# other summaries
PieChart(Dept)   # or pc(Dept), provides a doughnut or ring chart
PieChart(Dept, hole=0)   # full pie chart
Plot(Dept)  # or Plot(Dept), 1-categorical variable bubble plot
SummaryStats(Dept)  # or ss(Dept), no graphics

# read Mach IV data, integer Likert responses from 0 to 5
mydata <- Read("Mach4", format="lessR")  # read a data file included with lessR
# form a Bubble Plot Frequency Matrix (BPFM) from a range of x-variables
# each line is a bubble plot of frequencies for a single variable
Plot(c(m06,m07,m09,m10))
# for each bubble, lighten fill color, make border black
Plot(m06:m12, trans=.8, coloe="gray50")
# create BPFM for entire Mach IV scale and store as a pdf file
LikertCats <- c("Strongly Disagree", "Disagree", "Slightly Disagree",
                     "Slightly Agree", "Agree", "Strongly Agree")
Plot(m01:m20, value.labels=LikertCats, pdf.file="MachFreqs.pdf")
Plot(m06)  # Integrated Violin/Box/Scatterplot (VBS plot)

# back to the Employee data
mydata <- rd("Employee", format="lessR")  # includes variable labels


# distribution of a numeric (continuous) variable
# ------------------------------------------------------------------------
Plot(Salary)  #  Integrated Violin/Box/Scatterplot (VBS plot)
Plot(Salary, by1=Gender)  #  Trellis VBS plots for Gender
Histogram(Salary)   # or hs(Salary)
hs(Salary, bin.start=30000, bin.width=12000, bin.end=150000)  # common options
Density(Salary)  # or dn(Salary)
BoxPlot(Salary)  # or bx(Salary), a call to Plot with vbs.plot="b"
SummaryStats(Salary)  # or ss(Salary)
ss.brief(Salary)  # brief output
hs(Salary, Rmd="Salary")  # generate an R markdown file of all the methods
#hs(Salry)  #  ERROR, misspelled variable name to show lessR error messages

# for ordered values, such as by time (just for illustration here)
LineChart(Salary)  # or lc(Salary)
Plot(Salary, run=TRUE)
Plot(Salary, run=TRUE, center.line="off", show.runs=TRUE)
Plot(Salary, run=TRUE, lwd=0)  # points only
Plot(Salary, run=TRUE, size=0)  # line only
Plot(Salary, run=TRUE, area=TRUE)

# distributions for multiple variables (writes to pdf files)
# ------------------------------------------------------------------------
# the corresponding standard R functions do not take multiple variables
# specify multiple variables with c function or :
Histogram(c(Salary, Years))  # c is R combine function
BarChart(Gender:Satisfaction)  # : indicates a range of variables in R
Histogram() # a histogram for each numeric variable in mydata data table
CountAll()  # or ca(), a bar chart or histogram for each variable in mydata

# relationship of two variables
# ------------------------------------------------------------------------
# two numeric variables yields traditional scatter plot
Plot(Years, Salary)   # or Plot(Years, Salary)
Plot(Years, Salary, auto=TRUE)   # or Plot(Years, Salary)
Plot(Years, Salary, ellipse=TRUE, fit="loess")   # common options
Plot(Years, Salary, ellipse=c(.50,.75,.90))   # 3 data ellipses
Plot(Years, Salary, size=3) # bubbles

# classify by a 3rd variable, which is categorical
Plot(Years, Salary, by=Gender)
Plot(Years, Salary, by=Gender, color=c("green", "brown"))
Plot(Years, Salary, by=Gender, color=c("darkgreen", "brown"), shape=c("F","M"))
Plot(Years, Salary, by=Gender, color=c("darkgreen", "brown"), fit="ls")

# categorical with a numeric variable, shows scatter about each group mean
Plot(Dept, Salary)  # Dept is categorical, Salary is numeric
SummaryStats(Salary, by=Dept)   # stats, for each level of a grouping variable
ss.brief(Salary, by=Dept)   # brief output

# two categorical variables (factors)
BarChart(Gender, by=Dept)  # text from ss.brief
bc(Gender, by=Dept, beside=TRUE, horiz=TRUE)  # common options
Plot(Gender, Dept)  # scatter (bubble) plot in place of traditional bar chart
SummaryStats(Gender, Dept)  # 4 cross-tab tables
bc(Dept, by=Gender, beside=TRUE)
# the proportion of data values by fill variable within each group
bc(Dept, by=Gender, proportion=TRUE)

Plot(Dept, Salary)
# Plot refers to what is plotted in the scatter plot as the values
# Plot default for values is "data"
Plot(Dept, Salary, values="mean")
Plot(c(Pre, Post), Salary)
Plot(c(Pre, Post), Salary, fit="ls")



# Cleveland dot plot test
Plot(Salary, row.names)
Plot(c(Pre, Post), row.names)
Plot(row.names, Salary, means=FALSE, rotate.x=60, offset=1, xlab="")
Plot(Salary, row.names, fill="black", color="black", sort.y=TRUE)
style(panel.fill="off", grid.color="off")
Plot(Salary, row.names, sort.y=TRUE, segments.y=TRUE)
style()  # return to default style


# set system values
# ------------------------------------------------------------------------
# color themes
Help(theme)
style("orange", sub.theme="black")
hs(Salary)
Plot(Years, Salary, ellipse=TRUE, fit="loess")   # common options
style("darkred")
hs(Salary)
style("gray")
hs(Salary)
Plot(Years, Salary, ellipse=TRUE, fit="loess")   # common options

# more system settings, can also be set by individual function calls
style(brief=TRUE)  # partial text output
style(quiet=TRUE)  # no text output
style(n.cat=6)  # treat integer variables with <=6 unique values as categorical
style(brief=FALSE) # back to default
style(quiet=FALSE)
style(n.cat=0)


# analysis of a mean
# ------------------------------------------------------------------------
ttest(Salary)  # or tt(Salary) for confidence interval
tt.brief(Salary, mu0=70000)  # brief output, for CI and hypothesis test
tt.brief(n=37, m=63795.557, s=21799.533)  # input summary stats instead of data
ttestPower(n=20, s=5)


# compare 2 groups, with graphics
# ------------------------------------------------------------------------
ttest(Salary ~ Gender)   # or tt(Salary ~ Gender), compare means for two groups
#tt(Gender ~ Salary)  #  ERROR, do it wrong to illustrate lessR error messages
tt.brief(Salary ~ Gender)   # brief output
tt(n1=18, n2=19, m1=71147.458, m2=56830.598, s1=23128.436, s2=18438.456) # from stats
ttestPower(n1=14, n2=27, s1=4, s2=6, msmd=.5)

# Pre and Post are variables that are two matched sets of data values
ttest(Pre, Post)  # t-test to compare group means, data entered as two vectors
ttest(Pre, Post, paired=TRUE)  # t-test of paired differences


# analysis of variance, compare 2 or more groups
# ------------------------------------------------------------------------
# warpbreaks is a data set contained in R
ANOVA(breaks ~ tension, data=warpbreaks)  # or av, one-way ANOVA

# two-factor between-groups ANOVA with replications and interaction
ANOVA(breaks ~ wool * tension, data=warpbreaks)

# randomized blocks design with the second term the blocking factor
ANOVA(breaks ~ wool + tension, data=warpbreaks)


# regression analysis
# ------------------------------------------------------------------------
# single predictor variable regression
reg(Salary ~ Years)  # or reg, or reg.brief, equivalent of Excel regression
reg.brief(Salary ~ Years)  # reg.brief equivalent of Excel regression
#reg(Salary ~ 1)  # null model
#reg.brief(Salary ~ 1)  # null model

# multiple regression
reg(Salary ~ Years + Pre + Post)  # multiple reg

# output to an object
r <- reg(Salary ~ Years + Pre + Post)  # multiple reg, save output to object r
r   # see all the output
names(r)  # see the output segments available for viewing or further analysis
r$out_estimates  # see just the piece of the estimates

# create R markdown file that does full interpretation of the output
r <- reg(Salary ~ Years + Pre + Post, Rmd="MultReg")  # R markdown

# nested models analysis
Nest(Salary, c(Years), c(Pre, Post)) # compare reduced to full model, with common data

# logit analysis, with a classification table
Logit(Gender ~ Salary)


# transformations
# ------------------------------------------------------------------------
# use Recode to reverse score four Likert variables: m01, m02, m03, m10
mydata <- Read("Mach4", format="lessR")  # read a data file included with lessR
mydata <- Recode(c(m01:m03,m10), old=0:5, new=5:0)  # R has no recode function

mydata <- rd("Employee", format="lessR", quiet=TRUE)  # internal data, no console output
mydata <- Transform(Salary = Salary / 1000)  # transform by formula
males <- Subset(Gender=="M", columns=c(Years, Salary))  # only Males, two vars
mydata <- Sort(Salary)  # default from smallest to largest
mydata <- Sort(Salary, direction="-")  # can also specify directiont
mydata <- Sort(row.names)  # unlike standard R, can sort by row name
# also Merge for vertical or horizontal merge of two data tables


# variable labels
# ------------------------------------------------------------------------
# read data file w/o variable labels, and then separately read labels
mydata <- Read("http://lessRstats.com/data/employee.csv")
mylabels <- VariableLabels("http://lessRstats.com/data/employee_lbl.csv")

# read data file w/o variable labels, and then read labels from console
mydata <- Read("http://lessRstats.com/data/employee.csv")
lbl <- "
Years, time of company employment
Gender, Male or Female
Dept, department employed
Salary, annual salary
Satisfaction, satisfaction with work environment
HealthPlan, 1=GoodHealth 2=YellowCross 3=BestCare
Pre, Test score on legal issues before instruction
Post, Test score on legal issues after instruction
"
mylabels <- VariableLabels(lbl)

# modify/display a single variable label
mylabels <- VariableLabels(Salary, "Annual Salary (USD)") # add/modify 1 label
VariableLabels(Salary)  # list the contents of a single variable label

# display all variable labels
db()  # also the variable names and sample values
vl()


# write a data table to an external file
# ------------------------------------------------------------------------
Write("myfile")  # write default mydata data table to myfile in csv format
Write("myfile", row.names=FALSE)  # preferred if row names are only row numbers
Write("myfile", format="Excel", row.names=FALSE)  # to Excel data table

# after reading original data and doing the transformations, save as is
Write("myfile", format="R")  # native R format, straight copy of mydata


# correlation matrices and factor analysis
# ------------------------------------------------------------------------
mydata <- Read("Mach4", format="lessR")  # read a data file included with lessR
# calculate the correlations and store in mycor
head(mydata)
mydata <- Recode(c(m03, m04, m06, m07, m09:m11, m14, m16, m17, m19), old=0:5, new=5:0)
mycor <- cr(m01:m20)
efa(n.factors=4)
# confirmatory factor analysis of 4-factor solution of Mach IV scale
# Hunter, Gerbing and Boster (1982)
MeasModel <-
  "
Deceit =~ m07 + m06 + m10 + m09
Trust =~ m12 + m05 + m13 + m01
Cynicism =~ m11 + m16 + m04
Flattery =~ m15 + m02
"
c <- cfa(MeasModel)
# view all the output
c
# names of each output segment
names(c)
# view just the scale reliabilities
c$out_reliability

# correlation matrix operations
mycor <- cr(m01:m20, graphics=TRUE)
mydata <- corReorder()   # simple re-order algorithm by highest remaining cor
prop()
scree()


# read fixed width data, requires parameters: widths, color.names
# ------------------------------------------------------------------------
rep(1,20)  # standard R function, here a 1 listed 20 times
to("m",20)  # lessR, names sequential vars with same widths [unlike paste0("m", 1:20)]
# specify the width of each data column, then match with the column name
# the Mach4 scale is 6-pt Likert data, so each response is a single column
mydata <- Read("http://lessRstats.com/data/Mach4Plus.fwd",
               widths=c(4,2,1,5,1,rep(1,20)),
               col.names=c("ID", "Age", "Gender", "Code", "Form", to("m",20)))

