Data Analysis for Psychology in R 2
Department of Psychology
University of Edinburgh
2026–2027
|
Introduction to linear models (with Dr. Patrick Sturt) |
Introduction to linear regression |
| Interpreting linear models | |
| Testing predictors and evaluating linear models | |
| F-tests and model comparison | |
| Practice analysis: Linear models | |
|
Extending linear models (with Dr. Elizabeth Pankratz) |
Categorical predictors and treatment coding |
| Sum coding | |
| Assumptions and diagnostics | |
| Bootstrapping and confidence intervals | |
| Practice analysis: Categorical predictors (led by Patrick) |
|
Interactions (with Dr. Elizabeth Pankratz) |
Mean-centering and numeric/categorical interactions |
| Numeric/numeric interactions | |
| Categorical/categorical interactions | |
| Testing simple effects and correcting for multiple comparisons | |
| Practice analysis: Interactions (led by Patrick) | |
|
Logistic regression (with Dr. Elizabeth Pankratz) |
Probabilities and log-odds |
| Modelling binary outcomes with logistic regression | |
| Interactions, assumptions, diagnostics, comparisons | |
| Practice analysis: Logistic regression (led by Patrick) | |
| Report feedback, exam prep, full-course Q&A |
Understand the link between models and functions
Understand the key concepts (intercept and slope) of the linear model
Understand what residuals represent
Understand the key principles of least squares
Be able to specify a simple linear model (labs)
Pretty much all statistics is about models
A model is a formal representation of a system
Put another way, a model is an idea about the way the world is
We tend to represent mathematical models as functions
A function is an expression that defines the relationship between one variable (or set of variables) and another variable (or set of variables)
It allows us to specify what is important (arguments) and how these things interact with each other (operations)
This allows us to make and test predictions
To think through these relations, we can use a simpler example
Suppose I have a model for how long a baby’s body is, depending on how old the baby is:
\[ \text{Length} = 55 + 4 \times \text{Age} \]
Length is measured in cm; Age is measured in months
I’m using this model to formally represent the relationship between a baby’s age and their length
The x-axis shows Age
The y-axis shows Length
The black line represents our model: \(y = 55+4x\)
| Age | PredictedLength |
|---|---|
| 10.00 | 95 |
| 10.25 | 96 |
| 10.50 | 97 |
| 10.75 | 98 |
| 11.00 | 99 |
| 11.25 | 100 |
| 11.50 | 101 |
What does this say about our model?
If we were to collect actual data on height and age, will our observations fall on the line?
| Age | Year | Prediction_cm | Prediction_m |
|---|---|---|---|
| 216 | 18 | 919 | 9.19 |
| 228 | 19 | 967 | 9.67 |
| 240 | 20 | 1015 | 10.15 |
| 252 | 21 | 1063 | 10.63 |
| 264 | 22 | 1111 | 11.11 |
| 276 | 23 | 1159 | 11.59 |
| 288 | 24 | 1207 | 12.07 |
| 300 | 25 | 1255 | 12.55 |
We represent the model as a function. For linear regression models, that function is a line.
The model yields predictions: values we expect if our model is true.
We can collect data and see if the model’s predictions match the observed data.
If they deviate, then that suggests our model is not so good.
A deterministic model is a model for an exact relationship:
\[ y = \underbrace{3 + 2 x}_{f(x)} \]
A statistical model allows for case-by-case variability:
\[ y = \underbrace{3 + 2 x}_{f(x)} + \epsilon \]
Our focus in DAPR2 is on the linear model:
Assumes the relationship between the outcome variable and the predictor(s) is linear
Describes an outcome variable as a function of one or more predictor variables
In Blocks 1–3, we’ll look at continuous numeric outcome variables; in Block 4, we’ll learn how to deal with binary outcome variables (e.g., yes/no, correct/incorrect)
Question: Do students who study more get higher scores on the test?
| student | hours | score |
|---|---|---|
| ID1 | 0.5 | 1 |
| ID2 | 1.0 | 3 |
| ID3 | 1.5 | 1 |
| ID4 | 2.0 | 2 |
| ID5 | 2.5 | 2 |
| ID6 | 3.0 | 6 |
| ID7 | 3.5 | 3 |
| ID8 | 4.0 | 3 |
| ID9 | 4.5 | 4 |
| ID10 | 5.0 | 8 |
Codebook
student = ID variable unique to each respondent
hours = the number of hours spent studying. This will be our predictor (\(x\))
score = test score. This will be our outcome (\(y\))
The line can be described by two values, called “parameters”:
Intercept: the point where the line crosses (or “intercepts”) the \(y\)-axis, or in other words, the point where \(x = 0\)
Slope: the gradient of the line, or rate of change


