## Secrets of successful data analysis - Sykalo Eugene 2023

# Regression Analysis - Introduction to regression analysis and its applications

Data Analysis Tools and Techniques

## Introduction to Regression Analysis

Regression analysis is a statistical method used to explore the relationship between a dependent variable and one or more independent variables. It is a powerful tool for predicting the behavior of a dependent variable based on changes in one or more independent variables.

### Definition of Regression Analysis

In regression analysis, the dependent variable is known as the response variable, while the independent variables are known as the predictor variables. The aim of regression analysis is to estimate the relationship between the response variable and the predictor variables, and to use this relationship to predict the value of the response variable for new observations.

### Applications of Regression Analysis

Regression analysis has a wide range of applications in various fields, including finance, economics, social sciences, engineering, and more. In finance, regression analysis is used to model the relationship between stock prices and economic variables such as interest rates, inflation rates, and gross domestic product (GDP). In economics, regression analysis is used to model the relationship between demand and supply, price and quantity, and more. In social sciences, regression analysis is used to model the relationship between variables such as income, education, and health. In engineering, regression analysis is used to model the relationship between variables such as temperature, pressure, and volume.

### Types of Regression Analysis

There are several types of regression analysis, each of which is suitable for different types of data and research questions. The most commonly used types of regression analysis are:

- Simple Linear Regression: This is used when there is a linear relationship between the dependent variable and one independent variable.
- Multiple Linear Regression: This is used when there is a linear relationship between the dependent variable and two or more independent variables.
- Logistic Regression: This is used when the dependent variable is categorical, and the independent variables can be either categorical or continuous.
- Polynomial Regression: This is used when the relationship between the dependent variable and independent variables is not linear, but can be approximated by a polynomial function.
- Ridge Regression: This is used when there is multicollinearity among the independent variables, and aims to reduce the variance of the estimates.
- Lasso Regression: This is used when there is multicollinearity among the independent variables, and aims to select the most important variables.
- Elastic Net Regression: This is a combination of Ridge Regression and Lasso Regression, and aims to reduce the variance of the estimates and select the most important variables.

## Simple Linear Regression

Simple linear regression is a statistical method used to model the relationship between a dependent variable and a single independent variable. The dependent variable is also known as the response variable, while the independent variable is known as the predictor variable.

### Definition and Assumptions of Simple Linear Regression

In simple linear regression, it is assumed that there is a linear relationship between the response variable and the predictor variable. This means that as the value of the predictor variable changes, the value of the response variable changes in a linear fashion. The relationship between the two variables can be expressed using a straight line equation of the form:

y = β0 + β1x + ε

where y is the response variable, x is the predictor variable, β0 is the intercept, β1 is the slope of the line, and ε is the error term.

It is also assumed that the error term ε follows a normal distribution with a mean of 0 and a constant variance. This means that the errors are normally distributed around the regression line, and that the variance of the errors is the same for all values of the predictor variable.

### Estimating the Regression Parameters using the Least Squares Method

The least squares method is used to estimate the regression parameters β0 and β1 in simple linear regression. The aim is to find the values of β0 and β1 that minimize the sum of squared residuals, which is the difference between the observed values of the response variable and the predicted values based on the regression line.

The formula for the slope of the regression line is:

β1 = Σ[(xi - x̄)(yi - ȳ)] / Σ[(xi - x̄)2]

where xi is the ith value of the predictor variable, x̄ is the mean of the predictor variable, yi is the ith value of the response variable, and ȳ is the mean of the response variable.

The formula for the intercept of the regression line is:

β0 = ȳ - β1x̄

Once the values of β0 and β1 are estimated, the regression line can be expressed as:

ŷ = β0 + β1x

where ŷ is the predicted value of the response variable for a given value of the predictor variable x.

### Evaluating the Model Fit using R-squared and Residual Plot

R-squared is a measure of how well the regression line fits the data. It represents the proportion of the variance in the response variable that is explained by the predictor variable. The formula for R-squared is:

R2 = SSreg / SStot

where SSreg is the sum of squares due to regression and SStot is the total sum of squares. The value of R-squared ranges from 0 to 1, with a higher value indicating a better fit of the regression line to the data.

A residual plot is a graphical representation of the residuals of the regression line. The residuals are the differences between the observed values of the response variable and the predicted values based on the regression line. A residual plot can help to identify patterns in the residuals that may indicate that the assumptions of the regression model are not met.

### Interpreting the Regression Output

The regression output provides information about the estimated values of β0 and β1, the R-squared value, and the standard error of the estimate. The standard error of the estimate is a measure of the variability of the errors around the regression line.

The coefficients β0 and β1 can be used to make predictions of the response variable for a given value of the predictor variable. The standard error of the estimate can be used to calculate confidence intervals for the predicted values.

