set.seed(1)
x1 <- runif(100)
x2 <- 0.5 * x1 + rnorm(100) / 10
y <- 2 + 2 * x1 + 0.3 * x2 + rnorm(100)
# Fit the linear regression model
lm_fit <- lm(y ~ x1 + x2)
# Print the summary to get the regression coefficients
summary(lm_fit)
##
## Call:
## lm(formula = y ~ x1 + x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.8311 -0.7273 -0.0537 0.6338 2.3359
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.1305 0.2319 9.188 7.61e-15 ***
## x1 1.4396 0.7212 1.996 0.0487 *
## x2 1.0097 1.1337 0.891 0.3754
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.056 on 97 degrees of freedom
## Multiple R-squared: 0.2088, Adjusted R-squared: 0.1925
## F-statistic: 12.8 on 2 and 97 DF, p-value: 1.164e-05
# Calculate the correlation between x1 and x2
cor(x1, x2)
## [1] 0.8351212
# Create a scatterplot of x1 and x2
plot(x1, x2, main = "Scatterplot of x1 vs x2", xlab = "x1", ylab = "x2", pch = 19, col = "blue")

# Fit the regression model with x1 and x2
lm_fit <- lm(y ~ x1 + x2)
# Print the summary of the regression results
summary(lm_fit)
##
## Call:
## lm(formula = y ~ x1 + x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.8311 -0.7273 -0.0537 0.6338 2.3359
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.1305 0.2319 9.188 7.61e-15 ***
## x1 1.4396 0.7212 1.996 0.0487 *
## x2 1.0097 1.1337 0.891 0.3754
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.056 on 97 degrees of freedom
## Multiple R-squared: 0.2088, Adjusted R-squared: 0.1925
## F-statistic: 12.8 on 2 and 97 DF, p-value: 1.164e-05
# Fit the regression model with only x1
lm_fit_x1 <- lm(y ~ x1)
# Print the summary of the regression results
summary(lm_fit_x1)
##
## Call:
## lm(formula = y ~ x1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.89495 -0.66874 -0.07785 0.59221 2.45560
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.1124 0.2307 9.155 8.27e-15 ***
## x1 1.9759 0.3963 4.986 2.66e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.055 on 98 degrees of freedom
## Multiple R-squared: 0.2024, Adjusted R-squared: 0.1942
## F-statistic: 24.86 on 1 and 98 DF, p-value: 2.661e-06
# Fit the regression model with only x2
lm_fit_x2 <- lm(y ~ x2)
# Print the summary of the regression results
summary(lm_fit_x2)
##
## Call:
## lm(formula = y ~ x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.62687 -0.75156 -0.03598 0.72383 2.44890
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.3899 0.1949 12.26 < 2e-16 ***
## x2 2.8996 0.6330 4.58 1.37e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.072 on 98 degrees of freedom
## Multiple R-squared: 0.1763, Adjusted R-squared: 0.1679
## F-statistic: 20.98 on 1 and 98 DF, p-value: 1.366e-05
x1 <- c(x1, 0.1)
x2 <- c(x2, 0.8)
y <- c(y, 6)
# Re-fit the models with the new observation
lm_fit_new <- lm(y ~ x1 + x2)
lm_fit_x1_new <- lm(y ~ x1)
lm_fit_x2_new <- lm(y ~ x2)
# Print the summaries of the new models
summary(lm_fit_new)
##
## Call:
## lm(formula = y ~ x1 + x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.73348 -0.69318 -0.05263 0.66385 2.30619
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.2267 0.2314 9.624 7.91e-16 ***
## x1 0.5394 0.5922 0.911 0.36458
## x2 2.5146 0.8977 2.801 0.00614 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.075 on 98 degrees of freedom
## Multiple R-squared: 0.2188, Adjusted R-squared: 0.2029
## F-statistic: 13.72 on 2 and 98 DF, p-value: 5.564e-06
summary(lm_fit_x1_new)
##
## Call:
## lm(formula = y ~ x1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.8897 -0.6556 -0.0909 0.5682 3.5665
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.2569 0.2390 9.445 1.78e-15 ***
## x1 1.7657 0.4124 4.282 4.29e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.111 on 99 degrees of freedom
## Multiple R-squared: 0.1562, Adjusted R-squared: 0.1477
## F-statistic: 18.33 on 1 and 99 DF, p-value: 4.295e-05
summary(lm_fit_x2_new)
##
## Call:
## lm(formula = y ~ x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.64729 -0.71021 -0.06899 0.72699 2.38074
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.3451 0.1912 12.264 < 2e-16 ***
## x2 3.1190 0.6040 5.164 1.25e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.074 on 99 degrees of freedom
## Multiple R-squared: 0.2122, Adjusted R-squared: 0.2042
## F-statistic: 26.66 on 1 and 99 DF, p-value: 1.253e-06
# Calculate leverage and residuals
hatvalues(lm_fit_new)
## 1 2 3 4 5 6 7
## 0.01905793 0.01594849 0.01097584 0.04036164 0.03579376 0.05738552 0.03643856
## 8 9 10 11 12 13 14
## 0.02106144 0.01367365 0.03845500 0.06007045 0.02568754 0.01702432 0.01222831
## 15 16 17 18 19 20 21
## 0.02305638 0.01005414 0.03916306 0.05642240 0.01240634 0.05168118 0.03585426
## 22 23 24 25 26 27 28
## 0.02693541 0.01479629 0.03817939 0.03081323 0.01247725 0.04657630 0.01231205
## 29 30 31 32 33 34 35
## 0.02737875 0.01715106 0.01280296 0.01114580 0.01868965 0.04300077 0.02576320
## 36 37 38 39 40 41 42
## 0.01380719 0.02829778 0.03375430 0.01677644 0.01161848 0.02509987 0.02185972
## 43 44 45 46 47 48 49
## 0.02889894 0.01304567 0.02631106 0.02236651 0.05657476 0.01289427 0.02746237
## 50 51 52 53 54 55 56
## 0.01611627 0.01332252 0.02653629 0.01734972 0.01989190 0.04102604 0.05274307
## 57 58 59 60 61 62 63
## 0.01808709 0.01499209 0.01376303 0.02960779 0.03473115 0.01865022 0.02338266
## 64 65 66 67 68 69 70
## 0.01814573 0.01291854 0.02052027 0.01108508 0.01933631 0.03633142 0.02815957
## 71 72 73 74 75 76 77
## 0.01648355 0.03678740 0.01433799 0.01482634 0.01027552 0.03307247 0.02688469
## 78 79 80 81 82 83 84
## 0.01209299 0.02294836 0.03814756 0.01076185 0.01803646 0.01316211 0.03261485
## 85 86 87 88 89 90 91
## 0.01860412 0.04179835 0.01604374 0.03386478 0.02365038 0.02897279 0.04829557
## 92 93 94 95 96 97 98
## 0.04627626 0.03248348 0.02958189 0.02865524 0.02514269 0.03892864 0.01139885
## 99 100 101
## 0.03397113 0.03088514 0.41472837
residuals(lm_fit_new)
## 1 2 3 4 5 6
## 0.229129803 0.021748235 -0.417928297 -0.585100908 -1.960255772 -1.423078153
## 7 8 9 10 11 12
## 1.188391563 -0.383283811 -1.515036285 1.694335259 -0.260680474 -0.394275660
## 13 14 15 16 17 18
## 0.921752176 0.789224132 -0.409365559 2.113426382 0.171518983 -1.625327432
## 19 20 21 22 23 24
## -0.270773180 -0.225640878 2.306190357 0.111277987 0.325277943 -0.052626703
## 25 26 27 28 29 30
## -0.188664204 -0.189555851 0.663848420 1.983396837 1.091477883 1.232007180
## 31 32 33 34 35 36
## -1.161772358 0.998952075 -0.093306353 -1.290751174 0.455088333 -0.223027500
## 37 38 39 40 41 42
## 1.283050327 -0.887276233 -0.483177281 -1.066658875 0.006369449 0.136419936
## 43 44 45 46 47 48
## -0.938828055 0.643988515 -1.599052781 -1.119496522 1.505418531 -0.946991840
## 49 50 51 52 53 54
## 0.715190539 -0.258204751 0.488825988 1.757099019 1.716395656 -0.506115185
## 55 56 57 58 59 60
## -2.341995764 1.914730802 0.393385679 0.296292050 -0.091301510 0.054610315
## 61 62 63 64 65 66
## 0.072217500 0.399960260 -0.781950584 -1.335371445 1.037009114 1.471194380
## 67 68 69 70 71 72
## -0.295509284 -1.147451027 0.335870525 0.077099888 -1.728076139 -0.225431326
## 73 74 75 76 77 78
## -0.686997136 -0.409981721 -1.192792872 1.733817202 -0.236182353 -1.686096992
## 79 80 81 82 83 84
## 0.396068608 0.447658741 -1.072286775 -2.733483562 -0.843568691 0.795013801
## 85 86 87 88 89 90
## -0.086843937 0.086993239 0.652004045 -1.253168324 1.101221340 -0.168809624
## 91 92 93 94 95 96
## 0.989222353 0.567673547 0.592413173 -0.693182765 1.458572647 -1.778912822
## 97 98 99 100 101
## -1.072862619 -0.341342108 0.178508105 1.370803848 1.707708783