Regression analysis is a powerful statistical method that allows you to examine the relationship between two or more variables of interest. In MATLAB, regression analysis can be performed using various built-in functions and toolboxes. This section will cover the basics of regression analysis, including linear regression, multiple regression, and polynomial regression.
Key Concepts
-
Dependent and Independent Variables:
- Dependent Variable (Y): The variable you are trying to predict or explain.
- Independent Variable (X): The variable you are using to predict the dependent variable.
-
Linear Regression:
- A method to model the relationship between a dependent variable and one or more independent variables using a linear equation.
-
Multiple Regression:
- An extension of linear regression that uses multiple independent variables to predict the dependent variable.
-
Polynomial Regression:
- A form of regression analysis in which the relationship between the independent variable and the dependent variable is modeled as an nth degree polynomial.
Linear Regression
Example: Simple Linear Regression
Let's start with a simple linear regression example where we have one independent variable.
% Sample data X = [1, 2, 3, 4, 5]'; Y = [2, 4, 5, 4, 5]'; % Perform linear regression p = polyfit(X, Y, 1); % Display the coefficients disp('Coefficients:'); disp(p); % Predict Y values Y_pred = polyval(p, X); % Plot the data and the regression line figure; scatter(X, Y, 'b', 'filled'); hold on; plot(X, Y_pred, 'r-'); xlabel('X'); ylabel('Y'); title('Simple Linear Regression'); legend('Data', 'Linear Fit'); hold off;
Explanation
polyfit(X, Y, 1)
: Fits a linear polynomial (degree 1) to the data.polyval(p, X)
: Evaluates the polynomial at the given points.- The plot shows the original data points and the fitted regression line.
Exercise: Simple Linear Regression
Task: Given the following data, perform a simple linear regression and plot the results.
Solution:
% Sample data X = [10, 20, 30, 40, 50]'; Y = [15, 25, 35, 45, 55]'; % Perform linear regression p = polyfit(X, Y, 1); % Predict Y values Y_pred = polyval(p, X); % Plot the data and the regression line figure; scatter(X, Y, 'b', 'filled'); hold on; plot(X, Y_pred, 'r-'); xlabel('X'); ylabel('Y'); title('Simple Linear Regression'); legend('Data', 'Linear Fit'); hold off;
Multiple Regression
Example: Multiple Linear Regression
When you have more than one independent variable, you can use multiple linear regression.
% Sample data X = [1, 2; 2, 3; 3, 4; 4, 5; 5, 6]; Y = [2, 3, 4, 5, 6]'; % Perform multiple linear regression b = regress(Y, [ones(size(X,1),1) X]); % Display the coefficients disp('Coefficients:'); disp(b); % Predict Y values Y_pred = [ones(size(X,1),1) X] * b; % Plot the data and the regression line figure; scatter3(X(:,1), X(:,2), Y, 'b', 'filled'); hold on; [X1, X2] = meshgrid(min(X(:,1)):0.1:max(X(:,1)), min(X(:,2)):0.1:max(X(:,2))); Y_pred_grid = b(1) + b(2)*X1 + b(3)*X2; mesh(X1, X2, Y_pred_grid); xlabel('X1'); ylabel('X2'); zlabel('Y'); title('Multiple Linear Regression'); legend('Data', 'Regression Plane'); hold off;
Explanation
regress(Y, [ones(size(X,1),1) X])
: Performs multiple linear regression. Theones(size(X,1),1)
adds a column of ones to account for the intercept term.- The plot shows the original data points and the fitted regression plane.
Exercise: Multiple Linear Regression
Task: Given the following data, perform a multiple linear regression and plot the results.
Solution:
% Sample data X = [1, 2; 2, 3; 3, 4; 4, 5; 5, 6]; Y = [3, 5, 7, 9, 11]'; % Perform multiple linear regression b = regress(Y, [ones(size(X,1),1) X]); % Predict Y values Y_pred = [ones(size(X,1),1) X] * b; % Plot the data and the regression line figure; scatter3(X(:,1), X(:,2), Y, 'b', 'filled'); hold on; [X1, X2] = meshgrid(min(X(:,1)):0.1:max(X(:,1)), min(X(:,2)):0.1:max(X(:,2))); Y_pred_grid = b(1) + b(2)*X1 + b(3)*X2; mesh(X1, X2, Y_pred_grid); xlabel('X1'); ylabel('X2'); zlabel('Y'); title('Multiple Linear Regression'); legend('Data', 'Regression Plane'); hold off;
Polynomial Regression
Example: Polynomial Regression
Polynomial regression can model non-linear relationships.
% Sample data X = [1, 2, 3, 4, 5]'; Y = [1, 4, 9, 16, 25]'; % Perform polynomial regression (degree 2) p = polyfit(X, Y, 2); % Display the coefficients disp('Coefficients:'); disp(p); % Predict Y values Y_pred = polyval(p, X); % Plot the data and the regression curve figure; scatter(X, Y, 'b', 'filled'); hold on; plot(X, Y_pred, 'r-'); xlabel('X'); ylabel('Y'); title('Polynomial Regression'); legend('Data', 'Polynomial Fit'); hold off;
Explanation
polyfit(X, Y, 2)
: Fits a polynomial of degree 2 to the data.polyval(p, X)
: Evaluates the polynomial at the given points.- The plot shows the original data points and the fitted polynomial curve.
Exercise: Polynomial Regression
Task: Given the following data, perform a polynomial regression of degree 3 and plot the results.
Solution:
% Sample data X = [1, 2, 3, 4, 5]'; Y = [1, 8, 27, 64, 125]'; % Perform polynomial regression (degree 3) p = polyfit(X, Y, 3); % Predict Y values Y_pred = polyval(p, X); % Plot the data and the regression curve figure; scatter(X, Y, 'b', 'filled'); hold on; plot(X, Y_pred, 'r-'); xlabel('X'); ylabel('Y'); title('Polynomial Regression'); legend('Data', 'Polynomial Fit'); hold off;
Conclusion
In this section, we covered the basics of regression analysis in MATLAB, including simple linear regression, multiple linear regression, and polynomial regression. We provided practical examples and exercises to help you understand how to perform these analyses and visualize the results. Understanding these concepts is crucial for data analysis and predictive modeling in MATLAB.
MATLAB Programming Course
Module 1: Introduction to MATLAB
- Getting Started with MATLAB
- MATLAB Interface and Environment
- Basic Commands and Syntax
- Variables and Data Types
- Basic Operations and Functions
Module 2: Vectors and Matrices
- Creating Vectors and Matrices
- Matrix Operations
- Indexing and Slicing
- Matrix Functions
- Linear Algebra in MATLAB
Module 3: Programming Constructs
- Control Flow: if, else, switch
- Loops: for, while
- Functions: Definition and Scope
- Scripts vs. Functions
- Debugging and Error Handling
Module 4: Data Visualization
Module 5: Data Analysis and Statistics
- Importing and Exporting Data
- Descriptive Statistics
- Data Preprocessing
- Regression Analysis
- Statistical Tests