x <- 1:30
n <- length(x)
b0 <- 20
b1 <- 1.2
set.seed(2)
e <- rnorm(n, mean = 0, sd = 5)
y <- b0 + b1 * x + e
ybar <- mean(y)
d <- data.frame(x, y)
d
## x y
## 1 1 16.71543
## 2 2 23.32425
## 3 3 31.53923
## 4 4 19.14812
## 5 5 25.59874
## 6 6 27.86210
## 7 7 31.93977
## 8 8 28.40151
## 9 9 40.72237
## 10 10 31.30606
## 11 11 35.28825
## 12 12 39.30876
## 13 13 33.63652
## 14 14 31.60166
## 15 15 46.91114
## 16 16 27.64465
## 17 17 44.79302
## 18 18 41.77903
## 19 19 47.86414
## 20 20 46.16133
## 21 21 55.65410
## 22 22 40.40037
## 23 23 55.54819
## 24 24 58.57326
## 25 25 50.02469
## 26 26 38.94147
## 27 27 54.78619
## 28 28 50.61721
## 29 29 58.76102
## 30 30 57.44818
COL <- c(rgb(255, 0, 0, 255, max = 255),
rgb( 0, 0, 255, 255, max = 255),
rgb( 0, 155, 0, 255, max = 255))
matplot(x,y,pch = 1, col = COL[1])
grid()

fit <- lm(y ~ x, data = d)
summary(fit)
##
## Call:
## lm(formula = y ~ x, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.5989 -2.8452 0.0335 3.6787 9.2076
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 20.8526 2.2356 9.327 4.38e-10 ***
## x 1.2188 0.1259 9.678 1.97e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.97 on 28 degrees of freedom
## Multiple R-squared: 0.7699, Adjusted R-squared: 0.7616
## F-statistic: 93.66 on 1 and 28 DF, p-value: 1.972e-10