correlation vs lm with two predictors I'm trying to find an equivalent result between cor.test and lm with two predictors.
dat <- data.frame(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))

x and y are correlated when d == 1
cor.test(dat$y[dat$d == 1], dat$x[dat$d == 1])
    t = 4.5776, df = 7, p-value = 0.002551

How can I reach the equivalent conclusion with lm?
mod <- lm(y ~ x * d, data=dat)


The null hypothesis you are testing against with the correlation test is:
$E[x \cdot y |d=1] = E[x|d=1] \cdot E[y|d=1]$
Your model though does not directly check for the hypothesis above, but you test for a general effect of x on y plus an additional effect of x on y given d=1.
In order to test the hypothesis as in your correlation test, you can do this:
mod <- lm(y ~ x:d, data = dat %>% dplyr::filter(d == 1))
summary(mod)

This yields the same t-statistic (and therefore also the same p-value) as in your correlation-test.
