Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multivariate Linear Regression #87

Open
tusharmath opened this issue Feb 28, 2015 · 6 comments
Open

Multivariate Linear Regression #87

tusharmath opened this issue Feb 28, 2015 · 6 comments

Comments

@tusharmath
Copy link

Is there a plan to support multivariate linear regression? Some code -

var LinearRegression, MeanError, _;

_ = require('lodash');

MeanError = Error

LinearRegression = (function() {
  function LinearRegression() {}

  LinearRegression.prototype._hypothesis = function(P, Xi) {
    var tmp;
    if (Xi[0] !== 1) {
      throw new MeanError('x1 should be 1');
    }
    tmp = function(cost, Xij, j) {
      return cost + Xij * P[j];
    };
    return _.reduce(Xi, tmp, 0);
  };

  LinearRegression.prototype._diffWithHypothesis = function(X, Y, P) {
    return _.map(Y, (function(_this) {
      return function(Yi, i) {
        return Yi - _this._hypothesis(P, X[i]);
      };
    })(this));
  };

  LinearRegression.prototype._gradientDescent = function(P, X, Y, al) {
    var hypDiff, m;
    m = X.length;
    if (m !== Y.length) {
      throw new MeanError('labels length not matching training data');
    }
    hypDiff = this._diffWithHypothesis(X, Y, P);
    return _.map(P, function(Pj, j) {
      var func1;
      func1 = function(val, Xi, i) {
        return val + hypDiff[i] * Xi[j];
      };
      return Pj + al / m * _.reduce(X, func1, 0);
    });
  };

  LinearRegression.prototype.train = function(X, Y, epoch, al) {
    var P, n, predict, _i, _results;
    if (epoch == null) {
      epoch = 1000;
    }
    if (al == null) {
      al = 0.1;
    }
    X = _.map(X, function(Xi) {
      Xi.unshift(1);
      return Xi;
    });
    n = X[0].length;
    P = (function() {
      _results = [];
      for (var _i = 0; 0 <= n ? _i < n : _i > n; 0 <= n ? _i++ : _i--){ _results.push(_i); }
      return _results;
    }).apply(this).map(function() {
      return 0;
    });
    _.times(epoch, (function(_this) {
      return function() {
        return P = _this._gradientDescent(P, X, Y, al);
      };
    })(this));
    predict = (function(_this) {
      return function(testXi) {
        testXi.unshift(1);
        return _this._hypothesis(P, testXi);
      };
    })(this);
    return {
      predict: predict
    };
  };

  return LinearRegression;

})();

module.exports = LinearRegression;
@tmcw
Copy link
Member

tmcw commented Feb 28, 2015

It's certainly possible: can you point to some implementations of this in other systems that are good examples?

@tusharmath
Copy link
Author

As a matter of fact I created this issue because I couldn't find anything good.

@tusharmath
Copy link
Author

@tmcw Found one - https://github.com/NaturalNode/natural

@tmcw
Copy link
Member

tmcw commented Mar 2, 2015

Hm, where specifically is their implementation? I'm seeing this in the apparatus project that they use, but it's logistic

@tmcw
Copy link
Member

tmcw commented Apr 23, 2015

seeing https://github.com/dambalah/shaman too

@mcclella
Copy link

jStat has a multiple regression function at http://jstat.github.io/models.html#regression_Models

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants