Data Analysis Workflows, Causal Inference, and Linear Modeling in R
AI Guidelines and Academic Responsibility in Data Analysis Lab Nov 3
Permissible Use of Artificial Intelligence:
AI tools (such as large language models) may be utilized to assist in analytical workflows and coding.
The analyst maintains full, ultimate responsibility for the integrity, correctness, and reporting of all results.
Outputs containing hallucinations, confabulations, or statistical errors are the sole liability of the researcher.
Data Security and Privacy Standards:
Primary participant data must never be uploaded directly into commercial or public AI models.
Research best practices dictate that raw participant data must remain within secure, compliant storage environments.
AI models should be utilized exclusively for generating, debugging, or optimizing code templates, which are subsequently executed locally on private machines.
Industry Context: At an OpenAI Munich event in 2023, leadership acknowledged that institutional data trust remains an ongoing corporate trade-off, underscoring the imperative for strict independent data isolation in scientific inquiry.
Causal Inference and Instrumental Variables (IV)
Foundations of Causal Identification:
Rubin Causal Model: Serves as the standard framework for conceptualizing causality in social sciences.
Randomized Controlled Trial (RCT): The benchmark "workhorse" design in psychology. By randomly allocating individuals to treatment versus control conditions with equal probability, confounding variables are balanced, isolating the causal effect without relying on natural variation.
Quasi-Experimental Methods: Historically developed within psychology and later adopted by economics and political science—such as Difference-in-Differences (DiD) and Regression Discontinuity Design (RDD)—are increasingly vital due to the prevalence of massive, observational online datasets.
Barriers to Inferring Causation from Correlation:
The Third-Variable Problem (Confounding): An unobserved extraneous variable simultaneously drives variations in both the predictor and outcome variables, creating a spurious correlation.
Classical Illustration: The correlation between ice cream sales () and drowning deaths () is entirely driven by summer temperature (), which independently increases both swimming activity and ice cream consumption.
Reverse Causality and Bidirectionality: When two variables are correlated, the direction of influence is often ambiguous, or reciprocal feedback loops may exist.
Psychological Illustration: The correlation between social isolation and depression exhibits bidirectional ambiguity—depressive symptoms may drive withdrawal, while social isolation actively intensifies depressive pathology.
Mechanisms of Instrumental Variable (IV) Estimation:
Definition of an Instrument: In experimental science, an instrument is an objective measuring device (e.g., a ruler for physical distance, a spectrometer for light spectra, or a psychometric survey for attitudes). An instrument or metric is distinct from the underlying construct (e.g., an IQ test score is an imperfect operational instrument, not intelligence itself).
Methodological Function of an IV: An instrumental variable () is an exogenous source of variation used to predict an endogenous explanatory variable () in order to isolate its causal impact on an outcome ().
Core Conditions for a Valid Instrument:
Relevance Condition: The instrument () must be strongly correlated with the endogenous explanatory variable ().
Exclusion Restriction: The instrument () must affect the outcome variable () only through its relationship with , exhibiting no direct relationship with and no correlation with unobserved confounders.
Empirical Case Studies of Instrumental Variables:
Family Size and Maternal Wages: To determine whether family size causally depresses maternal compensation, researchers leverage the sex ratio of the first two children as an instrument. Because parental preference for sibling sex diversity frequently prompts families with two same-sex children to conceive a third, sibling sex composition serves as an exogenous predictor of family size.
Agricultural Output and Prosocial Behavior: Economists frequently employ exogenous meteorological variation (such as regional rainfall) as an instrument for local agricultural wealth, assessing its downstream effects on social and economic behaviors.
Transatlantic Slave Trade and Present-Day Interpersonal Trust (Wantchekon & Nunn):
Context and Alternative Hypotheses: Low interpersonal trust is a documented impediment to modern African market operations. Researchers investigated whether modern mistrust was caused by historical exposure to the transatlantic slave trade, or whether pre-existing regional mistrust simply facilitated slave procurement.
Biographical Background: Study co-author Leonard Wantchekon began his career escaping political imprisonment as a former student rebel from Benin, crossing into Nigeria, fleeing to France and Montreal (completing his undergraduate degree at age 30), and ultimately becoming an economics professor at Princeton.
Instrument Selection: Geographic distance from the ocean was selected as the instrument. Slave raiding ships were historically constrained to coastal waters, making penetration deeper into the interior progressively difficult, which created an exogenous geographic gradient in slave trade exposure.
Validation of the Instrument: Distance from the ocean strongly predicted exposure to the slave trade (relevance). To confirm the exclusion restriction, researchers examined regions globally (and within non-participating African regions) that did not experience the transatlantic slave trade; in those control populations, distance to the ocean exhibited zero baseline correlation with interpersonal trust.
Other Established Instrumental Variable Frameworks:
Lottery Allocations: Random administrative lotteries (e.g., draft lotteries, housing vouchers, charter school admissions) providing exogenous assignment to opportunities or interventions.
Judge Fixed Effects: Random assignment of criminal defendants to judicial arbiters with varying baseline sentencing stringencies to isolate the causal impacts of incarceration.
Data Management, Hygiene, and Data Provenance
Data Preservation Protocols:
Redundancy Axiom: The baseline standard for data storage requires maintaining triple backups ("If you have one, you have none; if you have three, you have two").
File Format Standards: Analysis must prioritize comma-separated value (
.csv) plain text files over proprietary spreadsheets (.xlsx). Spreadsheet software frequently applies automated conversions that irreparably corrupt data (e.g., misinterpreting alphanumeric strings or gene nomenclature as calendar dates).
Data Provenance and Reproducibility:
Definition: Data provenance requires maintaining an unbroken, fully auditable digital trail connecting raw data capture (e.g., paper forms, Qualtrics, SurveyMonkey) to published statistical tables.
Immutability of Raw Data: Raw data files must remain permanently unaltered. All transformations, variable re-codings, and data scrubbing must be scripted in code.
Pitfalls of Manual Alterations: Executing string replacements manually in spreadsheet editors introduces catastrophic cascading errors (e.g., executing a global find-and-replace of "United" to "USA" erroneously converts "United Kingdom" and "United Arab Emirates" into "USA"). Scripted transformations permit immediate debugging and re-execution without raw-file corruption.
Exploratory Data Analysis and Visualization Fundamentals
The Primacy of Visual Data Inspection:
Data analysis workflow allocation approximates data sanitation/preparation and statistical inference.
Automated analytical summaries must never supersede direct visual inspection.
Anscombe's Quartet Principles:
A set of four distinct bivariate datasets constructed by statistician Francis Anscombe that share identical descriptive statistical properties:
Identical sample sizes ().
Identical means (, ).
Identical sample variances (, \s_y^2 = 4.125).
Identical correlation coefficients () and regression lines ().
Divergent Underlying Distributions Exposed by Plotting:
Standard Linear: A conventional, moderately noisy linear relationship.
Nonlinear and Non-Monotonic: A curvilinear, parabolic arc requiring polynomial modeling (e.g., a quadratic term ).
Linear with Leverage Outlier: A tight linear distribution distorted by a single extreme outlier along the regression trajectory.
Zero-Variance Leverage Design: The independent variable has zero variation across observations except for a single distant point that artificially manufactures the entire regression slope.
Functional Nonlinearity Terminology:
Monotonicity: A function is monotonic if its values consistently move in a single direction (exclusively non-increasing or non-decreasing).
Non-Monotonicity: Relationships that reverse direction across the domain (e.g., inverted U-curves).
Polynomial Approximations: When the theoretical underlying mathematical function (e.g., sigmoidal, exponential) is unknown, the simplest baseline statistical approximation for curvilinear trends is the addition of a second-order polynomial (quadratic) term: .
Implementing Analysis Workflows in R and RStudio
Interactive Environments vs. Scripted Pipelines:
Interactive Console: Useful for ad-hoc evaluations, but inherently ephemeral; executing transformations exclusively in the console breaks data provenance and prevents replication.
R Scripts (
.R): Plain text files containing sequential code; provides a basic, repeatable record of analytical steps.R Markdown (
.Rmd) and Quarto (.qmd): Literate programming environments integrating prose, embedded code execution chunks, and dynamic rendering into publication-grade HTML, PDF, or Microsoft Word documents.
Fundamental R Syntax and Commands:
Variable Assignment: Performed using the directional arrow operator:
<-(or=in specific parameter calls).File Ingestion:
read.csv("filename.csv")reads external structured tables into an R data frame object.Data Structure Inspection:
str(data_object): Prints variable structures, computational storage modes, and initial values.dplyr::glimpse(data_object): Provides a tidy, formatted view of column types and sample entries.summary(data_object): Calculates minimum, 1st quartile, median, mean, 3rd quartile, and maximum values for numerical parameters.head(data_object)/tail(data_object): Displays the first six or last six rows of the dataset.
Plotting Architectures (
ggplot2):Built upon the "Grammar of Graphics" framework; plots are constructed hierarchically in layers connected by the
+operator.Core Functions and Aesthetics:
ggplot(data = d, aes(x = IV, y = DV, fill = Group)): Initializes the plotting object and aesthetic mappings.geom_boxplot(alpha = 0.5): Generates boxplot geometry with 50% opacity/transparency.scale_fill_brewer(palette = "Set2"): Defines the color palette.theme_minimal(): Applies an uncluttered layout removing heavy default backgrounds.labs(title = "...", x = "...", y = "..."): Adds descriptive label layers.
Specialized Styling Packages:
ggsciprovides institutional scientific themes, including automated palettes aligned with Nature publishing standards.
Data Simulation Paradigms in Modern Psychology:
Elementary Parameter Generation: Synthetic generation of numerical variables based on defined covariance matrices using basic R commands.
Agentic LLM-Driven Simulation: Cutting-edge research methodologies (e.g., Centaur,
psych-101) fine-tune open-weight models to simulate psychologically realistic human survey participants across diverse demographic, cognitive, and personality profiles to pilot interventions.
Measurement Scales and Descriptive Statistics
Typology of Measurement Scales (Stevens' Typology):
Nominal Scale: Categorical identifiers lacking quantitative magnitude, rank, or direction.
Examples: Gender (male, female, non-binary), categorical parenting classifications (authoritative, authoritarian, permissive, neglectful).
Ordinal Scale: Ordered categories possessing directionality, but lacking uniform, mathematically equal intervals between ranks.
Examples: Socioeconomic status tiers (low, middle, high), academic rankings.
Interval Scale: Quantitative measurements with strictly equal intervals between points, but lacking a true absolute zero point.
Examples: Likert-type scales (often treated analytically as continuous), Fahrenheit and Celsius temperature scales. Mathematical operations such as doubling are invalid ( is not physically twice as hot as because does not represent the complete absence of thermal energy).
Ratio Scale: Quantitative metrics featuring equal measurement intervals and an objective, absolute zero point denoting the total absence of the measured property.
Examples: Monetary currency ( dollars indicates no money; dollars is exactly twice the value of dollars), Kelvin thermal scale ( = absolute zero), absolute physical aggression scores.
Standard Academic Tables:
Descriptive Statistics Tables: Typically located within the Methods section; details demographics, percentages, sample sizes (), means (), standard deviations (), and parameter variances.
Inferential Regression Tables: Located within the Results section; details unstandardized or standardized beta coefficients, standard errors (), confidence intervals (), model fits (, adjusted ), and overall significance tests (-tests).
Automated Table Generation: The R package
stargazerautomatically constructs formatted tables conforming to American Psychological Association (APA) or economics journal specifications directly from model objects.
Linear Regression Modeling and Dummy Coding
The General Linear Model Formulation:
In R, linear models are specified via
lm(y ~ x, data = d), where the tilde (~) denotes "modeled as a function of / predicted by."Equivalence in Bivariate Designs: When a simple linear regression features a single continuous independent variable, the model coefficient of determination () equals the exact square of the Pearson correlation coefficient ().
Mathematical Mechanics of Dummy Coding:
To integrate a nominal variable with distinct groups into a numerical regression, the system must generate binary dummy indicator variables (d \in \{0, 1\}$).\n * *Omitted Reference Category:* One baseline category is omitted from the dummy set and absorbed into the constant intercept term (\beta_0).\n * *Four-Level Parenting Style Case Study:*\n * Parenting styles: Authoritative (Baseline), Authoritarian (d_{\text{auth}}d_{\text{perm}}d_{\text{negl}}).\n * Dummy Variable Matrix Formulation:\n * Authoritative: d_{\text{auth}} = 0d_{\text{perm}} = 0d_{\text{negl}} = 0\n * Authoritarian: d_{\text{auth}} = 1d_{\text{perm}} = 0d_{\text{negl}} = 0\n * Permissive: d_{\text{auth}} = 0d_{\text{perm}} = 1d_{\text{negl}} = 0\n * Neglectful: d_{\text{auth}} = 0d_{\text{perm}} = 0d_{\text{negl}} = 1\n * Regression Equation: Y_{\text{aggression}} = \beta_0 + \beta_1 d_{\text{auth}} + \beta_2 d_{\text{perm}} + \beta_3 d_{\text{negl}}\n\n* **Deconstructing Group Estimators from Regression Output:**\n * *Baseline Mean Calculation:* When an individual is from the authoritative category (d_{\text{auth}} = 0d_{\text{perm}} = 0d_{\text{negl}} = 0Y = \beta_0\beta_0 = 3535.\n * *Comparative Group Mean Calculation:* The regression slope coefficient reflects the differential offset from the baseline.\n * If the coefficient for permissive parenting is \beta_2 = 17p < 0.001p = 2 \times 10^{-16}), the group mean equals:\n Y_{\text{permissive}} = \beta_0 + \beta_2 = 35 + 17 = 52\n * *Re-Leveling Reference Groups:* If Permissive parenting is re-leveled as the baseline, the intercept adjusts to \beta_0 = 52\beta = -17), producing an equivalent estimate:\n Y = 52 - 17 = 35\n\n\n# Advanced Regression, Model Comparison, and Publication-Quality Reporting\n\n* **Multivariate Adjustments and Covariates:**\n * Covariates (e.g., biological sex, socioeconomic status) are included additively to evaluate whether primary group effects survive statistical control.\n * If gender and SES are coded such that baseline values are zero (e.g., female, high SES), the intercept captures the outcome mean for baseline parenting under baseline covariate conditions.\n\n* **Hierarchical Model Comparison via Analysis of Variance (ANOVA):**\n * *Stepwise Testing Protocol:*\n * *Model 1 (Demographic Baseline):* \text{Depressive Symptoms} = \beta_0 + \beta_1(\text{Age}) + \beta_2(\text{Sex})\n * *Model 2 (Psychological Extension):* Model 1 terms plus Perceived Stress, Neuroticism, Conscientiousness, and Social Support.\n * *ANOVA Testing:* An F\Delta R^2) significantly exceeds chance.\n\n* **Coefficient Visualization (Forest Plots):**\n * Displays point estimates of unstandardized or standardized regression weights accompanied by horizontal 95\% confidence interval bars.\n * *Evaluation of Standardization:* Direct comparisons of effect sizes across predictors require standardized coefficients (\beta) to reconcile divergent underlying metric scales (e.g., comparing Likert scores with raw monetary values).\n * *Interpretation of Confidence Intervals:* Extremely narrow confidence intervals indicate high precision in the estimated relationship between predictor and outcome; this reflects low standard errors of the regression slope, not an absence of variance in the raw variables.\n\n\n# Questions and Discussion\n\n* **Definition of an Scientific Instrument:**\n * *Question:* What constitutes an "instrument" in empirical scientific research?\n * *Response:* An instrument is any tool or apparatus deployed to measure a target property or construct (e.g., a physical ruler measuring distance, a spectrometer measuring light wavelengths, or a psychometric survey measuring implicit attitudes). An instrumental variable is a variable that serves as an imperfect measurement or proxy for an underlying construct.\n\n* **Primary Causes of Non-Causality in Correlation:**\n * *Question:* What are the two primary reasons why statistical correlation does not establish causation?\n * *Response:* \n 1. *The Third-Variable Problem:* The observed statistical association is confounded by an unmodeled external variable influencing both parameters.\n 2. *Reverse Causality:* The directional flow of causal impact is unresolved, or the two variables exert simultaneous reciprocal feedback on each other.\n\n* **Sibling Sex Composition as a Predictor of Family Size:**\n * *Question:* Why does the sex distribution of the first two children serve as a viable predictor of total family size?\n * *Response:* Parental preferences frequently favor having children of both sexes. Parents whose first two children are of the same biological sex (e.g., two boys or two girls) are statistically more likely to attempt to conceive a third child to obtain sibling sex diversity, providing an exogenous driver of overall family size.\n\n* **Role of Interpersonal Trust in Commercial Markets:**\n * *Question:* Why is mutual interpersonal trust—distinguished from trust in fiat currency—a fundamental requirement for the functioning of commercial capitalism?\n * *Response:* In environments lacking interpersonal trust and enforceable legal recourse, market actors anticipate exploitation, deceit, breach of contract, or low-quality goods without possibilities for refunds or restitution. Under such structural friction, normal commercial transactions break down.\n\n* **Exclusion Restriction for Coastal Distance:**\n * *Question:* How did researchers demonstrate that the relationship between distance to the ocean and modern trust operated solely through the historical slave trade rather than an intrinsic coastal effect?\n * *Response:* Researchers tested the association in global geographic regions and non-participating African territories that were unexposed to the transatlantic slave trade. In these regions, geographic proximity to the coast exhibited zero correlation with interpersonal trust levels, confirming that coastal distance does not intrinsically alter human social trust.\n\n* **Risks of Working Solely Within the R Console:**\n * *Question:* What is the fundamental flaw of executing end-to-end data processing directly inside the interactive R console?\n * *Response:* Console commands are transient and are lost once the application closes. This eliminates reproducibility, prevents the establishment of auditable data provenance, and impedes the construction of single-click automated research pipelines.\n\n* **Statistical Modeling of Categorical Groups:**\n * *Question:* How does a linear regression algorithm evaluate a purely nominal categorical variable possessing multiple non-numerical groups?\n * *Response:* The software creates automated dummy coding, converting the categorical variable into k - 101\beta_0$$).