MATLAB unary linear regression analysis application example
Author: Kailugaji - Blog Park http://www.cnblogs.com/kailugaji/
huigui.m
function [b,bint,r,rint,states,sima2,p,y0,zxqj]= huigui(x,y,x0) %x – an n×p matrix of n observations of the independent variable of the p-element linear model, y - An n × 1 vector of n observations of the dependent variable of a p-element linear model, where x0 is the abscissa of the predicted value %b - least squares estimate of model coefficient β, bint - 100(1-alpha)% confidence interval for model coefficient β, r - model fit residual, rint - 100(1-alpha) model fit residual )% confidence interval. %stats - contains the R^2 statistic, the value of the F statistic of the ANOVA, the significant probability p value of the ANOVA and the estimated value of sigama^2, y0 is the predicted value ordinate format short ; x1=[ones(length(x),1),x]; [b,bint,r,rint,states]=regress(y,x1); sima2=(vpa(states(4),20)); p =vpa(states( 3 ), 20 ); p value of % test p<0.01, the regression equation is highly significant; 0.0.1<=p<0.05, the regression equation is significant; p>=0.05 , the regression equation is not significant y0 = b( 1 )+b( 2 )* x0; s=sqrt(states(4)); zxqj =[y0- 2 *s,y0+ 2 *s]; % confidence interval plot(x,y, ' . ' ),lsline %rcoplot(r,rint) %residual analysis
result:
>> x=[1,2,3,4,5,6,7,8,9]'; >> y=[2,4,6,8,10,11,14,16,18]'; >> x0=10; >> [b,bint,r,rint,states,sima2,p,y0,zxqj]=huigui(x,y,x0) b = -0.0278 1.9833 bint = -0.6342 0.5786 1.8756 2.0911 r = 0.0444 0.0611 0.0778 0.0944 0.1111 -0.8722 0.1444 0.1611 0.1778 rint = -0.6654 0.7543 -0.7116 0.8338 -0.7363 0.8918 -0.7426 0.9315 -0.7321 0.9543 -0.8722 -0.8722 -0.6611 0.9500 -0.5981 0.9203 -0.5124 0.8679 states = 1.0e + 03 * 0.0010 1.8941 0.0000 0.0001 sima2 = 0.12460317460317460317 p = 0.00000000088276169535500757861 y0 = 19.8056 zxqj = 19.0996 20.5115
Residual plot: