I wonder whether you really mean that d
is continuous. If you take d
as categorical with two levels you could do:
library(data.table)
library(ggplot2)
dat <- data.table(y=c(1.4, 6.3, 3.2, 1.6, 4.3, 4.5, 8.4, 2.2, 4.2, 6.3, 8.3, 2.2, 1.1, 5.3, 2.2, 1.8, 7.5,1.4),
x=c(22.2,44.3,13.3,11.4,57.3,54.8,78.5,22.6,45.6,65.4,14.5,78.9,14.4,67.4,11.1,66.8,91.4,39.6),
d=c(1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0))
# Make d categorical, set '1' as reference level:
dat[, d := as.factor(d)]
dat[, d := relevel(d, ref= '1')]
Now the output of the regression recapitulates the correlation:
mod <- lm(y ~ x * d, data= dat)
summary(mod)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.66304 1.54848 0.428 0.6750
x 0.08609 0.03482 2.473 0.0268 *
d0 2.27474 2.15567 1.055 0.3092
x:d0 -0.06460 0.04345 -1.487 0.1592
(Intercept)
is the value of y
when x == 0
and d == 1
x
is the change in y for 1 unit change in x when d == 1
(i.e. the slope of the regression line). The slope is different from 0 at the significance level 0.0268 (compared to 0.002 when regressing using only the data at d == 1
).
d0
is the change in intercept of the regression line between d == 1
and d == 0
. I.e. the regression line when d == 0 has intercept 0.66304 + 2.27474
x:d0
Is the difference in slopes between the regression line with d == 1
and d == 0
(effect of interaction between x
and d
). I.e. the regression line when d == 0 has slope 0.08609 - 0.06460
You can visualize this with:
ggplot(data= dat, aes(x, y, colour= d)) +
geom_point() +
geom_smooth(se= FALSE, method= 'lm') +
xlim(c(0, NA)) +
theme_light()
