当前位置:网站首页>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.
边栏推荐
- General code for pytorch model to libtorch and onnx format
- Lightweight AlphaPose
- Failed to install using npx -p @storybook/cli sb init, build a dedicated storybook by hand
- Spark及相关生态组件安装配置——快速回忆
- Publish module to NPM should be how to operate?Solutions to problems and mistake
- Yolov5 official code reading - prior to transmission
- IPV4和IPV6是什么?
- pygame绘制弧线
- 模板系列-并查集
- 用U盘怎么重装Win7系统?如何使用u盘重装系统win7?
猜你喜欢
What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
win10无法直接用照片查看器打开图片怎么办
win10 system update error code 0x80244022 how to do
STM32LL library - USART interrupt to receive variable length information
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
MATLAB绘图函数plot详解
PHY6222蓝牙5.2支持MESH组网M0内核超低功耗
IPV4和IPV6是什么?
win10任务栏不合并图标如何设置
How to update Win11 sound card driver?Win11 sound card driver update method
随机推荐
Use tencent cloud builds a personal blog
General syntax and usage instructions of SQL (picture and text)
How to set the win10 taskbar does not merge icons
Yolov5 official code reading - prior to transmission
【系统设计与实现】基于flink的分心驾驶预测与数据分析系统
6.统一记录日志
pytorch模型转libtorch和onnx格式的通用代码
推开机电的大门《电路》(一):电压,电流,参考方向
BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
2021-10-14
Win10安装了固态硬盘还是有明显卡顿怎么办?
word方框怎么打勾?
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
1.开发社区首页,注册
What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
STM32LL库——USART中断接收不定长信息
实战美团Nuxt +Vue全家桶,服务端渲染,邮箱验证,passport鉴权服务,地图API引用,mongodb,redis等技术点
深入理解Golang之Map
Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal