CHAD C. WILLIAMS
  • Home
  • Research
  • Publications
  • Statistics
    • How to Pass a Stats Class
    • Statistics with Excel
    • Statistics with R
  • Portfolio
  • Contact
  • …
  • YouTube
  • Google Scholar
  • Twitter
  • GitHub
  • OSF
  • Home
  • Research
  • Publications
  • Statistics
    • How to Pass a Stats Class
    • Statistics with Excel
    • Statistics with R
  • Portfolio
  • Contact
  • …
  • YouTube
  • Google Scholar
  • Twitter
  • GitHub
  • OSF
Picture

Common R Commands

This is a guide to commands for the RStudio statistical program. This is an attempt to conglomerate everything there is to do with stats with R, therefore, it will always be a work in progress. Likely the most useful parts of this page are the various ANOVAs (one-way, two-way, independent, repeated-measures, mixed design) and Generalized Linear Mixed Effects Models that are described as it is difficult to find very much information on these subjects. If you have any questions, feel free to email me at ccwillia@uvic.ca.

This page does not address the theoretical concepts of statistics.

'Get R Done' Video Series
I am now producing step-by-step R tutorials on my YouTube Channel:
Check it out here!

Getting started
  • Installing Addons
    • Install.packages(“package_name”)
    • Library(package_name)
  • Set working directory
    • setwd("enter path here")
Working with data
  • Load data
    • variablename = read.table(“data_name.txt”)
  • Rename columns of table
    • names(variablename) = c(“Column1”, “Column2”, “Column3”)
  • Rename columns and rows of matrix
    • colnames(variablename) = c(“Column1”, “Column2”, “Column3”)
    • rownames(variablename) = c(“Row1”, “Row2”, “Row3”)
  • Selecting column
    • variablename$Column1
  • Selecting specific data in column
    • variablename$Column2[variablename$Column1 == 1]
Creating data
  • Normal distribution
    • rnorm(N, mean, SD)
    • E.g. rnorm(1000,300,25)
  • Random distribution
    • runif(N, min, max)
    • e.g. runif(1000,1,100)
Descriptive statistics
  • Mean
    • mean(variablename$Column1)
  • Median
    • median(variablename$Column1)
  • Mode
    • mode(variablename$Column1)
  • Sum
    • sum(variablename$Column1)
  • Max
    • max(variablename$Column1)
  • Min
    • min(variablename$Column1)
  • Standard Deviation
    • sd(variablename$Column1)
  • Variance
    • var(variablename$Column1)
  • Confidence Interval
    • confint(variablename$Column1)
Inferential statistics - ttests & Cohen's d
  • Single Sample
    • t.test(variablename$Column1, mu = 0)
  • Paired Sample (Install package 'effsize' for cohen.d)
    • t.test(variablename$Column1, variablename$Column2, paired=TRUE)
    • cohen.d(variablename$Column1,variablename$Column2,paired = TRUE)
  • Independent Samples (Install package 'effsize' for cohen.d)
    • t.test(variablename$Column1, variablename$Column2)
    • cohen.d(variablename$Column1,variablename$Column2,paired = FALSE)
  • Bayesian Single Sample (with BEST package)
    • BESTmcmc(difscores)
  • Bayesian Independent Samples (with BEST Package)
    • BESTmcmc(variablename$Column1, variablename$Column2)
Inferential statistics - correlation and regression
  • Correlation
    • cor(variablename$Column1, variablename$Column2)
    • cor.test(variablename$Column1, variablename$Column2)
  • Regression
    • General Linear Model
      • lm(variablename$Column1~ variablename$Column2)
Inferential statistics - Anovas and partial eta squared
(Install package 'ez' if you will use the ezANOVA method)
  • One-Way Independent Samples ANOVA
    • ANOVAResults = ezANOVA(data=variablename,dv=.(DV),between=.(Group),detailed = TRUE, type=3)
  • One-Way Repeated Measures ANOVA
    • ANOVAResults = ezANOVA(data=variablename, dv=.(DV), wid=.(Subjects), within=.(IV), detailed = TRUE, type=3)
  • Two-Way Repeated Measures ANOVA
    • ANOVAResults = ezANOVA(data=variablename, dv=.(DV), wid=.(Subjects), within=.(IV,IV2), detailed = TRUE, type=3)
  • Two-Way Independent Samples ANOVA
    • ANOVAResults = ezANOVA(data=variablename, dv=.(DV), wid=.(Subjects), between=.(IV,IV2), detailed = TRUE, type=3)
  • Two-Way Mixed Design ANOVA
    • ANOVAResults = ezANOVA(data=variablename, dv=.(DV), wid=.(Subjects), within=.(IV), between=(Group),detailed = TRUE, type=3)
  • Effect size (need lsr package)
    • EtaandPartialEtaSquared = etaSquared(ANOVAResults)
