没有得到线性回归方程,来自解析字符串的r平方值

时间:2015-08-27 15:15:34

标签: javascript jquery flot

的index.html

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>regression-js example</title>
        <script type="text/javascript" src="jquery-1.11.3.min.js"></script>
        <script type="text/javascript" src="excanvas.min.js"></script>
        <script src="jquery.flot.min.js"></script>
        <script src="linear.js"></script>
        <script type="text/javascript">
            $(function() {
                var datax = "20.248602, 18.54032, 18.254036, 15.217833, 13.194518, 17.521723, 17.78833, 24.018597, 18.401508, 24.46464, 20.752457, 22.570473, 17.870035, 22.474863, 20.783422, 24.075064, 17.72947, 20.202723, 16.776543, 19.087515, 7.232176, 8.508578, 7.5457883, 10.0660925, 13.344348, 5.5784864, 5.3469315, 17.087952";
                datax = datax.split(',');
                var data = [];
                for (i = 0; i < datax.length; i++) {
                    data.push([i, datax[i]]);
                }
                var myRegression = regression('linear', data);
                $.plot($('.graph'), [{
                    data: myRegression.points,
                    label: 'linear'
                }, {
                    title: '27th october',
                    data: data,
                    lines: {
                        show: true,
                    },
                    points: {
                        show: true
                    }
                }, ]);
                $('h2').text(myRegression.string);
                $('h3').text(myRegression.cor);
            });
        </script>
    </head>
    <body>
        <div class="container">
            <h1>Polynomial regression-js</h1>
            <div class="graph"></div>
            <h2></h2>
            <h3></h3>
        </div>
    </body>
</html>

linear.js

;(function () {
    'use strict';

    var methods = {
        linear: function (data) {
            var sum = [0, 0, 0, 0, 0],
                n = 0,
                results = [];

            for (; n < data.length; n++) {
                if (data[n][1] != null) {
                    sum[0] += data[n][0];
                    sum[1] += data[n][1];
                    sum[2] += data[n][0] * data[n][0];
                    sum[3] += data[n][0] * data[n][1];
                    sum[4] += data[n][1] * data[n][1];
                }
            }

            var gradient = (n * sum[3] - sum[0] * sum[1]) / (n * sum[2] - sum[0] * sum[0]);
            var intercept = (sum[1] / n) - (gradient * sum[0]) / n;
            var correlation = Math.pow((n * sum[3] - sum[0] * sum[1]) / Math.sqrt((n * sum[2] - sum[0] * sum[0]) * (n * sum[4] - sum[1] * sum[1])), 2);

            for (var i = 0, len = data.length; i < len; i++) {
                var coordinate = [data[i][0], data[i][0] * gradient + intercept];
                results.push(coordinate);
            }

            var string = 'y = ' + Math.round(gradient * 100) / 100 + 'x + ' + Math.round(intercept * 100) / 100;
            var cor = 'R-squared value = ' + Math.round(correlation * 100) / 100;
            return {
                equation: [gradient, intercept, correlation],
                points: results,
                string: [string, cor]
            };
        }
    };
    var regression = (function (method, data, order) {
        if (typeof method == 'string') {
            return methods[method](data, order);
        }
    });

    if (typeof exports !== 'undefined') {
        module.exports = regression;
    } else {
        window.regression = regression;
    }

}());

在运行此程序时,我收到了以下图片enter image description here

我没有得到正确的方程式,r平方值。我该如何解决这个问题?

如果我更换

var datax="20.248602, 18.54032, 18.254036, 15.217833, 13.194518, 17.521723, 17.78833, 24.018597, 18.401508, 24.46464, 20.752457, 22.570473, 17.870035, 22.474863, 20.783422, 24.075064, 17.72947, 20.202723, 16.776543, 19.087515, 7.232176, 8.508578, 7.5457883, 10.0660925, 13.344348, 5.5784864, 5.3469315, 17.087952";
    datax = datax.split(',');
    var data = [];
    for(i=0; i < datax.length; i++) {
        data.push([i,datax[i]]);
    }    

var data = [[0, 20.248602], [1, 18.54032], [2, 18.254036], [3, 15.217833], [4, 13.194518], [5, 17.521723], [6, 17.78833], [7, 24.018597], ..........];
然后,我能够获得具有方程和r平方值的适当线性回归图表。但我想要相同的输出而不改变它。我怎么才能得到它?我也没有得到图表的标题。

1 个答案:

答案 0 :(得分:0)

您的解析代码为您提供了一个包含字符串的数组:

var data = [[0, "20.248602"], [1, "18.54032"], ...

并且您的回归函数需要数字。将for循环更改为

for (i = 0; i < datax.length; i++) {
    data.push([i, parseFloat(datax[i])]);
}

它有效。请参阅此fiddle