\[y_i = \beta_0 + \beta_1 x_{i} + \epsilon_i\]
\(y_i\) = the outcome variable (e.g. score)
\(x_i\) = the predictor variable, (e.g. hours)
\(\beta_0\) = intercept
\(\beta_1\) = slope
\(\epsilon_i\) = residual (we will come to this shortly)
\[y_i = \beta_0 + \beta_1 x_{i} + \epsilon_i\]
\(i\) is a subscript to indicate that each participant has their own value.
So each participant has their own:
The line represents a model of our data.
In the scatterplot, the data are represented by points
So a good line is a line that is “close” to all points
The method that we use to identify the best-fitting line is the Principle of Least Squares
\[y_i = \beta_0 + \beta_1 x_{i} + \epsilon_i\]
Where
How do we calculate \(\beta_0\) and \(\beta_1\)?
The values \(\beta_0\) and \(\beta_1\) are typically unknown and need to be estimated from our data.
We find the values of \(\hat \beta_0\) and \(\hat \beta_1\) (and thus our best line) using least squares
Least squares:
minimises the distances between the actual values of \(y\) and the model-predicted values of \(\hat y\)
that is, it minimises the residuals for each data point (the line is “close” to the data points)
The best line is the one that minimises the residual sum of squares.
Why do you think we square the residuals?
\[SS_{Residual} = \sum_{i=1}^{n}(y_i - \hat{y}_i)^2\]
\[SS_{Residual} = \sum_{i=1}^{n}(\color{#BF1932}{y_i} - \hat{y}_i)^2\]
\[SS_{Residual} = \sum_{i=1}^{n}(y_i - \color{#BF1932}{\hat{y}_i})^2\]
\[SS_{Residual} = \sum_{i=1}^{n}(\color{#BF1932}{y_i - \hat{y}_i})^2\]
The values of the intercept and slope that minimise the sum of square residual are our estimated coefficients from our data
Minimising the \(SS_{residual}\) means that across all our data, the predicted values from our model are as close as they can be to the actual measured values of the outcome
\[\hat \beta_1 = \frac{SP_{xy}}{SS_x}\]
\[SP_{xy} = \sum_{i=1}^{n}(x_i - \bar{x})(y_i - \bar{y})\]
\[SS_x = \sum_{i=1}^{n}(x_i - \bar{x})^2\]
hours)scores)\[\hat \beta_0 = \bar{y} - \hat \beta_1 \bar{x}\]
\[\hat{y}_i = \color{blue}{b_0 \cdot{}}\color{orange}{1} \color{blue}{+b_1 \cdot{}} \color{orange}{x_i}\]
values of the linear model (coefficients)
values we provide (inputs)
y ~ 1 + xlm in Rlm() functionR the name of the data set
lm in Rlm in RR, store in object named mod1:lm in Rsummary() of output in R:
Call:
lm(formula = score ~ hours, data = test)
Residuals:
Min 1Q Median 3Q Max
-1.6182 -1.0773 -0.7454 1.1773 2.4364
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.4000 1.1111 0.360 0.7282
hours 1.0545 0.3581 2.945 0.0186 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.626 on 8 degrees of freedom
Multiple R-squared: 0.5201, Adjusted R-squared: 0.4601
F-statistic: 8.67 on 1 and 8 DF, p-value: 0.01858
Tasks:
Work on exercises in labs
Complete the weekly quiz
Get support:
Consult the flash cards
Ask questions anonymously on Piazza
We really like seeing you in office hours!