当前位置:网站首页>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) = b
The 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.
边栏推荐
- Win11声卡驱动如何更新?Win11声卡驱动更新方法
- DP1332E刷卡芯片支持NFC内置mcu智能楼宇/终端poss机/智能门锁
- Use tencent cloud builds a personal blog
- Win10 cannot directly use photo viewer to open the picture
- CI24R1小模块2.4G收发模块无线通信低成本兼容si24r1/XN297超低功耗
- 如何用硬币模拟1/3的概率,以及任意概率?
- 倍增和稀疏表
- Mysql connection error solution
- How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?
- Introduction to MATLAB drawing functions ezplot explanation
猜你喜欢
win10系统更新错误代码0x80244022怎么办
为vscode配置clangd
Win11电脑一段时间不操作就断网怎么解决
6.统一记录日志
How to add a one-key shutdown option to the right-click menu in Windows 11
win10怎么设置不睡眠熄屏?win10设置永不睡眠的方法
推开机电的大门《电路》(二):功率计算与判断
Use tencent cloud builds a personal blog
cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
[STM32 Learning 1] Basic knowledge and concepts are clear
随机推荐
C语言函数参数传递模式入门详解
3.用户上传头像
Installation and configuration of Spark and related ecological components - quick recall
What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
STM32LL库使用——SPI通信
IPV4和IPV6是什么?
Configure clangd for vscode
mysql的索引结构为什么选用B+树?
Spark及相关生态组件安装配置——快速回忆
pygame拖动条的实现方法
cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so
2021-10-14
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
MATLAB图形加标注的基本方法入门简介
Win11怎么在右键菜单添加一键关机选项
【系统设计与实现】基于flink的分心驾驶预测与数据分析系统
GMP scheduling model of golang
win11一直弹出用户账户控制怎么解决
使用 腾讯云搭建一个个人博客
pygame绘制弧线