# R - Linear Regression

Simple linear regression is a statistical method that is used to model the **relationship between two variables.** The two variables are called the independent variable and the dependent variable. The independent variable is the variable that is believed to cause the change in the dependent variable. The dependent variable is the variable that is being predicted.

In simple linear regression, the equation for the line is:

where y is the dependent variable, **x is the independent variable,** m is the slope of the line, and b is the y-intercept.

The slope of the line, m, tells us how much the dependent variable changes for every unit change in the independent variable. The y-intercept, b, tells us the value of the dependent variable when the independent variable is 0.

## Simple Linear Regression in R

Simple linear regression can be performed in R using the **lm() function.** The lm() function takes two arguments:

**formula:**A formula that specifies the relationship between the two variables. The formula is in the following format:

where y is the dependent variable and x is the independent variable.

**data:**A data frame that contains the data for the two variables.

For example, the following code performs a simple linear regression to model the relationship between the height and weight of a group of people:

The output of the lm() function is an object that contains the results of the simple linear regression. You can use the **summary() function** to view the results of the simple linear regression:

The summary of the linear regression shows that the **coefficient of the independent variable,** height, is 1.50. This means that for every 1 unit increase in height, there is a 1.50 unit increase in weight. The p-value for the coefficient of the independent variable is less than 0.05, which means that the coefficient is statistically significant. The **R-squared value is 0.816,** which means that 81.6% of the variation in weight is explained by the linear regression model.

Here are some other things to keep in mind about simple linear regression in R:

- The lm() function can also be used to perform multiple linear regression, which is a regression with multiple independent variables.
- The predict() function can be used to predict the value of the dependent variable for a given value of the independent variable.
- The plot() function can be used to plot the data and the fitted linear regression line.

### Conclusion

Simple linear regression in R is a powerful tool for modeling **relationships between two continuous variables** and making predictions based on that relationship. It provides insights into how changes in the independent variable influence the dependent variable.

**Related Topics**