Regression is a supervised learning method, which is employed to model and analyze the relationship between a dependent (response) variable and one or more independent (predictor) variables.

Linear Regression is based on minimum squared errors of the fitted values.

The fitted model can then be applied to data for continuous value predictions.

Function Used : lm

Assumptions of Linear Regression :

Fitting a Linear Regression Model

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
fit_lm <- lm(mpg~., data=mtcars)

Summary of fitted model

summary(fit_lm)
## 
## Call:
## lm(formula = mpg ~ ., data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4506 -1.6044 -0.1196  1.2193  4.6271 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 12.30337   18.71788   0.657   0.5181  
## cyl         -0.11144    1.04502  -0.107   0.9161  
## disp         0.01334    0.01786   0.747   0.4635  
## hp          -0.02148    0.02177  -0.987   0.3350  
## drat         0.78711    1.63537   0.481   0.6353  
## wt          -3.71530    1.89441  -1.961   0.0633 .
## qsec         0.82104    0.73084   1.123   0.2739  
## vs           0.31776    2.10451   0.151   0.8814  
## am           2.52023    2.05665   1.225   0.2340  
## gear         0.65541    1.49326   0.439   0.6652  
## carb        -0.19942    0.82875  -0.241   0.8122  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.65 on 21 degrees of freedom
## Multiple R-squared:  0.869,  Adjusted R-squared:  0.8066 
## F-statistic: 13.93 on 10 and 21 DF,  p-value: 3.793e-07

In Call section, the function called to generate the fitted model is displayed.

In Residuals section, it provides a quick summary (min, 1Q, median, 3Q, max) of the residual distribution which would be same as summary(fit_lm$residuals)

In Coefficients section, each coefficient is a Gaussian random variable.

Residual standard error outputs the standard deviation of residuals
degree of freedom indicates the differences between the observation in training samples and the number used in the model
Multiple R-squared is obtained by dividing the sum of squares. One can use R-squared to measure how close the data is to fit into the regression line.
Adjusted R-squared uses an unbiased estimate, and will be slightly less than multiple R-squared. It’s better to use Adjusted R-squared value for model interpretation.
F-statistic is retrieved by performing an f-test on the model.

Break down of Summary