In addition, the t-test and p-value for each coefficient can be used to test the hypothesis that the coefficient is equal to 0. If the p-value is less than the chosen significance level (usually 0.05), then there is evidence to reject the null hypothesis and conclude that the coefficient is significantly different from 0.

## Multiple Linear Regression

Multiple linear regression is a statistical method used to model the relationship between a dependent variable and two or more independent variables. The dependent variable is also known as the response variable, while the independent variables are known as the predictor variables.

### Definition and Assumptions of Multiple Linear Regression

In multiple linear regression, it is assumed that there is a linear relationship between the response variable and the predictor variables. This means that as the values of the predictor variables change, the value of the response variable changes in a linear fashion. The relationship between the variables can be expressed using an equation of the form:

y = β0 + β1x1 + β2x2 + ... + βkxk + ε

where y is the response variable, x1, x2, ..., xk are the predictor variables, β0 is the intercept, β1, β2, ..., βk are the slopes of the lines, and ε is the error term.

It is also assumed that the error term ε follows a normal distribution with a mean of 0 and a constant variance. This means that the errors are normally distributed around the regression plane, and that the variance of the errors is the same for all values of the predictor variables.

### Estimating the Regression Parameters using the Least Squares Method

The least squares method is used to estimate the regression parameters β0, β1, β2, ..., βk in multiple linear regression. The aim is to find the values of the parameters that minimize the sum of squared residuals, which is the difference between the observed values of the response variable and the predicted values based on the regression plane.

The formula for the intercept of the regression plane is:

β0 = ȳ - β1x̄1 - β2x̄2 - ... - βkx̄k

where ȳ is the mean of the response variable, and x̄1, x̄2, ..., x̄k are the means of the predictor variables.

The formula for the slopes of the regression plane are:

βj = Σ[(xij - x̄j)(yi - ȳ)] / Σ[(xij - x̄j)2]

where xij is the ith value of the jth predictor variable, x̄j is the mean of the jth predictor variable, yi is the ith value of the response variable, and ȳ is the mean of the response variable.

Once the values of the parameters are estimated, the regression plane can be expressed as:

ŷ = β0 + β1x1 + β2x2 + ... + βkxk

where ŷ is the predicted value of the response variable for a given set of values of the predictor variables.

### Evaluating the Model Fit using R-squared and Residual Plot

R-squared is a measure of how well the regression plane fits the data. It represents the proportion of the variance in the response variable that is explained by the predictor variables. The formula for R-squared is:

R2 = SSreg / SStot

where SSreg is the sum of squares due to regression and SStot is the total sum of squares. The value of R-squared ranges from 0 to 1, with a higher value indicating a better fit of the regression plane to the data.

A residual plot is a graphical representation of the residuals of the regression plane. The residuals are the differences between the observed values of the response variable and the predicted values based on the regression plane. A residual plot can help to identify patterns in the residuals that may indicate that the assumptions of the regression model are not met.

### Interpreting the Regression Output

The regression output provides information about the estimated values of the parameters, the R-squared value, and the standard error of the estimate. The standard error of the estimate is a measure of the variability of the errors around the regression plane.

The coefficients can be used to make predictions of the response variable for a given set of values of the predictor variables. The standard error of the estimate can be used to calculate confidence intervals for the predicted values.

In addition, the t-test and p-value for each coefficient can be used to test the hypothesis that the coefficient is equal to 0. If the p-value is less than the chosen significance level (usually 0.05), then there is evidence to reject the null hypothesis and conclude that the coefficient is significantly different from 0.

### Dealing with Multicollinearity

Multicollinearity occurs when there is a high correlation between two or more predictor variables. This can cause problems in multiple linear regression, as it can make it difficult to estimate the regression parameters accurately.

One way to deal with multicollinearity is to remove one or more of the highly correlated predictor variables from the model. Another way is to use techniques such as ridge regression or principal component regression, which can help to reduce the effects of multicollinearity on the estimates of the regression parameters.

## Logistic Regression

Logistic regression is a statistical method used to model the relationship between a binary dependent variable and one or more independent variables. The dependent variable is binary, meaning it can take only two values, usually coded as 0 and 1. The independent variables can be either categorical or continuous.

### Definition and Assumptions of Logistic Regression

In logistic regression, it is assumed that there is a logistic relationship between the response variable and the predictor variables. This means that the probability of the dependent variable taking the value of 1 can be modeled using a logistic function of the form:

P(Y = 1) = e^(β0 + β1x1 + β2x2 + ... + βkxk) / (1 + e^(β0 + β1x1 + β2x2 + ... + βkxk))

where Y is the binary response variable, x1, x2, ..., xk are the predictor variables, β0 is the intercept, and β1, β2, ..., βk are the coefficients of the independent variables.

It is also assumed that the errors in the logistic regression model are independent and follow a binomial distribution with a fixed probability of success.

### Estimating the Regression Parameters using Maximum Likelihood Estimation