Inferential statistics - post-hocs
  • Post-Hoc Analyses (Independent Samples)
    • tukeyHSD(aov(variablename$Column1~ variablename$Column2))
inferential statistics - testing assumptions
  • Tests of normality
    • Shapiro-Wilk
      • shapiro.test(variablename$Column1)
    • Kolmogorov-Smirnov
      • ks.test(variablename$Column1,'pnorm')
  • Homogeneity of Variance
    • Bartlett Test (Compares data C1 to data C2)
      • bartlett.test(variablename$Column1~ variablename$Column2)
    • Levene's Test (Compares data C1 based on factor codes on C2)
      • leveneTest(variablename$Column1~ variablename$Column2)
Inferential statistics - Linear Regression & Linear Mixed Effects Models
  • Linear Regression - Independent samples design
    • Single factor
      • lm(data = variablename, DV~IV)
    • Multiple factors with interaction
      • lm(data = variablename, DV~IV1+IV2+IV1*IV2)

  • Linear Mixed Effects Models - Dependent samples design                  
  • The following requires the lme4 R package
  • See Winter, 2013 for more info on LMMs:
  • http://www.bodowinter.com/tutorial/bw_LME_tutorial2.pdf
    • Single factor - Accounting for participant y-intercept variability
      • lmer(data = variablename, DV~IV + (1|ParticipantID))
    • Single factor - Accounting for participant y-intercept and slope variability
      • lmer(data = variablename, DV~IV + (IV|ParticipantID))
    • Multiple factors - Accounting for participant y-intercept variability
      • lmer(data = variablename, DV~IV1 + IV2 + IV1*IV2 + (1|ParticipantID))
 
  • Testing Significance of Models (requires lmerTest package for the GLMMs)
    • anova(regressionmodelname)
 
  • Determining Variability Explained of Model
    • Linear Regression
      • summary(regressionmodelname)
    • Linear Mixed Effects Models (requires MuMIn package)
      • r.squaredGLMM(GLMMmodelname)
Plotting Data
Note: This is basic plotting, you can do professional grade plotting with ggplot in R. I have a tutorial on how to do this here.
  • Scatterplot
    • plot(variablename$Column1)
  • Histogram
    • hist(variablename$Column1)
  • Barplot
    • barplot(variablename$Column1)
  • Boxplot
    • Boxplot(variablename$Column1)
  • Plotting data on both axes
    • plot(variablename$Column1~variablename$Column2)
Manipulating plots
  • The following come before the plot script
    • Displaying a range of graphs
      • Assort row by row
        • par(mfrow = c(1,2))
      • Assort column by column
        • par(mfcol = c(1,2))
  • The following are included in the plot script:
    • E.g., barplot(variablename$Column, ylim =c(1,1000), xlim = c(1,1000), main = “Title Name”, sub = “Subtitle Name”, ylab = “Label Name”, xlab = “Label Name”, col = “Red”)
    • Determining Axis ranges
      • ylim = c(1,1000)
      • xlim = c(1,1000)
    • Determining labels
      • main = “Title Name”
      • sub = “Subtitle Name”
      • ylab = “Label Name”
      • xlab = “Label Name”
    • Determining colours of data
      • col = “Red”
  • The following come after the plot script
    • Adding a line that represents the mean
      • abline(a = mean(mydata$rt), b = 0, col = "red")
    • Adding a point that represents the mean
      • points(mean(mydata$rt), pch=16***,col = "red")
    • Error Bars
      • arrows(bp,means-cis, bp, means+cis, angle=90, code=3, length=0.5)***
Powered by Create your own unique website with customizable templates.
  • Home
  • Research
  • Publications
  • Statistics
    • How to Pass a Stats Class
    • Statistics with Excel
    • Statistics with R
  • Portfolio
  • Contact
  • …
  • YouTube
  • Google Scholar
  • Twitter
  • GitHub
  • OSF