fit_lm$coefficients
## (Intercept)         cyl        disp          hp        drat          wt 
## 12.30337416 -0.11144048  0.01333524 -0.02148212  0.78711097 -3.71530393 
##        qsec          vs          am        gear        carb 
##  0.82104075  0.31776281  2.52022689  0.65541302 -0.19941925
fit_lm$residuals
##           Mazda RX4       Mazda RX4 Wag          Datsun 710 
##        -1.599505761        -1.111886079        -3.450644085 
##      Hornet 4 Drive   Hornet Sportabout             Valiant 
##         0.162595453         1.006565971        -2.283039036 
##          Duster 360           Merc 240D            Merc 230 
##        -0.086256253         1.903988115        -1.619089898 
##            Merc 280           Merc 280C          Merc 450SE 
##         0.500970058        -1.391654392         2.227837890 
##          Merc 450SL         Merc 450SLC  Cadillac Fleetwood 
##         1.700426404        -0.542224699        -1.634013415 
## Lincoln Continental   Chrysler Imperial            Fiat 128 
##        -0.536437711         4.206370638         4.627094192 
##         Honda Civic      Toyota Corolla       Toyota Corona 
##         0.503261089         4.387630904        -2.143103442 
##    Dodge Challenger         AMC Javelin          Camaro Z28 
##        -1.443053221        -2.532181498        -0.006021976 
##    Pontiac Firebird           Fiat X1-9       Porsche 914-2 
##         2.508321011        -0.993468693        -0.152953961 
##        Lotus Europa      Ford Pantera L        Ferrari Dino 
##         2.763727417        -3.070040803         0.006171846 
##       Maserati Bora          Volvo 142E 
##         1.058881618        -2.968267683
fit_lm$effects
##   (Intercept)           cyl          disp            hp          drat 
## -1.136497e+02 -2.859568e+01  6.131391e+00 -3.061197e+00 -4.058009e+00 
##            wt          qsec            vs            am          gear 
## -8.802034e+00 -1.987287e+00  3.602342e-01  3.804502e+00 -9.857538e-01 
##          carb                                                         
## -6.377059e-01  2.679169e+00  2.072950e+00 -6.318915e-03 -1.254563e+00 
##                                                                       
##  2.453586e-02  4.730750e+00  5.654391e+00 -9.629859e-01  5.285476e+00 
##                                                                       
## -1.333950e+00 -9.287503e-01 -2.204646e+00 -7.723442e-01  3.228137e+00 
##                                                                       
## -2.849364e-01  2.549272e+00  4.116721e+00 -1.854365e+00  4.859340e-02 
##                             
##  9.356676e-01 -2.205753e+00
fit_lm$rank
## [1] 11
fit_lm$fitted.values
##           Mazda RX4       Mazda RX4 Wag          Datsun 710 
##            22.59951            22.11189            26.25064 
##      Hornet 4 Drive   Hornet Sportabout             Valiant 
##            21.23740            17.69343            20.38304 
##          Duster 360           Merc 240D            Merc 230 
##            14.38626            22.49601            24.41909 
##            Merc 280           Merc 280C          Merc 450SE 
##            18.69903            19.19165            14.17216 
##          Merc 450SL         Merc 450SLC  Cadillac Fleetwood 
##            15.59957            15.74222            12.03401 
## Lincoln Continental   Chrysler Imperial            Fiat 128 
##            10.93644            10.49363            27.77291 
##         Honda Civic      Toyota Corolla       Toyota Corona 
##            29.89674            29.51237            23.64310 
##    Dodge Challenger         AMC Javelin          Camaro Z28 
##            16.94305            17.73218            13.30602 
##    Pontiac Firebird           Fiat X1-9       Porsche 914-2 
##            16.69168            28.29347            26.15295 
##        Lotus Europa      Ford Pantera L        Ferrari Dino 
##            27.63627            18.87004            19.69383 
##       Maserati Bora          Volvo 142E 
##            13.94112            24.36827
fit_lm$assign
##  [1]  0  1  2  3  4  5  6  7  8  9 10
fit_lm$qr
## $qr
##                     (Intercept)          cyl          disp            hp
## Mazda RX4            -5.6568542 -35.00178567 -1.305160e+03 -8.297898e+02
## Mazda RX4 Wag         0.1767767   9.94359090  6.224581e+02  3.177801e+02
## Datsun 710            0.1767767   0.21715832 -2.978770e+02 -3.542113e+01
## Hornet 4 Drive        0.1767767   0.01602374  1.633784e-01  2.085379e+02
## Hornet Sportabout     0.1767767  -0.18511084  5.218988e-02  1.565722e-01
## Valiant               0.1767767   0.01602374  5.259438e-02  1.610903e-01
## Duster 360            0.1767767  -0.18511084  5.218988e-02 -1.790983e-01
## Merc 240D             0.1767767   0.21715832  2.433459e-01  8.682158e-02
## Merc 230              0.1767767   0.21715832  2.235391e-01 -7.669726e-02
## Merc 280              0.1767767   0.01602374 -1.401026e-01  2.346322e-02
## Merc 280C             0.1767767   0.01602374 -1.401026e-01  2.346322e-02
## Merc 450SE            0.1767767  -0.18511084 -2.304771e-01  5.732646e-02
## Merc 450SL            0.1767767  -0.18511084 -2.304771e-01  5.732646e-02
## Merc 450SLC           0.1767767  -0.18511084 -2.304771e-01  5.732646e-02
## Cadillac Fleetwood    0.1767767  -0.18511084  4.281840e-01  1.128340e-01
## Lincoln Continental   0.1767767  -0.18511084  3.878989e-01  5.415388e-02
## Chrysler Imperial     0.1767767  -0.18511084  3.207571e-01 -3.565418e-02
## Fiat 128              0.1767767   0.21715832  1.506376e-02  6.852906e-03
## Honda Civic           0.1767767   0.21715832  4.992493e-03  7.130519e-02
## Toyota Corolla        0.1767767   0.21715832 -1.045012e-02  4.854299e-03
## Toyota Corona         0.1767767   0.21715832  1.540473e-01 -1.047923e-01
## Dodge Challenger      0.1767767  -0.18511084 -8.880792e-02  2.389092e-01
## AMC Javelin           0.1767767  -0.18511084 -1.358072e-01  2.263942e-01
## Camaro Z28            0.1767767  -0.18511084  1.861898e-02 -1.880376e-01
## Pontiac Firebird      0.1767767  -0.18511084  1.864735e-01  1.923295e-01
## Fiat X1-9             0.1767767   0.21715832  1.607089e-02  7.121086e-03
## Porsche 914-2         0.1767767   0.21715832  1.547187e-01 -7.584174e-02
## Lotus Europa          0.1767767   0.21715832  7.012005e-02 -2.038653e-01
## Ford Pantera L        0.1767767  -0.18511084  2.197607e-02 -2.782542e-01
## Ferrari Dino          0.1767767   0.01602374 -2.159729e-01 -2.460949e-01
## Maserati Bora         0.1767767  -0.18511084 -1.458785e-01 -6.634167e-01
## Volvo 142E            0.1767767   0.21715832  1.570687e-01 -1.615312e-01
##                             drat            wt          qsec           vs
## Mazda RX4           -20.34522986 -18.199514334 -1.009678e+02 -2.474873734
## Mazda RX4 Wag        -2.08369142   4.262896616 -5.882432e+00 -2.275334960
## Datsun 710            0.54376131  -2.298719924 -2.296134e+00 -0.136279198
## Hornet 4 Drive        0.82206584  -0.317037533 -4.324799e+00 -0.270435586
## Hornet Sportabout    -1.88390110   0.616380940  2.522421e+00  0.406895941
## Valiant              -0.37171623   2.396228056  3.089107e+00  0.236291724
## Duster 360           -0.08793686   0.242105871 -4.950134e+00 -0.841231300
## Merc 240D            -0.09193016  -0.156492420 -5.882167e-02 -1.295019386
## Merc 230             -0.07434489  -0.173333156  6.988638e-01 -0.313628990
## Merc 280              0.08588521  -0.329915020  3.843920e-02  0.503331940
## Merc 280C             0.08588521  -0.329915020  1.596480e-01  0.426004609
## Merc 450SE           -0.13528319  -0.194006658  7.369100e-03 -0.046238983
## Merc 450SL           -0.13528319  -0.052116991  1.158834e-01 -0.119567690
## Merc 450SLC          -0.13528319  -0.072983119  1.866729e-01 -0.164126166
## Cadillac Fleetwood    0.08582482  -0.185328012 -5.580502e-02 -0.111184502
## Lincoln Continental   0.07225159  -0.290847599 -7.269231e-02 -0.061093725
## Chrysler Imperial     0.11451002  -0.343953463 -3.967092e-02  0.001431647
## Fiat 128             -0.02318037   0.009749353  1.212245e-01  0.076616858
## Honda Civic           0.46207265   0.011214818  1.474821e-01  0.221423374
## Toyota Corolla        0.03977957   0.107873288  3.030896e-01 -0.009673993
## Toyota Corona        -0.23543047   0.125758311  2.213640e-01 -0.049602398
## Dodge Challenger     -0.13607129   0.173819622 -1.587122e-01 -0.084462039
## AMC Javelin           0.04481216   0.077514554  1.602960e-02 -0.102454264
## Camaro Z28            0.16941914  -0.027039115  6.319892e-03  0.142138620
## Pontiac Firebird      0.11603386   0.167065328 -4.218267e-02 -0.100977417
## Fiat X1-9            -0.02262036   0.121005670  5.914552e-02  0.112835483
## Porsche 914-2         0.16943455   0.066251081 -2.733329e-01 -0.358482199
## Lotus Europa         -0.29026866   0.468372846 -1.429286e-01  0.225327670
## Ford Pantera L        0.37755782   0.150686316  1.149901e-01  0.198757109
## Ferrari Dino         -0.26286111   0.038374156 -2.530123e-01 -0.068492316
## Maserati Bora        -0.27787174   0.132176080  1.987287e-01  0.133647943
## Volvo 142E           -0.05011268  -0.095405901 -8.574182e-03  0.207441849
##                               am         gear         carb
## Mazda RX4           -2.298097039 -20.85965005 -15.90990258
## Mazda RX4 Wag       -1.451940264  -2.02391673   4.73923359
## Datsun 710           0.771165266   1.05774493   1.67466143
## Hornet 4 Drive       1.106636950   2.31853284   5.40625729
## Hornet Sportabout   -0.817576764  -0.98585837  -1.10916641
## Valiant             -0.694550593  -0.27191345   2.91869977
## Duster 360           0.834360111   0.97278576   1.98609700
## Merc 240D            0.299692488   0.13528414   0.36213558
## Merc 230             1.361790581   0.65748269   0.29305961
## Merc 280            -0.007366376  -1.95999774  -1.49862291
## Merc 280C           -0.070042669   0.17981601   3.19781483
## Merc 450SE          -0.010689498  -0.10814794   0.42855210
## Merc 450SL           0.006616335  -0.04158895   0.24945286
## Merc 450SLC         -0.040785203   0.03372733   0.25962216
## Cadillac Fleetwood  -0.126327613   0.21197339  -0.35158231
## Lincoln Continental -0.126681411   0.12051264  -0.12657909
## Chrysler Imperial   -0.075012790  -0.03719881   0.08421755
## Fiat 128            -0.237474396   0.05867150   0.27814023
## Honda Civic         -0.097907766  -0.14606322  -0.15649490
## Toyota Corolla      -0.246703517   0.14798929   0.14737805
## Toyota Corona        0.451448479  -0.48302966   0.01523328
## Dodge Challenger     0.102533702  -0.01135677   0.03264996
## AMC Javelin          0.056440332   0.01839298   0.13933456
## Camaro Z28           0.280035716  -0.54610130   0.09743337
## Pontiac Firebird     0.083604994   0.05867197  -0.09418390
## Fiat X1-9           -0.148072534  -0.03280582   0.16415590
## Porsche 914-2        0.525907698  -0.01710709   0.20104607
## Lotus Europa         0.151941269   0.08406615  -0.03694607
## Ford Pantera L      -0.278388308   0.43150754   0.37393255
## Ferrari Dino         0.105071636   0.09937047  -0.23377891
## Maserati Bora       -0.285847708   0.26904434  -0.20724412
## Volvo 142E          -0.169093778  -0.17183832   0.25595958
## attr(,"assign")
##  [1]  0  1  2  3  4  5  6  7  8  9 10
## 
## $qraux
##  [1] 1.176777 1.016024 1.113427 1.166614 1.078523 1.067422 1.053314
##  [8] 1.068987 1.073454 1.060903 1.075256
## 
## $pivot
##  [1]  1  2  3  4  5  6  7  8  9 10 11
## 
## $tol
## [1] 1e-07
## 
## $rank
## [1] 11
## 
## attr(,"class")
## [1] "qr"
fit_lm$df.residual
## [1] 21
fit_lm$xlevels
## named list()
fit_lm$call
## lm(formula = mpg ~ ., data = mtcars)
fit_lm$terms
## mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb
## attr(,"variables")
## list(mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)
## attr(,"factors")
##      cyl disp hp drat wt qsec vs am gear carb
## mpg    0    0  0    0  0    0  0  0    0    0
## cyl    1    0  0    0  0    0  0  0    0    0
## disp   0    1  0    0  0    0  0  0    0    0
## hp     0    0  1    0  0    0  0  0    0    0
## drat   0    0  0    1  0    0  0  0    0    0
## wt     0    0  0    0  1    0  0  0    0    0
## qsec   0    0  0    0  0    1  0  0    0    0
## vs     0    0  0    0  0    0  1  0    0    0
## am     0    0  0    0  0    0  0  1    0    0
## gear   0    0  0    0  0    0  0  0    1    0
## carb   0    0  0    0  0    0  0  0    0    1
## attr(,"term.labels")
##  [1] "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"
## attr(,"order")
##  [1] 1 1 1 1 1 1 1 1 1 1
## attr(,"intercept")
## [1] 1
## attr(,"response")
## [1] 1
## attr(,".Environment")
## <environment: R_GlobalEnv>
## attr(,"predvars")
## list(mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)
## attr(,"dataClasses")
##       mpg       cyl      disp        hp      drat        wt      qsec 
## "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" 
##        vs        am      gear      carb 
## "numeric" "numeric" "numeric" "numeric"
fit_lm$model
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

Finding the best fit model

step <- step(lm(mpg ~ ., mtcars), trace = 0); 
summary(step)
## 
## Call:
## lm(formula = mpg ~ wt + qsec + am, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4811 -1.5555 -0.7257  1.4110  4.6610 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   9.6178     6.9596   1.382 0.177915    
## wt           -3.9165     0.7112  -5.507 6.95e-06 ***
## qsec          1.2259     0.2887   4.247 0.000216 ***
## am            2.9358     1.4109   2.081 0.046716 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.459 on 28 degrees of freedom
## Multiple R-squared:  0.8497, Adjusted R-squared:  0.8336 
## F-statistic: 52.75 on 3 and 28 DF,  p-value: 1.21e-11

Also refer the document Effect of Transmission Type on Car’s Mileage which finds the best fit model using Backward Elimination (p-value) Method