fit1=lm(mpg~cyl+hp,data=mtcars)
summary(fit1)
##
## Call:
## lm(formula = mpg ~ cyl + hp, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.4948 -2.4901 -0.1828 1.9777 7.2934
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 36.90833 2.19080 16.847 < 2e-16 ***
## cyl -2.26469 0.57589 -3.933 0.00048 ***
## hp -0.01912 0.01500 -1.275 0.21253
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.173 on 29 degrees of freedom
## Multiple R-squared: 0.7407, Adjusted R-squared: 0.7228
## F-statistic: 41.42 on 2 and 29 DF, p-value: 3.162e-09
hist(fit1$residuals)

plot(mtcars$mpg,fit1$fitted.values)

dim(mtcars)
## [1] 32 11
data1=mtcars[c(1:16),]
fit1_1=lm(mpg~cyl+hp,data=data1)
summary(fit1_1)
##
## Call:
## lm(formula = mpg ~ cyl + hp, data = data1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.6288 -0.8117 0.5287 0.9580 2.9599
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 29.54400 2.63039 11.232 4.6e-08 ***
## cyl -0.47767 0.80376 -0.594 0.5625
## hp -0.05704 0.02370 -2.407 0.0317 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.055 on 13 degrees of freedom
## Multiple R-squared: 0.7872, Adjusted R-squared: 0.7545
## F-statistic: 24.05 on 2 and 13 DF, p-value: 4.281e-05
data2=mtcars[c(13:20),]
fit1_2=lm(mpg~cyl+hp,data=data2)
summary(fit1_2)
##
## Call:
## lm(formula = mpg ~ cyl + hp, data = data2)
##
## Residuals:
## Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental
## 2.4885 0.3885 -3.0348 -2.4841
## Chrysler Imperial Fiat 128 Honda Civic Toyota Corolla
## 2.6419 0.4420 -2.3289 1.8869
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 46.46121 5.93709 7.826 0.000546 ***
## cyl -2.71718 2.20013 -1.235 0.271694
## hp -0.05507 0.06077 -0.906 0.406357
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.753 on 5 degrees of freedom
## Multiple R-squared: 0.9455, Adjusted R-squared: 0.9237
## F-statistic: 43.37 on 2 and 5 DF, p-value: 0.0006934