Tsallaka zuwa abun ciki
Justice Tech School

Linear Regression

lm fits a line. coef gives the intercept and slope.

Example
x <- c(1, 2, 3, 4, 5)
y <- c(2.1, 3.9, 6.2, 7.8, 10.1)
fit <- lm(y ~ x)
print(round(coef(fit), 2))
print(round(summary(fit)$r.squared, 3))
print(round(predict(fit, data.frame(x = 6)), 1))

Exercise

Fit a model.

(y ~ x)

Quiz Test

What does the formula y ~ x mean?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.