Mathematical Modeling: Logistic Regression in Excel

00Comment iconComment iconComment iconComment icon

Here you will dive into the world of mathematical modeling using Logistic regression in Excel. Learn how to analyze data and make predictions with this powerful tool.

Writer image

تمت الترجمة بواسطةEditorial

Writer image

تمت المراجعة بواسطةEditorial

Edit Article

What Is Mathematical Modeling?

Mathematical modeling is the process of using formulas and equations to represent real-world situations. It helps us predict outcomes, understand relationships between variables, and make informed decisions.

Logistic Regression

Logistic Regression is a statistical technique used to predict binary outcomes (yes/no, 0/1) based on one or more independent variables. Unlike linear regression, which predicts continuous values, logistic regression predicts the probabilities of an event occurring.

The central idea is to model the probability of an event happening using the logistic function (also called the sigmoid). The logistic function maps any real value to a value between 0 and 1, which can be interpreted as a probability.

The logistic regression equation is:

\[ P(Y=1) = \frac{1}{1 + e^{-(aX + b)}} \]

Where:

- \(P(Y=1)\): Probability of the event happening.

- \(X\): Independent variable (the influencing factor).

- \(a\): Coefficient that indicates the impact of \(X\) on the probability.

- \(b\): Intercept.

- \(e\): Euler's number (approximately 2.71828).

When to Use It?

- When you want to predict a binary outcome (e.g., whether a customer will buy or not, whether a patient has a disease or not).

- When the dependent variable is categorical with two classes (0 or 1).

Logistic Regression in Excel

In Excel, there is no native function for logistic regression, but we can use Solver to fit the model. Solver is an optimization tool that adjusts the coefficients \(a\) and \(b\) to maximize the likelihood of the data.

Log-likelihood is the best way to evaluate the model fit because it measures the probability of observing the real data under the model parameters. Unlike mean squared error, which may not be suitable for binary variables, log-likelihood reflects the model's adequacy by considering appropriate statistical distributions, such as the Bernoulli distribution in logistic regression.

Step-by-Step in Excel

1. Organize the Data

- Put the data into columns. Example:

Age (X)Bought (Y)
150
201
250
301
351
401

2. Define the Logistic Function

- In a column, calculate the predicted probability using the logistic function:

\[ P(Y=1) = \frac{1}{1 + e^{-(aX + b)}} \]

- Use initial values for \(a\) and \(b\) (e.g., \(a = 0\), \(b = 0\)).

Calculating the logistic probability
Calculating the logistic probability

3. Calculate the Log-Likelihood

- Log-likelihood is a measure of how well the model fits the data. It is calculated as:

\[ \text{Log-Likelihood} = \sum_{i=1}^n \left[ Y_i \cdot \ln(P(Y_i=1)) + (1 - Y_i) \cdot \ln(1 - P(Y_i=1)) \right] \]

- The goal is to maximize this function.

Calculating the Log-Likelihood
Calculating the Log-Likelihood

It is recommended to add a very small value (0.0000001) inside the LN function to avoid an error in the formula, since LN(0) is undefined.

4. Use Solver

- Go to the Data tab > Solver.

- Set the log-likelihood cell as the Objective.

- Set the cells for \(a\) and \(b\) as Decision Variables.

- Configure Solver to Maximize the log-likelihood.

- Run Solver to adjust the coefficients \(a\) and \(b\).

Solver follows a process similar to Monte Carlo, that is, it "tests" values and moves until it finds the most appropriate maximum (or minimum) value. For nonlinear processes, it is recommended to use the GRG Nonlinear optimizer (Generalized Reduced Gradient); in this case, instead of being 100% random, it follows a descending gradient to find the best values for A and B.

Due to this characteristic of the optimizer, remember to always start at points 0 and 0 for A and B respectively. Also remember that if the result is a very large B, it probably indicates that the Logistic Regression is too perfect with several possible answers.

5. Interpret the Results

- The coefficients \(a\) and \(b\) adjusted by Solver are used to predict probabilities.

- The coefficient \(a\) indicates the impact of variable \(X\) on the probability of the event.

- The coefficient \(b\) is the intercept, which represents the probability when \(X = 0\).

Practical Example

Data

Age (X)Bought (Y)
150
201
250
301
351
401

Solver Results

Solver maximizing the sum of the Log-Likelihood
Solver maximizing the sum of the Log-Likelihood

- Coefficient \(a\): 0.228927774

- Coefficient \(b\): 5.059228074

Logistic Regression Equation:

\[ P(Y=1) = \frac{1}{1 + e^{-(0.2 \cdot X - 5)}} \]

Prediction

- For a 30-year-old person:

\[ P(Y=1) = \frac{1}{1 + e^{-(0.2 \cdot 30 - 5)}} \approx 0.85 \]

- There is an 85% chance of purchase. Therefore, since it is above 50%, they are expected to buy.

Exercises

Exercise 1: Product Purchase

Age (X)Bought (Y)
250
351
400
451
551

a) What is the logistic regression equation?

b) What is the probability that a 40-year-old person will buy the product?

c) What does the coefficient \(a\) represent in this context?

Exercise 2: Medical Diagnosis

Age (X)Disease (Y)
300
350
401
410
501
601

a) What is the logistic regression equation?

b) What is the probability that a 55-year-old patient has the disease?

c) What does the coefficient \(b\) represent in this context?

Exercise 3: Default

Income (X)Defaulting (Y)
20000
29001
30000
35001
40001
50001

a) What is the logistic regression equation?

b) What is the probability that a customer with an income of R$ 3500 will default?

c) What does the coefficient \(a\) represent in this context?