2024-06-03

What Is Linear Regression ?

  • Simple Linear Regression is a method in which we are able to see the direct relationship between an independent and dependent variable.
  • The relationship between x and y is modeled by fitting a linear equation to the observed data.

Mathematical Explanation

  • The equation of a linear model is: \[ y = \beta_0 + \beta_1 x + \epsilon \]
  • \(y\) being the dependent variable
  • \(x\) being the independent variable
  • \(\beta_0\) being the constant or intercept
  • \(\beta_1\) being X’s slope value
  • \(\epsilon\) being the error term

Data Set Example

##             x         y
## 1 -0.56047565 0.5718916
## 2 -0.23017749 1.2809208
## 3  1.55870831 6.6332545
## 4  0.07050839 3.5801275
## 5  0.12928774 2.1620922
## 6  1.71506499 8.6616656

Plotting the Data

Fitting the Linear Model

## 
## Call:
## lm(formula = y ~ x, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.46443 -0.48623 -0.02705  0.49405  2.02559 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.1476     0.1294   16.60   <2e-16 ***
## x             2.9649     0.1411   21.02   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9142 on 48 degrees of freedom
## Multiple R-squared:  0.902,  Adjusted R-squared:    0.9 
## F-statistic: 441.8 on 1 and 48 DF,  p-value: < 2.2e-16

Visualization of the Linear Model (ggplots)

## `geom_smooth()` using formula = 'y ~ x'

Plotly Plot

## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

R Code Example

## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.46443 -0.48623 -0.02705  0.49405  2.02559 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.1476     0.1294   16.60   <2e-16 ***
## x             2.9649     0.1411   21.02   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9142 on 48 degrees of freedom
## Multiple R-squared:  0.902,  Adjusted R-squared:    0.9 
## F-statistic: 441.8 on 1 and 48 DF,  p-value: < 2.2e-16

Conclusion

  • In conclusion Linear Regression Models are very fun and easy to make.
  • Linear Regression Models play a key role in the word of statistics.
  • Being able to see the relationship between two variables helps solve and visualize many world problems.