Simple linear regression is a statistical method used to model the relationship between two continuous variables. In this presentation, we will explore the concept of simple linear regression and see how it can be applied in R.
2023-02-06
Simple linear regression is a statistical method used to model the relationship between two continuous variables. In this presentation, we will explore the concept of simple linear regression and see how it can be applied in R.
The simple linear regression equation can be represented as:
\[ y = \beta_0 + \beta_1x \]
where \(y\) is the dependent variable, \(x\) is the independent variable, \(\beta_0\) is the intercept, and \(\beta_1\) is the slope of the regression line. This equation assumes a linear relationship between the dependent and independent variables.
To visualize the relationship between the two variables, we can create a scatterplot of the data.In this case it is the weight of the car in relation ot its average MPG
Using this code we can create a simple linear regression on the graph (which will be displayed on the next slide)
model <- lm(mpg ~ wt, data = mtcars)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_abline(intercept = model\(coef[1], slope = model\)coef[2]) +
labs(x = “Weight (wt)”, y = “Miles per Gallon (mpg)”) +
ggtitle(“Scatterplot with Regression Line”)
This is the same plot but now a regression line has been added to depict the general trend in the data
The regression line can also be represented mathematically using the equation of a line:
\[y = \beta_0 + \beta_1x\]
where \(\beta_0\) is the intercept and \(\beta_1\) is the slope of the regression line.
## ## 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
This is the same data including the regression line but made in plotly so each data point can be interacted with.