当前位置:网站首页>Based on the matrix calculation in the linear regression equation of the coefficient estimates
Based on the matrix calculation in the linear regression equation of the coefficient estimates
2022-08-02 15:33:00 【Yang Lao head soft work】
I. Description of the problem:
Regression analysis is to use known data to determine the coefficients in the regression equation.
Univariate linear regression analysis is a very simple and very basic regression theory, which can be used to describe the change trend of the linear relationship between two variables, and then predict the data at the unknown point.
Univariate linear regression analysis is to use known data to estimate the coefficients k and b in the equation y=kx+b. The common methods are: calculation mathematics method - least square method, statistical method - maximum likelihood estimation method, machine learning methods - perceptrons, etc., and can also be directly solved based on matrix operations, because the linear equation system with invertible coefficient matrix can be constructed using known data.
This paper takes the data of y = 2x + 1 for fitting as an example, and gives the method of estimating coefficients based on matrix operations and the matlab implementation.
II. Mathematical derivation
Problem description
Assuming known data points (xi,yi), i=1…n, where xi is not all 1, a linear trend can be preliminarily analyzed according to the scatter plot.
Therefore, y=kx+b can be used to fit this set of data. At this time, y=kx+b can be called either the fitting function of the set of data (computational mathematics) or the regression equation (statisticalstudy).
The formula is derived as follows:
Substitute (xi,yi) into the linear function y=kx+b to get:
Write the matrix form as follows:
Remember
Then equation (2) can be written as 
and the two column vectors of matrix X are linearly independent, then X'X is an invertible matrix, which gives: 
SoThere are: 
This will get the linear regression equationcoefficient.
Third, Matlab program
1. Draw a scatter plot
trainX = linspace( 0, 2, 50 );trainY = 2 * trainX + 1 + randn( size( trainX ) )*0.3;plot( trainX, trainY, 'b.', 'markersize', 20 )The result is shown below:
From the imageIt can be seen that the known data points basically show a linear trend, so a univariate linear regression model can be used to fit this set of data.
2. Data Fitting
I = ones( size(trainX) );X = [ trainX', I' ];Y = trainY';B = inv( X' * X ) * X' * Y % regression coefficient, B(1) = k, B(2) = bThe regression coefficients are: k = B(1) = 2.0660, b = B(2) = 0.9925
3. Complete code
clear allclctrainX = linspace( 0, 2, 50 );trainY = 2 * trainX + 1 + randn( size( trainX ) )*0.3;% draw a scatter plotplot( trainX, trainY, 'b.', 'markersize', 20 )I = ones( size(trainX) );X = [ trainX', I' ];Y = trainY';B = inv( X' * X ) * X' * Y % regression coefficient, B(1) = k, B(2) = b% draw the regression function curve (straight line)x = [ -1 : 3 ];y = B(1) * x + B(2);hold onplot( x, y, 'r', 'LineWidth', 2 );title( 'y = 2x + 1' )The result is as follows:
Modify the sentence "trainY= 2 * trainX + 1 + randn( size( trainX ) )*0.3;" is "trainY = -2 * trainX + 5 + randn( size( trainX ) )*0.3;", the following fitting image can be obtained:
Fourth, Supplementary Instructions
This method is only suitable for regression coefficient estimation in univariate or multiple linear regression analysis. For nonlinear regression, it can be considered to transform the linear regression analysis by logarithmic transformation and other methods before solving.
边栏推荐
- 2021-10-14
- 推开机电的大门《电路》(二):功率计算与判断
- [STM32 Learning 1] Basic knowledge and concepts are clear
- 使用npx -p @storybook/cli sb init安装失败,手把手搭建专属的storybook
- Win11没有本地用户和组怎么解决
- 基于矩阵计算的线性回归分析方程中系数的估计
- 2021-10-14
- Detailed introduction to drawing complex surfaces using the plot_surface command
- 二叉树遍历之后序遍历(非递归、递归)入门详解
- Win11系统找不到dll文件怎么修复
猜你喜欢

Win11 system cannot find dll file how to fix

总结计算机网络超全面试题

What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer

Mapreduce环境详细搭建和案例实现

MATLAB绘图函数ezplot入门详解

Redis常见面试题

奇技淫巧-位运算

Win11系统找不到dll文件怎么修复

Redis的线程模型

General syntax and usage instructions of SQL (picture and text)
随机推荐
1.开发社区首页,注册
Lightweight AlphaPose
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
Detailed explanation of Golang garbage collection mechanism
Mysql connection error solution
基于矩阵计算的线性回归分析方程中系数的估计
发布模块到npm应该怎么操作?及错误问题解决方案
How to set the win10 taskbar does not merge icons
STM32LL library - USART interrupt to receive variable length information
Cmd Markdown 公式指导手册
IPV4和IPV6是什么?
【系统设计与实现】基于flink的分心驾驶预测与数据分析系统
2021-10-14
BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
[STM32 Learning 1] Basic knowledge and concepts are clear
总结计算机网络超全面试题
2021-10-14
Win7遇到错误无法正常开机进桌面怎么解决?
Letter combination of LeetCode2 phone number