Applied Psychological Statistics and Data Analysis in R and Python
Preface
About this Manual
For people who are not familiar with coding, I would suggest starting with JASP: it can help you build up the workflow and reduce pain. When you feel familiar with it, you can transfer to R or Python. JASP is free, open-source software with a graphical interface, and it can do a lot of advanced statistics.

This document is intended for students who have a psychology, sociology, or neuroscience background, and who use R as their primary statistical tool; SEM-related modules use lavaan and also include Mplus syntax. It focuses on the application of statistics and programming to your projects, so it is more of a toolkit, and you may need to have some knowledge of statistics and programming. Statistical principles and formulas are kept in optional Under the hood sections that you can skip.
Do I need a foundation in programming and statistics?
Yes, you need to have a slight foundation in statistics and programming. But it’s okay if you don’t have a firm grasp of the statistics: reading through the examples section of this document will help you solidify the knowledge. In the programming section, although I will not walk you through every step, I will list some necessary basic operations and some syntax samples.
Do I need to understand the statistical principles and formulas?
Not necessarily. For students who major in social sciences, even if you don’t understand statistical principles and mathematical formulas, you can still do data analysis, as long as you know the circumstances under which you should use a statistical method, the specific steps to follow, and how to interpret the results. Still, I recommend that you learn about them if you have the time, as this will deepen your understanding of statistics. That is why some modules include an optional Under the hood section: it explains the math behind the method and implements it by hand (for example, with matrix operations), so you can compare the result with the package output and see, for example, that the t-test, ANOVA, and regression are all the same linear model.
How do I use this manual?
For example, when you get a set of data, after reviewing the literature, you may know how to analyze it (or not), so you try to use R for statistical analysis but find that you do not know what functions or packages to use. Here you can review this document to find which statistical methods, and which functions, are appropriate. However, note that the most cutting-edge algorithms or statistics may still require you to write code.
What is included in this document?
Each module includes the name of the statistic, the type of variable, an example of the scenario in which it will be used, the packages and functions, useful resources (documentation, web pages, etc.), and a review of the package and the resource. Some modules also include an optional Under the hood section with the math and a hand-coded implementation.
Why don’t you give specific code or steps?
The process of learning programming is similar to riding a bicycle: just knowing the names of the operations is not enough, and you need to practice them over and over again. You can get started by reading a textbook or taking a lecture, but if you are doing a research project, these two methods may not be as effective, as they will take a lot of your time.
In the middle and later stages of programming, you will learn mainly by looking up web pages or reading official documentation, so it is necessary to develop the ability to learn on your own (especially by reading official documentation). Code is flexible: simple copy and paste may not achieve the desired effect if you do not understand what the code means.
Learning Objectives
- Master the basic syntax and concepts of the R language.
- Be able to write simple scripts to process and analyze data.
- Get familiar with R’s data structures, such as vectors, data frames, and lists, and be able to manipulate and process data.
- Import and export data efficiently, work with a variety of data formats, and perform preliminary data cleaning and preparation.
- Select and use appropriate R packages to extend the functionality of R.
- Perform basic statistical analysis and data visualization, and use R to create clear and informative charts.
- Develop problem-solving and self-study skills, applying the knowledge gained to real-world problems through example-based learning and project exercises.
Outline & Topics
Topics without a link are planned. The advanced ones, such as regularized regression and network analysis, will include Python.
Getting Started with R
- Introduction: why choose R, installation, interface, and data structures
- Example data: the simulated dataset used throughout the manual
- R Basic Operations
- Importing and exporting data
- Read and save data frames
- Merging data, converting variable types, naming variables and labels, and selecting variables
- Read data from several files and combine data (left and right)
- Data format: from wide to long
- Importing and exporting data
- Control flow and defining functions
forloopif else
Data Preparation
- Data cleaning
- Handling of missing values: direct deletion, multiple imputation, pros & cons
- Outliers
- Questionnaires or cross-sectional research:
- Reverse items, sum, average
- Descriptive statistics: M, SD, frequency, odds
Linear Models
- Overview: t-tests, ANOVA, correlation, and regression as one model
- Assumption checks
- Normality testing: univariate and multivariate normality tests
- Homogeneity of variance
- Residual diagnostics
- Basic statistics:
- Comparing means: t-tests and ANOVA
- Within-subject designs: paired t-test, repeated-measures ANOVA (sphericity)
- Post hoc tests and multiple comparisons
- Correlation and linear regression
- Correlation (Pearson, Spearman)
- Simple linear regression
- Multiple linear regression, multicollinearity test (VIF)
- Chi-square test
- Under the hood: the general linear model in matrix form (t-test, ANOVA, and regression as special cases)
- Nonparametric tests: overview and lookup table
- Under the hood: rank-based tests as linear models on ranks
- Multivariate analysis of variance (MANOVA)
- Effect sizes and confidence intervals
- Power analysis (a priori)
- Comparing means: t-tests and ANOVA
- Generalized linear model
- Logistic regression
- Ordinal regression
- Count models: Poisson, negative binomial
- Multilevel modeling (MLM)
- Regularized regression (lasso, ridge) and cross-validation
Reporting and Visualization
- Exporting data analysis results
- Plotting: ggplot2 package
- Tables: gtsummary package
Measurement
- Reliability and validity: Cronbach’s α, McDonald’s ω, convergent, discriminant, and criterion validity
- Multitrait-multimethod (MTMM) models
- Principal component analysis & factor analysis (EFA)
Structural Equation Modeling
- SEM
- CFA
- Second-order and bifactor models
- Model comparison: nested and non-nested models, chi-square difference test, AIC and BIC, equivalent models
- Example of nested models: congeneric, tau-equivalent, and parallel measurement models
- Mediation test, bootstrap
- Moderation
- Plotting and reading interactions
- Simple slopes and Johnson-Neyman intervals
- Reporting
- Latent variable interactions: a complete example
- Moderated mediation
- Multiple group comparison
- Dyadic data: the actor-partner interdependence model (APIM)
- Bayesian SEM (blavaan)
- Multilevel SEM
- SEM by hand: the implied covariance matrix and ML estimation, in R and Python
- CFA
Longitudinal Models
- Longitudinal measurement invariance
- Cross-lagged panel models
- Autoregressive (simplex) models
- Cross-lagged panel model (CLPM)
- Random intercept cross-lagged panel model (RI-CLPM)
- Longitudinal mediation
- Latent growth curve model
- Linear and quadratic growth
- Time coding and unequal intervals
- Latent basis model
- Time-invariant and time-varying covariates
- Parallel process growth
- The same model as a multilevel model
- Choosing the time metric; between- and within-person effects
- Longitudinal moderation
Time Series Analysis
- Time series analysis
Other Topics
- Cluster and discriminant analysis: latent profile analysis (LPA), latent class analysis (LCA)
- Psychological network analysis
- Partial correlation networks (Gaussian graphical model, EBICglasso)
- Accuracy and stability (bootstrap)
- Network comparison between groups
- Temporal networks (intensive longitudinal data)
- Bayesian statistics (introduction)
- Meta-analysis & systematic review
- FDR control
- Sensitivity analysis
Structure
- Categories: nonparametric & parametric; cross-sectional & longitudinal; categorical & continuous
- Statistical method name (Chinese & English)
- Variable type (the categorization needs to be clear)
- Package & function (R; Mplus syntax for SEM-related modules)
- Nonparametric alternative (for each test module)
- Comment (your own comments, e.g. the difference between ANOVA and the chi-square test)
- Resource (URL, textbook, website, etc.)
- Under the hood (optional): math principles and a hand-coded implementation
Reference
- Sara Emily Burke: Video Series: Brief Introduction to R
- Hands-On Programming with R
- R Cookbook, 2nd Edition
- R Cheat Sheet
- PsyTeachR: data-skills-v2 (GitHub)
- Research Methods in R (Andy Wills)
- Research Methods in R: source repository (GitHub)
- The Epidemiologist R Handbook
- R for Data Science (2e)
- Advanced R (2e)