Correlation and regression in R

Measure association with cor.test(), fit a linear regression with lm(), and interpret coefficients, p-values, and R-squared. Runnable examples included.

About 16 minutes, every example runs in your browser

When both variables are continuous, the questions change: how strongly are they related (correlation), and can one predict the other (regression)? We will use car weight and fuel efficiency from mtcars, a relationship you can guess before computing it: heavier cars burn more fuel.

Correlation with cor() and cor.test()

Pearson's r runs from -1 (perfect negative) through 0 (none) to +1 (perfect positive). cor() gives the number; cor.test() adds the significance test and confidence interval you need for a paper.

cor(mtcars$wt, mtcars$mpg)

cor.test(mtcars$wt, mtcars$mpg)

Output

[1] -0.8676594

	Pearson's product-moment correlation

data:  mtcars$wt and mtcars$mpg
t = -9.559, df = 30, p-value = 1.294e-10
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.9338264 -0.7440872
sample estimates:
       cor
-0.8676594

r = -.87 is a very strong negative association. Note the scientific notation: 1.294e-10 means p is about 0.0000000001, which APA reports simply as p < .001. If your data are skewed or ordinal, add method = "spearman" for a rank-based alternative.

Regression with lm()

Correlation is symmetric; regression fits a prediction line. lm() (linear model) uses the familiar formula, here "predict mpg from weight":

model <- lm(mpg ~ wt, data = mtcars)
summary(model)

Output

Call:
lm(formula = mpg ~ wt, data = mtcars)

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
wt           -5.3445     0.5591  -9.559 1.29e-10 ***
---
Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared:  0.7528,	Adjusted R-squared:  0.7446
F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

Reading the summary

  • Intercept 37.29: predicted mpg for a hypothetical weightless car; often not meaningful by itself
  • Slope -5.34: the substantive result: each extra 1000 lbs of weight predicts about 5.3 fewer miles per gallon
  • R-squared .75: weight alone accounts for about 75 percent of the variance in fuel efficiency
  • The F line: tests the model as a whole; with one predictor it echoes the slope's t-test (t squared equals F: 9.559 squared is 91.4)

Reporting it in APA style

Vehicle weight was strongly negatively associated with fuel efficiency, r(30) = -.87, p < .001. In a simple linear regression, weight significantly predicted mpg, b = -5.34, t(30) = -9.56, p < .001, R² = .75.

One caution that belongs in every methods course: correlation is not causation. Heavier cars do not choose to burn fuel; the design, not the statistic, is what supports a causal claim. Next lesson: making all of this visible with plots.

Ready to turn your spreadsheet into results?

Create an account and run your first analysis in minutes. No install, no statistics course required.