Packages
library(tidyverse)
library(knitr)
library(broom) # install.packages("broom")
cov(mtcars$mpg, mtcars$hp)
[1] -320.7321
Tương quan âm, khá chặt chẽ
m1 <- lm(
mpg ~ hp,
mtcars
)
summary(m1)
Call:
lm(formula = mpg ~ hp, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-5.7121 -2.1122 -0.8854 1.5819 8.2360
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.09886 1.63392 18.421 < 2e-16 ***
hp -0.06823 0.01012 -6.742 1.79e-07 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.863 on 30 degrees of freedom
Multiple R-squared: 0.6024, Adjusted R-squared: 0.5892
F-statistic: 45.46 on 1 and 30 DF, p-value: 1.788e-07
\[ \hat{mpg}_i = 30.09 -0.06 \times hp_i \\ ~~ \\ mpg_i = 30.09 - 0.06 \times hp_i + e_i \]
confint(m1)
2.5 % 97.5 %
(Intercept) 26.76194879 33.4357723
hp -0.08889465 -0.0475619
anova(m1)
Analysis of Variance Table
Response: mpg
Df Sum Sq Mean Sq F value Pr(>F)
hp 1 678.37 678.37 45.46 1.788e-07 ***
Residuals 30 447.67 14.92
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
m2 <- lm(
hp ~ mpg - 1,
mtcars
)
summary(m2)
Call:
lm(formula = hp ~ mpg - 1, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-138.66 -42.48 11.86 83.28 244.88
Coefficients:
Estimate Std. Error t value Pr(>|t|)
mpg 6.0078 0.8673 6.927 9.06e-08 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 102.8 on 31 degrees of freedom
Multiple R-squared: 0.6075, Adjusted R-squared: 0.5948
F-statistic: 47.98 on 1 and 31 DF, p-value: 9.062e-08
confint(m2, level = 0.99)
0.5 % 99.5 %
(Intercept) 248.64087 399.523758
mpg -12.43108 -5.228378
m4 <- lm(
hp ~ .,
mtcars
)
summary(m4)
Call:
lm(formula = hp ~ ., data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-38.681 -15.558 0.799 18.106 34.718
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 79.0484 184.5041 0.428 0.67270
mpg -2.0631 2.0906 -0.987 0.33496
cyl 8.2037 10.0861 0.813 0.42513
disp 0.4390 0.1492 2.942 0.00778 **
drat -4.6185 16.0829 -0.287 0.77680
wt -27.6600 19.2704 -1.435 0.16591
qsec -1.7844 7.3639 -0.242 0.81089
vs 25.8129 19.8512 1.300 0.20758
am 9.4863 20.7599 0.457 0.65240
gear 7.2164 14.6160 0.494 0.62662
carb 18.7487 7.0288 2.667 0.01441 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 25.97 on 21 degrees of freedom
Multiple R-squared: 0.9028, Adjusted R-squared: 0.8565
F-statistic: 19.5 on 10 and 21 DF, p-value: 1.898e-08
ggplot(mtcars, aes(hp, mpg)) +
geom_point() +
geom_smooth(method = "lm", se = F) +
geom_smooth(method = "lm", formula = y ~ x + I(x^2), se = F, col = "red") +
geom_smooth(method = "lm", formula = y ~ I(1/x), se = F, col = "yellow") +
geom_smooth(method = "lm", formula = y ~ I(1/x), se = F, col = "yellow")
m5 <- lm(
mpg ~ hp + I(hp^2),
mtcars
)
summary(m5)
Call:
lm(formula = mpg ~ hp + I(hp^2), data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.5512 -1.6027 -0.6977 1.5509 8.7213
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.041e+01 2.741e+00 14.744 5.23e-15 ***
hp -2.133e-01 3.488e-02 -6.115 1.16e-06 ***
I(hp^2) 4.208e-04 9.844e-05 4.275 0.000189 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.077 on 29 degrees of freedom
Multiple R-squared: 0.7561, Adjusted R-squared: 0.7393
F-statistic: 44.95 on 2 and 29 DF, p-value: 1.301e-09
summary(m1)
Call:
lm(formula = mpg ~ hp, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-5.7121 -2.1122 -0.8854 1.5819 8.2360
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.09886 1.63392 18.421 < 2e-16 ***
hp -0.06823 0.01012 -6.742 1.79e-07 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.863 on 30 degrees of freedom
Multiple R-squared: 0.6024, Adjusted R-squared: 0.5892
F-statistic: 45.46 on 1 and 30 DF, p-value: 1.788e-07
anova(m5)
Analysis of Variance Table
Response: mpg
Df Sum Sq Mean Sq F value Pr(>F)
hp 1 678.37 678.37 71.633 2.514e-09 ***
I(hp^2) 1 173.04 173.04 18.273 0.0001889 ***
Residuals 29 274.63 9.47
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
anova(m1)
Analysis of Variance Table
Response: mpg
Df Sum Sq Mean Sq F value Pr(>F)
hp 1 678.37 678.37 45.46 1.788e-07 ***
Residuals 30 447.67 14.92
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1