Linear function Fitting R Language Example _r language

Source: Internet
Author: User
Linear function fitting (Y=A+BX)

1. R Run Instance

The R language runs the code as follows: Green for the data to be provided, the yellow identification information is required to be saved.

X<-c (0.10,0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.20, 0.21, 0.23)

Y<-c (42.0,43.5, 45.0, 45.5, 45.0, 47.5, 49.0, 53.0, 50.0, 55.0, 55.0, 60.0)

Data1=data.frame (x=x,y=y) #数据存入数据框

#拟合线性函数

LM.DATA1<-LM (y~ x,data=data1)

Summary (LM.DATA1) #输出拟合后信息

Call:

LM (formula = data1$y ~ data1$x)

Residuals: #残差分位数 (all residual values are shown here when the number of residuals is low)

Min 1Q Median 3Q Max

-2.0431-0.7056 0.1694 0.6633 2.2653

Coefficients:

#系数估计值 #系数标准误差 #t检验值 #对应t值概率的2倍

Estimate Std. Error T value Pr (>|t|)

(Intercept) 28.493 1.580 18.04 5.88e-09 * * *

data1$x 130.835 9.683 13.51 9.50e-08 * * *

---

Signif. codes:0 ' * * * 0.001 ' * * ' 0.01 ' * ' 0.05 '. ' 0.1 "' 1

Residual standard error:1.319 on 10degrees of Freedom

#拟合优度判定系数 #修正的拟合优度判定系数

Multiple r-squared:0.9481, adjustedr-squared:0.9429

#F检验值 #F检验值对应的概率

f-statistic:182.6 on 1 and DF, p-value:9.505e-08

T-Test value corresponds to PR (>|t|) : This number is twice times the probability of T-test, so its value is less than 0.1, it shows that the corresponding independent variable is significant for the variable, of course, the smaller the value, the higher the significance.

F test value corresponding to the P-VALUE:F test value of the corresponding probability. When the value is less than 0.05, the regression equation is significant.

Adjusted r-squared modified fitting: The closer the value is to 1, the better the model fits.

/*********************** Numerical output ********************************/

Coefficient<-lm.data1$coefficients #回归所有系数列举

COEFFICIENT[1] #系数a: Intercept

coefficient [2] #系数b

Tvalue<-summary (lm.data1) $coefficients [1:2,3] #t检验值

TVALUE[1] #系数a对应的t检验值

TVALUE[2] #系数b对应的t检验值

Pr<-summary (lm.data1) $coefficients [1:2,4] #t检验值对应的概率的2倍

PR[1] #系数a对应t检验值对应的概率2倍

PR[2] #系数b对应t检验值对应的概率2倍

Arsquared<-summary (LM.DATA1) $adj. r.squared #修正的拟合优度判定系数

F_info<-summary (lm.data1) $fstatistic #F检验的F值, degrees of freedom DF and probability

F_INFO[1] #F检验值

F_INFO[2] #F检验量的自由度

F_INFO[3] #F检验值对应的概率

/****************************************************************/

#残差正态性检验

Ks.test (Lm.data1$residuals, "Pnorm", 0)

One-samplekolmogorov-smirnov Test

Data:lm.data1$residuals

#D检验值 #对应的概率值

D = 0.1332, P-value = 0.9648

Alternative hypothesis:two-sided

The probability value corresponding to the D test value is P-value: The value is greater than 0.05, indicating that the residuals obey the normal distribution with the mean value of 0.

/*********************** Numerical output ********************************/

Ks_test<-ks.test (Lm.data1$residuals, "Pnorm", 0) #获取正态性检验信息

KS_TEST[1] #KS检验值

KS_TEST[2] #KS检验值对应的概率

/****************************************************************/

#残差独立性检验: DW method to test the autocorrelation of residual error sequences

Dwtest (LM.DATA1)

Durbin-watsontest

Data:lm.data1

#d. W Test Value # corresponding to the probability value

DW = 2.5465, P-value = 0.7422

Alternative hypothesis:trueautocorrelation is greater than 0

DW test value corresponding to the probability value P-value: This value is greater than 0.05, the residual sequence is independent.

/*********************** Numerical output ********************************/

Dw_test<-dwtest (LM.DATA1) #获取独立性检验信息

DW_TEST[1] #DW检验值

DW_TEST[4] #DW检验值对应的概率

/****************************************************************/

#残差同方差检验

Bptest (LM.DATA1)

Studentizedbreusch-pagan Test

Data:lm.data1

#BP检验值 #自由度 #对应概率值

BP = 0.9831, df = 1, P-value = 0.3214

The probability value of the BP test value is P-value: The value is greater than 0.05, which indicates that the residual difference is the same variance.

/*********************** Numerical output ********************************/

bp_test<-bptest (lm.data1) #获取独立性检验信息

BP_TEST[1] #BP检验值

BP_TEST[2] #BP检验量的自由度

BP_TEST[4] #BP检验值对应的概率

/****************************************************************/

2. Curve Fitting

Ab<-round (lm.data1$coefficients[1],3) #回归方程系数a, retain 3 decimal places
Bb<-round (lm.data1$coefficients[2],3) #回归方程系数b, retain 3 decimal places
Plot (data1$x,data1$y,xlab= "x", Ylab = "Y", col= "Red", pch= "*") #训练数据点

Abline (lm.data1,col= "Blue") #拟合曲线

Text (mean (data1$x), Max (data1$y), paste ("y =", BB, "x+ (", AB, ")", Sep = "") #方程式



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.