Maximum likelihood estimation is used to estimate the regression parameters β0, β1, β2, ..., βk in logistic regression. The aim is to find the values of the parameters that maximize the likelihood of the observed data given the model.

The likelihood function is:

L(β0, β1, β2, ..., βk) = ∏[P(Yi = yi)]^yi * [1 - P(Yi = yi)]^(1 - yi)

where yi is the observed value of the response variable for the ith observation, and Yi is the predicted probability of the response variable taking the value of 1 for the ith observation.

The log-likelihood function is:

l(β0, β1, β2, ..., βk) = Σ[yi log(Yi) + (1 - yi) log(1 - Yi)]

The aim is to find the values of the parameters that maximize the log-likelihood function using an iterative optimization algorithm.

### Evaluating the Model Fit using Deviance and Goodness of Fit Tests

Deviance is a measure of the difference between the predicted probabilities and the observed values of the response variable. It is defined as:

D = -2 * [Σ(yi log(Yi) + (1 - yi) log(1 - Yi)) - Σ(yi log(yi) + (1 - yi) log(1 - yi))]

where yi is the observed value of the response variable for the ith observation, and Yi is the predicted probability of the response variable taking the value of 1 for the ith observation.

Smaller values of deviance indicate a better fit of the model to the data.

Goodness of fit tests, such as the Hosmer-Lemeshow test, can be used to assess whether the observed values of the response variable are significantly different from the predicted probabilities.

### Interpreting the Regression Output

The logistic regression output provides information about the estimated values of the coefficients, the standard errors of the coefficients, and the significance of the coefficients.

The coefficients can be used to calculate the odds ratios of the independent variables. An odds ratio of greater than 1 indicates that an increase in the independent variable is associated with an increase in the odds of the dependent variable taking the value of 1.

In addition, the Wald test and p-value for each coefficient can be used to test the hypothesis that the coefficient is equal to 0. If the p-value is less than the chosen significance level (usually 0.05), then there is evidence to reject the null hypothesis and conclude that the coefficient is significantly different from 0.

## Model Selection and Validation

Model selection is the process of choosing the best model from a set of candidate models. In regression analysis, this involves selecting the independent variables that are most strongly associated with the dependent variable, while avoiding overfitting the model to the data. Overfitting occurs when the model is too complex, and fits the noise in the data rather than the underlying relationship between the variables.

### Techniques for Selecting the Best Model

There are several techniques for selecting the best model in regression analysis, including:

- Backward Elimination: This involves starting with a model that includes all the independent variables, and then removing the variables that do not contribute significantly to the model fit.
- Forward Selection: This involves starting with a model that includes only one independent variable, and then adding variables one at a time until the model fit is maximized.
- Stepwise Regression: This is a combination of backward elimination and forward selection, and involves adding and removing variables in a stepwise fashion based on their contribution to the model fit.
- Information Criteria: This involves comparing the fit of different models using information criteria such as Akaike's Information Criterion (AIC) or the Bayesian Information Criterion (BIC). These criteria penalize models that are too complex, and can help to choose the best model that fits the data without overfitting.

### Cross-Validation Techniques

Cross-validation is a technique used to estimate the performance of a model on new data. The idea is to divide the data into two sets: a training set and a validation set. The model is trained on the training set, and then tested on the validation set. This process is repeated several times, with different subsets of the data used for training and validation. The average performance of the model across all the subsets is then used as an estimate of the performance of the model on new data.

There are several types of cross-validation techniques, including:

- k-Fold Cross-Validation: This involves dividing the data into k subsets of approximately equal size. The model is trained on k-1 subsets, and then tested on the remaining subset. This process is repeated k times, with each subset used as the validation set once.
- Leave-One-Out Cross-Validation: This involves leaving one observation out of the data each time, and then training the model on the remaining observations. The model is then tested on the left-out observation. This process is repeated for each observation in the data set.
- Random Subsampling: This involves randomly dividing the data into training and validation sets, and then repeating the process several times with different random samples. The average performance of the model across all the samples is then used as an estimate of the performance of the model on new data.

### Overfitting and Regularization

Overfitting occurs when the model is too complex, and fits the noise in the data rather than the underlying relationship between the variables. Regularization is a technique used to reduce the complexity of the model, and to avoid overfitting.

There are several types of regularization techniques, including:

- Ridge Regression: This involves adding a penalty term to the sum of squared residuals, which helps to reduce the variance of the estimates and avoid overfitting.
- Lasso Regression: This involves adding a penalty term to the sum of absolute values of the coefficients, which helps to select the most important variables and avoid overfitting.
- Elastic Net Regression: This is a combination of Ridge Regression and Lasso Regression, and aims to reduce the variance of the estimates and select the most important variables.

Regularization can be used to improve the performance of the model on new data, by reducing the complexity of the model and avoiding overfitting. However, it is important to choose the right amount of regularization, as too much regularization can lead to underfitting the model to the data.