Matlab函数rsquare的问题

时间:2017-04-09 12:04:25

标签: matlab function data-fitting

我正在通过以下方式计算我的模型的确定系数:

Rsqrd1 = 1-  sum( (DataSeries(:)-ModelSeries(:)).^2 ) / sum( (DataSeries(:)-mean(ModelSeries(:) )).^2 ) ;

但是,当我使用下面的Matlab函数时,我会收到不同的结果(零,即)。我的问题可能是什么? (我不完全理解Matlab函数的编码以及如何测试它,这就是为什么我只能比较它的一部分......)

[Rsqrd2, ~] = rsquare(DataSeries,ModelSeries);

仅供参考,以下是我使用以下两个结果的数据样本:

Rsqrd1 = 0.300314935784522;
Rsqrd2 = 0;

DataSeries = [   8.109999999999999;   7.340000000000000;   8.140000000000001;   8.529999999999999;   8.580000000000000;  10.060000000000000;  34.219999999999999;  37.899999999999999;  41.490000000000002;  41.960000000000001;  43.280000000000001;  42.950000000000003;  40.950000000000003;  41.979999999999997;  43.909999999999997;  43.250000000000000;  41.259999999999998;  49.960000000000001;  59.829999999999998;  56.850000000000001;  49.960000000000001;  39.420000000000002;  43.020000000000003;  36.509999999999998;  37.020000000000003;  35.829999999999998;  32.289999999999999;  30.079999999999998;  30.010000000000002;  28.030000000000001;  27.210000000000001;  30.460000000000001;  36.600000000000001;  38.799999999999997;  40.320000000000000;  40.530000000000001;  38.500000000000000;  36.719999999999999;  33.000000000000000;  32.009999999999998;  32.869999999999997;  40.210000000000001;  47.490000000000002;  44.950000000000003;  36.890000000000001;  34.520000000000003;  36.000000000000000;  32.289999999999999;  33.060000000000002;  29.039999999999999;  25.590000000000000;  23.340000000000000;  23.250000000000000;  24.989999999999998;  15.119999999999999;  26.010000000000002;  34.649999999999999;  36.189999999999998;  37.500000000000000;  38.560000000000002;  36.930000000000000;  33.030000000000001;  29.930000000000000;  28.390000000000001;  26.390000000000001;  33.039999999999999;  38.990000000000002;  33.930000000000000;  27.079999999999998;  18.899999999999999;  25.070000000000000;  12.250000000000000;  13.630000000000001;  15.380000000000001;  22.480000000000000;  26.090000000000000;  28.390000000000001;  31.320000000000000;  48.689999999999998;  59.969999999999999;  54.789999999999999;  44.000000000000000;  42.090000000000003;  38.390000000000001;  37.649999999999999;  32.590000000000003;  29.800000000000001;  28.649999999999999;  29.180000000000000;  34.960000000000001;  37.640000000000001;  36.939999999999998;  29.579999999999998;  26.370000000000001;  19.079999999999998;   8.789999999999999;  11.279999999999999;  10.460000000000001;   8.619999999999999;   8.770000000000000;   7.270000000000000;  20.059999999999999;  31.820000000000000;  39.460000000000001;  38.840000000000003;  38.520000000000003;  38.039999999999999;  33.590000000000003;  32.979999999999997;  32.960000000000001;  36.640000000000001;  38.119999999999997;  39.549999999999997;  46.990000000000002;  58.469999999999999;  54.539999999999999;  39.770000000000003;  37.560000000000002;  39.259999999999998;  32.049999999999997;  34.729999999999997;  34.630000000000003;  31.260000000000002;  30.039999999999999;  30.280000000000001;  31.890000000000001;  41.670000000000002;  59.890000000000001;  59.890000000000001;  59.329999999999998;  58.409999999999997;  59.090000000000003;  55.969999999999999;  53.520000000000003;  49.939999999999998;  47.289999999999999;  47.570000000000000;  55.899999999999999;  59.939999999999998;  59.240000000000002;  52.939999999999998;  38.329999999999998;  36.710000000000001;  35.460000000000001;  35.810000000000002;  34.840000000000003;  33.259999999999998;  32.590000000000003;  33.259999999999998;  34.090000000000003;  44.939999999999998;  59.430000000000000;  58.270000000000003;  58.270000000000003;  58.270000000000003;  58.079999999999998;  56.039999999999999;  55.430000000000000;  53.950000000000003;  53.439999999999998;  51.969999999999999;  58.880000000000003;  82.299999999999997;  70.109999999999999;  62.310000000000002;  51.930000000000000;  45.950000000000003;  37.549999999999997];

