PSY 771 — R Scripts

Getting Started

Work through these two scripts before the first class session.

Script 1
What R is, how to install it, and the core building blocks — arithmetic, objects, vectors, matrices, and data frames. Start here if you are new to R.
Script 2
A complete data analysis workflow: importing a real psychology dataset, computing descriptive statistics with psych::describe(), testing correlations with cor.test(), and fitting a multiple regression with lm().
Script 3
Standardizing scores with the z-score formula and scale(), finding areas under the normal curve with pnorm(), constructing 80/90/95/99% confidence intervals with qnorm(), and writing a custom z.test() function in R.
Script 4
One-sample, independent samples (equal and Welch), and paired t-tests using a reading intervention dataset. Includes Levene's test for homogeneity of variance, bar graphs with error bars, and a demonstration of how the t-distribution converges to z as sample size increases.
Script 5
Testing whether reading time differs across device types (phone, tablet, laptop). Covers assumption checking (Shapiro-Wilk, Levene's test), fitting the model with lm() and aov(), eta squared as an overall effect size, and post-hoc comparisons with Tukey's HSD and Cohen's d.
Script 6
Testing whether blood pressure changes across four time points (Before, One Month, Three Months, Six Months) using the same participants. Covers the between-subjects vs. within-subjects distinction, reshaping data from wide to long format with melt(), assumption checking (normality of difference scores, sphericity), fitting the model with aov() and an Error() term, partial eta squared, Tukey's HSD via emmeans, confidence intervals, and Cohen's d for pairwise comparisons.
Script 7
Testing the effects of device type (Phone, Tablet, Laptop) and lighting condition (Blue, White, Yellow, Natural) on reading time in a 3 × 4 factorial design. Covers recoding factor levels with factor(), interaction plots, fitting the model with a Device * Light term, interpreting the ANOVA table (main effects and interaction), partial eta squared, and how a non-significant interaction guides the decision to interpret main effects directly. Post-hoc section uses Tukey's HSD via emmeans with confidence intervals and Cohen's d for pairwise comparisons of both factors.
Script 8
Verifying the four assumptions that underlie ANOVA — independence, homoscedasticity, normality, and (for repeated-measures designs) sphericity. Covers residual diagnostic plots, likelihood ratio tests for unequal variance using lme() from the nlme package, Q-Q plots, and Mauchly's test with Greenhouse-Geisser and Huynh-Feldt corrections via the car package.