What is linear regression

If our predicting variable is continuous in nature i.e., a numeric which could be any value o.1, o.22,2223.3 etc, then we use linear regression for predicting such outputs. In real world, the example could be interest rates. As the interest rates are something that numeric and continuous in nature i.e., they can take any floating value, therefore if we would like to predict them, then we use linear regression. Other example include rainfall in mm etc

Executing linear regression in R

library(datasets)
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
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
model <- lm(mpg~cyl+hp+wt+vs+gear+qsec, data = mtcars)
summary(model)
## 
## Call:
## lm(formula = mpg ~ cyl + hp + wt + vs + gear + qsec, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4441 -1.6780 -0.5283  1.0272  5.5570 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 26.50215   15.67122   1.691  0.10324   
## cyl         -0.50892    0.90915  -0.560  0.58061   
## hp          -0.01731    0.01764  -0.981  0.33582   
## wt          -3.45927    1.07974  -3.204  0.00368 **
## vs          -0.33781    2.02804  -0.167  0.86905   
## gear         0.71439    1.15831   0.617  0.54298   
## qsec         0.44371    0.65694   0.675  0.50561   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.625 on 25 degrees of freedom
## Multiple R-squared:  0.847,  Adjusted R-squared:  0.8102 
## F-statistic: 23.06 on 6 and 25 DF,  p-value: 4.652e-09
plot(model)

Here the model presents us with the summary output for the given mtcars data. Going through the p value, we can identify what the best values that can explain the mpg value better.