Download presentation
Presentation is loading. Please wait.
1
SFB stats workshop Bodo Winter
2
Today Correlation, linear models intro Linear models with categorical predictors and interactions Wrap-up & recommendations
3
Multiple regression / linear model awesomeness
y ~ A + B + C + D + ... can be of any form has to be continuous
4
Regression with categorical predictors
5
Regression with categorical predictors
6
Regression with categorical predictors
7
Regression with categorical predictors
8
Regression with categorical predictors
9
Regression with categorical predictors
10
Regression with categorical predictors
11
Regression with categorical predictors
12
Regression with categorical predictors
13
Demo set.seed(666) pred = c(rep(0,20),rep(1,20))
resp = c(rnorm(20,mean=2,sd=1), rnorm(20,mean=2,sd=1)) for(i in 1:10){ resp = c(resp[1:20],resp[21:40]+1) plot(resp~pred, xlim=c(-1,2),ylim=c(0,14),xaxt="n",xlab="") axis(side=1,at=c(0,1),labels=c("A","B")) text(paste("mean of B\nequals:",i,sep="\n"), x=-0.5,y=10,cex=1.5,font=2) abline(lm(resp~pred)) Sys.sleep(1.25) }
14
Output for categorical predictors
Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) <2e-16 *** genderFemale <2e-16 *** The males are in the intercept (=109) The slope for “female” is the change with respect to the male group
15
Default: Treatment coding
16
Default: Treatment coding
pitch gender male male male male female female female female
17
Default: Treatment coding
pitch gender gender male male male male female female female female 1 these are called dummy codes by default, R does “treatment coding” and assumes that the alphanumerically first element is the reference level
18
Changing the reference level
xdata$myfac = relevel(xdata$myfac,ref=“male”) Before releveling: Estimate Std. Error t value Pr(>|t|) (Intercept) <2e-16 *** genderFemale <2e-16 *** After releveling: (Intercept) <2e-16 *** myfactor.revmale <2e-16 ***
19
Other coding schemes
20
Other coding schemes
21
the intercept is now the mean of all pitch values (ignoring gender)
Other coding schemes the intercept is now the mean of all pitch values (ignoring gender)
22
this is now the difference from the mean (=55)
Other coding schemes This is called sum coding this is now the difference from the mean (=55)
23
Other coding schemes This is called deviation coding
24
Female = 0, Male = 1 (Treatment coding) Estimate Std
Female = 0, Male = 1 (Treatment coding) Estimate Std. Error t value Pr(>|t|) (Intercept) e-09 *** gender e-06 *** Female = -1, Male = 1 (Sum coding) (Intercept) e-09 *** gender e-06 *** Female = -0.5, Male = 0.5 (Deviation coding) gender e-06 ***
25
An extended linear Model
Y ~ b b1*X b2*X error coefficients predictors can be continuous or categorical and there can be (in principle) infinitely many
26
Interactions
27
Continuous * categorical interaction
28
Continuous * categorical interaction
29
Continuous * categorical interaction
RT ~ Noise + Gender
30
Continuous * categorical interaction
RT ~ Noise + Gender + Noise:Gender RT ~ Noise * Gender
31
Interpreting cont * cat interactions
Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 Y ~ b b1*X b2*X b3*(X1*X2) interaction term
32
Interpreting cont * cat interactions
Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 say you wanted the prediction for noise = 10 for females Y ~ b b1*X b2*X b3*(X1*X2)
33
Interpreting cont * cat interactions
Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 say you wanted the prediction for noise = 10 for females Y ~ b1*X b2*X b3*(X1*X2)
34
Interpreting cont * cat interactions
Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 say you wanted the prediction for noise = 10 for females Y ~ * b2*X b3*(X1*X2)
35
Interpreting cont * cat interactions
Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 say you wanted the prediction for noise = 10 for females Y ~ * (-150)* b3*(X1*X2)
36
Interpreting cont * cat interactions
Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 say you wanted the prediction for noise = 10 for females Y ~ * (-150)* *(10*1) “1” for female and “10” for the noise value we wanted
37
Interpreting cont * cat interactions
Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 What about the men? Y ~ * (-150)* *(1*10)
38
Interpreting cont * cat interactions
Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 What about the men? Y ~ * (-150)* *(0*10)
39
Interactions between continuous variables
No interaction Interaction
40
Interactions between continuous variables
No interaction Interaction
41
Interactions between continuous variables
No interaction Interaction
42
Interactions between continuous variables
No interaction Interaction
43
Interpreting continuous interactions
Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) ** word_frequency CD <2e-16 *** word_frequency:CD <2e-16 *** Y ~ b b1*X b2*X b3*(X1*X2)
44
Interpreting continuous interactions
Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) ** word_frequency CD <2e-16 *** word_frequency:CD <2e-16 *** Y ~ (-1.6)*X *X *(X1*X2) Predictions for word frequency 3 and CD 50 Y = (-1.6)* * *(3*50)
45
Interpreting continuous interactions
Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) ** word_frequency CD <2e-16 *** word_frequency:CD <2e-16 *** Y ~ (-1.6)*X *X *(X1*X2) Predictions for word frequency 3 and CD 50 Y = 801
46
Sign of the interaction
Coefficients: Estimate (Intercept) 10 A +1 B +1 A:B +1
47
Sign of the interaction
Coefficients: Estimate (Intercept) 10 A +1 B +1 A:B -1
48
Sign of the interaction
Coefficients: Estimate (Intercept) 10 A -1 B -1 A:B +1
49
Categorical interactions: 2 X 2 example output with treatment coding
attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) attitudepol genderM attitudepol:genderM informal polite female male
50
Categorical interactions: 2 X 2 example output with treatment coding
attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) attitudepol genderM attitudepol:genderM informal polite female male
51
Categorical interactions: 2 X 2 example output with treatment coding
attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) attitudepol genderM attitudepol:genderM informal polite female 260 male
52
Categorical interactions: 2 X 2 example output with treatment coding
attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) attitudepol genderM attitudepol:genderM informal polite female 260 male
53
Categorical interactions: 2 X 2 example output with treatment coding
attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) attitudepol genderM attitudepol:genderM informal polite female 260 260-30 male
54
Categorical interactions: 2 X 2 example output with treatment coding
attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) attitudepol genderM attitudepol:genderM informal polite female 260 260-30 male
55
Categorical interactions: 2 X 2 example output with treatment coding
attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) attitudepol genderM attitudepol:genderM informal polite female 260 260-30 male
56
Categorical interactions: 2 X 2 example output with treatment coding
attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) attitudepol genderM attitudepol:genderM informal polite female 260 260-30 male
57
Categorical interactions: 2 X 2 example output with treatment coding
attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) attitudepol genderM attitudepol:genderM informal polite female 260 260-30 male
58
Categorical interactions: 2 X 2 example output with treatment coding
attitude = “inf” or “pol” gender = “F” or “M” Opposite sign means that the effect of politeness is smaller for males Estimate (Intercept) attitudepol genderM attitudepol:genderM informal polite female 260 230 male 140 125
59
Today Correlation, linear models intro Linear models with categorical predictors and interactions Wrap-up & recommendations
60
… or, as Uncle Ben would say
61
“A world of subjectivity”
Sarah Depaoli (UC Merced) “IF YOU BEAT THE DATA, AT SOME TIME IT WILL SPEAK” data loss - illustrate how proportions loose data by 2/4 … and 30/60 ….etc.
63
Your regression equation = Your experiment = Your theory
My philosophy Your regression equation = Your experiment = Your theory
64
design analysis
65
Open, reproducible research
“BE HONEST…” “NOT PURE” John McArdle
67
Stay in touch (+ upcoming book)
Newsletter on my webpage: bodowinter.com
68
Reading list (1) Will send R tutorial for recap (2) Read my linear models tutorial (3) Buy (4) Datacamp ggplot2 tutorial + Coursera “data specialization”
69
Bedtime reading
70
“What we observe is not nature itself but nature exposed to our method of questioning.”
Werner Heisenberg
71
THANK YOU
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.