First, briefly introduce the least squares method
When we study the relationship between two variables (x,y), we can usually get a series of pairs of data (x1,y1.x2,y2 ... xm,ym); The data are depicted in an X-y coordinate system, and if they are found near a straight line, the equation can be as y=a0+ A1x (Type 1-1)
Now let's just give it to three dots (1,2), (2,2), (3,1), and look at the scatter chart.
X<-c (1,2,3)
y<-c (2,2,1)
#求相关性
Correlation (x,y)
[1]0.9942198
#相关性算法
#Correlation <-function (x,y) {
# len<-length (x)
# if (len!= length (y))
# Stop ("Length not equal!")
# x2 <-unlist (lapply (X,function (a) return (a^2))) # y2 <-(unlist lapply ( a) Return (a^2))
# xy <-x*y
# a <-sum (XY) *len-sum (x) *sum (y)
# b <-sqrt (SUM (x2 ) *len-sum (x) ^2) *sqrt (sum (y2) *len-sum (y) ^2)
# if (b = = 0)
# Stop ("Data is incorrect!")
# return (A/b)
#}
plot (x,y,col= "Blue", main= "least squares curve fit", xlab= "arguments", Xlim=c (0,4))
Then the fitting method is introduced, and the principle is
In order to establish the linear equation, we should determine A0 and A1, and apply the principle of least squares to the minimum of the measured value Yi and the deviation (yi-yj) of the calculated value Yj (yj=a0+a1xi) (type 1-1) as "optimization criterion".
Order: φ= (Type 1-2)
(Type 1-1) in (type 1-2) to be:
φ= (Type 1-3)
When the minimum, the available function φ for A0, A1 partial derivative, so that these two partial derivative equals zero.
∑2 (a0 + a1*xi-yi) =0 (type 1-4)
∑2xi (A0 +a1*xi-yi) =0 (type 1-5)
Then the A0,A1 is solved and the curve is obtained.
MODEL<-LM (Y~X)
model call
:
lm (formula = y ~ x)
coefficients:
(Intercept) x
2.667
The above coefficients means y=2.667-0.500x, which I manually calculate
y=8/3-1/2x consistent.
Ablink (model)