ModelSeries = [   8.109999999999998;   8.033959427220921;   8.323743041322626;   8.642708281120481;   8.582517975760446;  11.267851534695547;  19.265627892290375;  24.383823505293059;  24.255451695071979;  23.221588621286294;  23.061055862798810;  22.113019289306415;  21.371289863159902;  20.649139051697372;  20.517347467041816;  20.204389732885463;  20.152889919509455;  23.796341860624523;  28.357685405641778;  26.637620279541537;  22.250623983516938;  18.521786633703048;  17.231002075227554;  13.067216036414376;  19.142739022137061;  17.721978391048754;  15.855447009714094;  14.671720684372842;  14.681919810093815;  14.765381876692128;  11.357099766936273;  15.816337667523761;  20.080445840613475;  21.201625338935443;  22.073981571269272;  22.515202276491678;  21.543798733221458;  19.960000354644297;  18.067072108251327;  17.381842068691483;  17.031516337275718;  21.137586012909246;  25.023728337649139;  22.772401470260085;  18.479087827412922;  14.972363725126110;  17.654172434932196;  11.716255484871617;  19.715961876585990;  18.155212284709371;  16.157517128358631;  14.873595374529506;  14.807724678847782;  14.816736894019828;  11.339910137512746;  15.714998343157673;  19.855463769222808;  20.864416383513849;  21.621229985316365;  21.951821367820752;  20.909538825844994;  19.286028051295396;  17.380523953397315;  16.649325149265788;  16.244747678122277;  20.077382742947545;  23.671685948867548;  21.455816819780182;  17.342414495551786;  13.997360909813795;  16.442356118812683;  10.871782060716438;   9.254066806706989;   9.020435845559256;   9.198343907518664;   9.402433405571619;   9.194123813724323;  11.889052472788039;  20.026481568965160;  24.977165077948360;  24.489207826870764;  23.114540096244365;  22.636235398768406;  21.409588152343272;  20.414075626818608;  19.464490741778725;  19.090081921322344;  18.560137046203984;  18.282057191300733;  21.323265914583974;  25.105779337411779;  23.305664987061846;  19.243112642200309;  15.837518714974767;  14.571028978312411;  10.930566921047774;   8.043061483121409;   7.828278448844956;   7.971443209965454;   8.137571402681322;   7.947505847139930;  10.477597371114410;  17.994873577687876;  22.884932423902036;  22.880903074120706;  22.024300523218443;  21.996973699650383;  21.219211759187829;  20.636217357395591;  20.069520890245851;  20.077349735794858;  19.910967044646512;  20.005696385024002;  23.801346418078619;  28.585076924647108;  27.067028838815997;  22.796087957505328;  19.136718350436571;  17.957761530043999;  13.739457493789578;  10.310824888906227;  10.234357169105408;  10.627415478755154;  11.062492850356721;  11.016017242485642;  14.506641643689173;  24.884595907986515;  31.606278720197437;  31.557624279398006;  30.332367155586006;  30.248647555318538;  29.132457008298452;  28.284532096511711;  27.459546453686421;  27.419960762893837;  27.140870313592835;  27.215931128615544;  32.312946353894560;  38.724658154432746;  36.587267960523683;  30.743914731877290;  25.748050465465422;  24.103274970768545;  18.395418569792440;  13.769537490600122;  13.631459113517636;  14.116779371574737;  14.654058009734536;  14.551159058407341;  19.106427552870549;  32.677949691257076;  41.379150497789212;  41.187871023605659;  39.464033938070862;  39.228799119138912;  37.657741343154058;  36.440049352143852;  35.257483623102601;  35.085564440852409;  34.607106722839973;  34.579707730445875;  40.908025648164319;  48.846243022607112;  45.979419502968788;  38.491201346114494;  32.114034660484577;  29.947100964693604;  22.766547766228452];

1 个答案:

答案 0 :(得分:1)

获得一些数据有助于确定问题。 你指的是发布了here的Matlab函数吗?

如果是,在发布的示例中,它似乎适用于行向量。也许这可能是问题(?)我无法访问Matlab,但可能这可能有效:

rsquareColumnTest = @(x,y)rsquare((x(:))'),(y(:)。'));

然后Rsqrd2 = rsquareColumnTest(DataSeries,ModelSeries);

如果通过测试,当然可以直接传递数据的转置,而无需定义匿名函数。

收到数据后进一步编辑

感谢您提供数据。

首先:例如,R ^ 2通常不是一个好主意,你可能想要看看at this site

那就是说,如果你想比较线性模型相对于常数模型的作用,R ^ 2是有意义的。常数模型是线性模型的子集,这就是为什么R ^ 2永远不会为负(或者如果使用得当,则永远不应为负)的原因。

如果您查看definition,则将R ^ 2定义为

Rsqrd1 = 1 - sumSqDiff(DataSeries, ModelSeries) / sumSqDiff(DataSeries, mean(ModelSeries));

我已经定义了

sumSqDiff = @(x,y)  sum( (x(:)-y(:)).^2 );

根据R ^ 2的定义(我鼓励你看看为什么),你应该使用:

Rsqrd1 = 1-sumSqDiff(DataSeries, ModelSeries) / sumSqDiff(DataSeries, mean(DataSeries); 

如果你这样做,你有Rsqrd1 = -0.6791; 这是没有意义的,因为R ^ 2介于0和1之间,但由于您的模型不是线性模型(不包含常量模型,作为子集),因此可能。您在网络上找到的rsquare功能将此考虑在内并将其限制在0,特别是它说

r2 = max(0,1 - sum((y(:)-f(:)).^2)/sum((y(:)-mean(y(:))).^2));

由于r2为负数,因此结果为0。您可以使用该功能,考虑到您的模型中没有恒定的术语,可以调用它 rsquare(DataSeries,ModelSeries,false)(如果您查看文档,则引用此案例)。

同样,我鼓励你不要使用R ^ 2,尤其是在这种情况下,但我希望我已经澄清了你为什么会有不同的结果。