Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update src/regression.js

Fixed some issues with missing semicolons
Updated regresst function (anova results, Standard Error, T score & p value for regressors)
Removed the usage of function wide variables
  • Loading branch information...
commit c61968372171e78400dddb518971660fd7aae540 1 parent 6d68253
ppratscher ppratscher authored
Showing with 105 additions and 108 deletions.
  1. +105 −108 src/regression.js
213 src/regression.js
View
@@ -10,7 +10,6 @@
//Regressions
jStat.extend({
-
buildxmatrix: function(){
//Parameters will be passed in as such
//(array1,array2,array3,...)
@@ -18,8 +17,8 @@ jStat.extend({
//needs to be (1,x1,x2,x3,...)
var matrixRows = new Array(arguments.length);
for(var i=0;i<arguments.length;i++){
- var array = [1]
- matrixRows[i]= array.concat(arguments[i])
+ var array = [1];
+ matrixRows[i]= array.concat(arguments[i]);
}
return jStat(matrixRows);
@@ -31,7 +30,7 @@ jStat.extend({
var matrixRows = new Array(arguments[0].length);
for(var i=0;i<arguments[0].length;i++){
var array = [1]
- matrixRows[i]= array.concat(arguments[0][i])
+ matrixRows[i]= array.concat(arguments[0][i]);
}
return jStat(matrixRows);
@@ -57,43 +56,42 @@ jStat.extend({
matrixmult: function(A,B){
if (A.cols() == B.rows()) {
- if(B.rows()>1){
+ if(B.rows()>1){
+ var result = [];
+ for (var i = 0; i < A.rows(); i++) {
+ result[i] = [];
+ for (var j = 0; j < B.cols(); j++) {
+ var sum = 0;
+ for (var k = 0; k < A.cols(); k++) {
+ sum += A.toArray()[i][k] * B.toArray()[k][j];
+ }
+ result[i][j] = sum;
+ }
+ }
+ return jStat(result);
+ }
var result = [];
- for (var i = 0; i < A.rows(); i++) {
- result[i] = [];
- for (var j = 0; j < B.cols(); j++) {
+ for (var i = 0; i < A.rows(); i++) {
+ result[i] = [];
+ for (var j = 0; j < B.cols(); j++) {
var sum = 0;
- for (var k = 0; k < A.cols(); k++) {
- sum += A.toArray()[i][k] * B.toArray()[k][j];
- }
- result[i][j] = sum;
+ for (var k = 0; k < A.cols(); k++) {
+ sum += A.toArray()[i][k] * B.toArray()[j];
}
+ result[i][j] = sum;
}
- return jStat(result);
- }
- var result = [];
- for (var i = 0; i < A.rows(); i++) {
- result[i] = [];
- for (var j = 0; j < B.cols(); j++) {
- var sum = 0;
- for (var k = 0; k < A.cols(); k++) {
- sum += A.toArray()[i][k] * B.toArray()[j];
- }
- result[i][j] = sum;
}
- }
- return jStat(result);
- }
-
+ return jStat(result);
+ }
},
//regress and regresst to be fixed
regress: function(jMatX,jMatY){
- print("regressin!");
- print(jMatX.toArray());
- var innerinv = jStat.xtranspxinv(jMatX)
- print(innerinv);
+ //print("regressin!");
+ //print(jMatX.toArray());
+ var innerinv = jStat.xtranspxinv(jMatX);
+ //print(innerinv);
var xtransp = jMatX.transpose();
var next = jStat.matrixmult(jStat(innerinv),xtransp);
return jStat.matrixmult(next,jMatY);
@@ -101,35 +99,52 @@ jStat.extend({
},
regresst: function(jMatX,jMatY,sides){
- beta = jStat.regress(jMatX,jMatY);
- xtransp = jMatX.transpose();
- var ones = new Array(jMatX.length);
- for(var i=0; i<jMatX.length;i++){
- ones[i]=[1];
- }
- ones = jStat(ones);
- var onest = ones.transpose();
- var onesquare = jStat.matrixmult(ones,onest);
- var xm = jStat.matrixmult(onesquare,jMatX);
- xm = xm.divide(xm.length)
- var xdev = jStat.matrixsubtract(jMatX,xm);
- var varcovar = jStat.xtranspx(xdev);
- varcovar = varcovar.divide(xdev.length);
- var sds = new Array(xtransp.length)
- var ts = new Array(xtransp.length)
- var ps = new Array(xtransp.length)
- var compile = new Array(xtransp.length)
- for(var i=1; i<beta.length;i++){
- sds[i]=Math.sqrt(varcovar[i][i])
- print(sds[i])
- ts[i]=jStat.tscore(beta[i],0,sds[i],xdev.length);
- ps[i]=jStat.ttest(ts[i],xtransp.length,sides)
- compile[i]=[beta[i],sds[i],ts[i],ps[i]]
+ var beta = jStat.regress(jMatX,jMatY);
+
+ var compile = {};
+ compile.anova = {};
+ var jMatYBar = jStat.jMatYBar(jMatX, beta);
+ compile.yBar = jMatYBar;
+ var yAverage = jMatY.mean();
+ compile.anova.residuals = jStat.residuals(jMatY, jMatYBar);
+
+ compile.anova.ssr = jStat.ssr(jMatYBar, yAverage);
+ compile.anova.msr = compile.anova.ssr / (jMatX[0].length - 1);
+
+ compile.anova.sse = jStat.sse(jMatY, jMatYBar);
+ compile.anova.mse = compile.anova.sse / (jMatY.length - (jMatX[0].length - 1) - 1);
+
+ compile.anova.sst = jStat.sst(jMatY, yAverage);
+ compile.anova.mst = compile.anova.sst / (jMatY.length - 1);
+
+ compile.anova.r2 = 1 - (compile.anova.sse / compile.anova.sst);
+ if (compile.anova.r2 < 0) compile.anova.r2 = 0;
+
+ compile.anova.fratio = compile.anova.msr / compile.anova.mse;
+ compile.anova.pvalue = jStat.anovaftest(compile.anova.fratio, jMatX[0].length - 1, jMatY.length - (jMatX[0].length - 1) - 1);
+
+ compile.anova.rmse = Math.sqrt(compile.anova.mse);
+
+ compile.anova.r2adj = 1 - (compile.anova.mse / compile.anova.mst);
+ if (compile.anova.r2adj < 0) compile.anova.r2adj = 0;
+
+ compile.stats = new Array(jMatX[0].length);
+ var covar = jStat.xtranspxinv(jMatX);
+ var sds, ts, ps;
+
+ for(var i=0; i<beta.length;i++){
+ sds=Math.sqrt(compile.anova.mse * Math.abs(covar[i][i]));
+ ts= Math.abs(beta[i] / sds);
+ ps= jStat.ttest(ts, jMatY.length - jMatX[0].length - 1, sides);
+
+ compile.stats[i]=[beta[i], sds, ts, ps];
}
- return compile
+
+ compile.regress = beta;
+ return compile;
},
- xtranspx : function(jMatX){
+ xtranspx: function(jMatX){
return jStat.matrixmult(jMatX.transpose(),jMatX);
},
@@ -139,66 +154,48 @@ jStat.extend({
var innerinv = jStat.inv(inner);
return innerinv;
},
-
-
- samplesize: function(jMatX){
- return jMatX[0].length;
+
+ jMatYBar: function(jMatX, beta) {
+ var yBar = jStat.matrixmult(jMatX, beta);
+ return new jStat(yBar);
},
- residuals: function(jMatX,jMatY){
- residuals = new Array(xtransp.length)
- var yhat=0;
- for(var i=0; i<jMatX.length;i++){
- yhat=0
- for(var j=0; j<xtransp.length;j++){
- yhat=yhat + beta[j]*jMatX[i][j];
- }
- residuals[i]=jMatY[i]-yhat
- }
- return residuals
+ residuals: function(jMatY, jMatYBar) {
+ return jStat.matrixsubtract(jMatY, jMatYBar);
},
- ssr: function(jMatX,jMatY){
- residuals = jStat.residuals(jMatX,jMatY);
- var SSR=0
- for(var i=0; i<xtransp.length-1;i++){
- SSR= SSR + Math.pow(residuals[i],2)
- }
+ ssr: function(jMatYBar, yAverage) {
+ var ssr = 0;
+ for(var i = 0; i < jMatYBar.length; i++) {
+ ssr += Math.pow(jMatYBar[i] - yAverage, 2);
+ }
+ return ssr;
},
- sse: function(jMatX,jMatY){
- ybar = jStat.mean(jMatY.toArray)
- errors = new Array(xtransp.length)
- var yhat=0;
- for(var i=0; i<jMatX.length;i++){
- yhat=0
- for(var j=0; j<xtransp.length;j++){
- yhat=yhat + beta[j]*jMatX[i][j];
- }
- errors[i]=yhat-ybar
- }
- var sse=0
- for(var i=0; i<xtransp.length-1;i++){
- sse= sse + Math.pow(errors[i],2)
- }
- return sse
-
+ sse: function(jMatY, jMatYBar) {
+ var sse = 0;
+ for(var i = 0; i < jMatY.length; i++) {
+ sse += Math.pow(jMatY[i] - jMatYBar[i], 2);
+ }
+ return sse;
},
- rsq: function(jMatX,jMatY){
- return jStat.sse(jMatX,jMatY) + jStat.ssr(jMatX,jMatY);
-
+ sst: function(jMatY, yAverage) {
+ var sst = 0;
+ for(var i = 0; i < jMatY.length; i++) {
+ sst += Math.pow(jMatY[i] - yAverage, 2);
+ }
+ return sst;
},
matrixsubtract: function(A,B){
- var ans = new Array(A.length);
- for(var i=0;i<A.length;i++){
- ans[i] = new Array(A[i].length);
- for(var j=0;j<A[i].length;j++){
- ans[i][j]=A[i][j]-B[i][j];
+ var ans = new Array(A.length);
+ for(var i=0;i<A.length;i++){
+ ans[i] = new Array(A[i].length);
+ for(var j=0;j<A[i].length;j++){
+ ans[i][j]=A[i][j]-B[i][j];
+ }
}
- }
- return jStat(ans);
- },
-
-})
+ return jStat(ans);
+ }
+});
Please sign in to comment.
Something went wrong with that request. Please try again.