两个自变量x1、x2,一个因变量y。通过绘图可以看出各个自变量和因变量的关系分布类似于正态分布,因此选用二维正太分布模型拟合。
下面是二维正态分布模型:
http://baike.baidu.com/view/9306579.htm
可详见 http://baike.baidu.com/view/9306579.htm 百度百科
下面是数据:
y x1 x2
0 1000 2
0 1200 5
1 1400 15
1 1570 20
2 1720 30
2 1790 40
3 1860 60
4 1880 90
6 1980 172
7 2250 200
7 2530 250
8 2850 350
9 3100 450
10 3300 550
11 3500 650
12 3600 800
13 3800 1100
14 4000 1440
14 4200 1501
14 4500 1563
14 4800 1637
6 6800 2863
下面是函数代码:
function yy=fun(beta,x)
b1 = beta(1);
b2 = beta(2);
b3 = beta(3);
b4 = beta(4);
b5 = beta(5);
yy=(2*pi*b3*b4*(1-b5^2)^(1/2))^(-1)*exp(-1/2*(1-b5^2)^(-1)*((x(:,1)-b1)^2*b3^(-2)-2*b5*(x(:,1)-b1)*(x(:,2)-b2)*(b3*b4)^(-1)+(x(:,2)-b2)^2*b4^(-2)));
下面是程序运行代码:
clc
clear
data=load('data.txt');
y=data(:,1);
x=data(:,2:3);
beta=[1,0.1,3,1,0.5];
[beta_out,r,J,COVB,mse]=nlinfit(x,y,@fun,beta);
beta_out;
mse;
betaci=nlparci(beta_out,r,'Jacobian',J);
betaa=[beta_out',betaci];
[yy,delta]=nlpredci(@fun,x,beta_out,r,'Jacobian',J);
alpha=0.05;
nlintool(x,y,'fun',beta,alpha)
r=corrcoef(y,yy);
plotregression(y,yy)
regstats(y,yy)
运行结果:
Warning: Rank deficient, rank = 0, tol = 0.000000e+00.
> In nlinfit>LMfit at 351
In nlinfit at 184
In nihe at 7
Warning: Some columns of the Jacobian are effectively zero at the solution, indicating that the
model is insensitive to some of its parameters. That may be because those parameters are not
present in the model, or otherwise do not affect the predicted values. It may also be due to
numerical underflow in the model function, which can sometimes be avoided by choosing better
initial parameter values, or by rescaling or recentering. Parameter estimates may be unreliable.
> In nlinfit at 253
In nihe at 7
Warning: Matrix is singular to working precision.
> In nlinfit at 274
In nihe at 7
Warning: Matrix is singular to working precision.
> In nlparci at 104
In nihe at 10
Warning: Matrix is singular to working precision.
> In nlpredci at 181
In nihe at 12
Warning: Matrix is singular to working precision.
> In nlintool>PredictionsPlusError at 409
In nlintool at 257
In nihe at 14
Warning: Matrix is singular to working precision.
> In nlintool>PredictionsPlusError at 409
In nlintool at 257
In nihe at 14
Warning: Matrix is singular to working precision.
> In nlintool>PredictionsPlusError at 409
In nlintool at 271
In nihe at 14
Warning: Matrix is singular to working precision.
> In regstats at 193
In regstats at 155
In nihe at 17
Warning: Matrix is singular to working precision.
> In regstats at 208
In regstats at 155
In nihe at 17
出现很多错误,拟合度很低或者出现NaN。
求大神看是什么问题。 |