private Result regress(int[] target,String targetName,double[][] regressors,List<String> regressorNames){
  double[][] x;
  double[] c1;
  int numRegressors=regressors.length;
  int numCases=target.length;
  x=new double[numRegressors + 1][];
  c1=new double[numCases];
  x[0]=c1;
  System.arraycopy(regressors,0,x,1,numRegressors);
  for (int i=0; i < numCases; i++) {
    x[0][i]=1.0;
    c1[i]=1.0;
  }
  double[] xMeans=new double[numRegressors + 1];
  double[] xStdDevs=new double[numRegressors + 1];
  double[] y0=new double[numCases];
  double[] y1=new double[numCases];
  for (int i=0; i < numCases; i++) {
    y0[i]=0;
    y1[i]=0;
  }
  int ny0=0;
  int ny1=0;
  int nc=0;
  for (int i=0; i < numCases; i++) {
    if (target[i] == 0.0) {
      y0[i]=1;
      ny0++;
    }
 else {
      y1[i]=1;
      ny1++;
    }
    nc+=y0[i] + y1[i];
    for (int j=1; j <= numRegressors; j++) {
      xMeans[j]+=(y0[i] + y1[i]) * x[j][i];
      xStdDevs[j]+=(y0[i] + y1[i]) * x[j][i] * x[j][i];
    }
  }
  for (int j=1; j <= numRegressors; j++) {
    xMeans[j]/=nc;
    xStdDevs[j]/=nc;
    xStdDevs[j]=Math.sqrt(Math.abs(xStdDevs[j] - xMeans[j] * xMeans[j]));
  }
  xMeans[0]=0.0;
  xStdDevs[0]=1.0;
  for (int i=0; i < nc; i++) {
    for (int j=1; j <= numRegressors; j++) {
      x[j][i]=(x[j][i] - xMeans[j]) / xStdDevs[j];
    }
  }
  double[] par=new double[numRegressors + 1];
  double[] parStdErr=new double[numRegressors + 1];
  double[] coefficients;
  par[0]=Math.log((double)ny1 / (double)ny0);
  for (int j=1; j <= numRegressors; j++) {
    par[j]=0.0;
  }
  double[][] arr=new double[numRegressors + 1][numRegressors + 2];
  double lnV;
  double ln1mV;
  double llP=2e+10;
  double ll=1e+10;
  double llN=0.0;
  while (Math.abs(llP - ll) > 1e-7) {
    llP=ll;
    ll=0.0;
    for (int j=0; j <= numRegressors; j++) {
      for (int k=j; k <= numRegressors + 1; k++) {
        arr[j][k]=0.0;
      }
    }
    for (int i=0; i < nc; i++) {
      double q;
      double v=par[0];
      for (int j=1; j <= numRegressors; j++) {
        v+=par[j] * x[j][i];
      }
      if (v > 15.0) {
        lnV=-Math.exp(-v);
        ln1mV=-v;
        q=Math.exp(-v);
        v=Math.exp(lnV);
      }
 else {
        if (v < -15.0) {
          lnV=v;
          ln1mV=-Math.exp(v);
          q=Math.exp(v);
          v=Math.exp(lnV);
        }
 else {
          v=1.0 / (1 + Math.exp(-v));
          lnV=Math.log(v);
          ln1mV=Math.log(1.0 - v);
          q=v * (1.0 - v);
        }
      }
      ll=ll - 2.0 * y1[i] * lnV - 2.0 * y0[i] * ln1mV;
      for (int j=0; j <= numRegressors; j++) {
        double xij=x[j][i];
        arr[j][numRegressors + 1]+=xij * (y1[i] * (1.0 - v) + y0[i] * (-v));
        for (int k=j; k <= numRegressors; k++) {
          arr[j][k]+=xij * x[k][i] * q* (y0[i] + y1[i]);
        }
      }
    }
    if (llP == 1e+10) {
      llN=ll;
    }
    for (int j=1; j <= numRegressors; j++) {
      for (int k=0; k < j; k++) {
        arr[j][k]=arr[k][j];
      }
    }
    for (int i=0; i <= numRegressors; i++) {
      double s=arr[i][i];
      arr[i][i]=1.0;
      for (int k=0; k <= numRegressors + 1; k++) {
        arr[i][k]=arr[i][k] / s;
      }
      for (int j=0; j <= numRegressors; j++) {
        if (i != j) {
          s=arr[j][i];
          arr[j][i]=0.0;
          for (int k=0; k <= numRegressors + 1; k++) {
            arr[j][k]=arr[j][k] - s * arr[i][k];
          }
        }
      }
    }
    for (int j=0; j <= numRegressors; j++) {
      par[j]+=arr[j][numRegressors + 1];
    }
  }
  double chiSq=llN - ll;
  String[] sigMarker=new String[numRegressors];
  double[] pValues=new double[numRegressors + 1];
  double[] zScores=new double[numRegressors + 1];
  for (int j=1; j <= numRegressors; j++) {
    par[j]=par[j] / xStdDevs[j];
    parStdErr[j]=Math.sqrt(arr[j][j]) / xStdDevs[j];
    par[0]=par[0] - par[j] * xMeans[j];
    double zScore=par[j] / parStdErr[j];
    double prob=norm(Math.abs(zScore));
    pValues[j]=prob;
    zScores[j]=zScore;
  }
  parStdErr[0]=Math.sqrt(arr[0][0]);
  double zScore=par[0] / parStdErr[0];
  pValues[0]=norm(zScore);
  zScores[0]=zScore;
  double intercept=par[0];
  coefficients=par;
  return new Result(targetName,regressorNames,xMeans,xStdDevs,numRegressors,ny0,ny1,coefficients,parStdErr,pValues,intercept,ll,sigMarker,chiSq,alpha);
}

private Result regress ( int [ ] target , String targetName , double [ ] [ ] regressors , List < String > regressorNames ) { double [ ] [ ] x ; double [ ] c1 ; int numRegressors = regressors . length ; int numCases = target . length ; x = new double [ numRegressors + 1 ] [ ] ; c1 = new double [ numCases ] ; x [ 0 ] = c1 ; System . arraycopy ( regressors , 0 , x , 1 , numRegressors ) ; for ( int i = 0 ; i < numCases ; i ++ ) { x [ 0 ] [ i ] = 1.0 ; c1 [ i ] = 1.0 ; } double [ ] xMeans = new double [ numRegressors + 1 ] ; double [ ] xStdDevs = new double [ numRegressors + 1 ] ; double [ ] y0 = new double [ numCases ] ; double [ ] y1 = new double [ numCases ] ; for ( int i = 0 ; i < numCases ; i ++ ) { y0 [ i ] = 0 ; y1 [ i ] = 0 ; } int ny0 = 0 ; int ny1 = 0 ; int nc = 0 ; for ( int i = 0 ; i < numCases ; i ++ ) { if ( target [ i ] == 0.0 ) { y0 [ i ] = 1 ; ny0 ++ ; } else { y1 [ i ] = 1 ; ny1 ++ ; } nc += y0 [ i ] + y1 [ i ] ; for ( int j = 1 ; j <= numRegressors ; j ++ ) { xMeans [ j ] += ( y0 [ i ] + y1 [ i ] ) * x [ j ] [ i ] ; xStdDevs [ j ] += ( y0 [ i ] + y1 [ i ] ) * x [ j ] [ i ] * x [ j ] [ i ] ; } } for ( int j = 1 ; j <= numRegressors ; j ++ ) { xMeans [ j ] /= nc ; xStdDevs [ j ] /= nc ; xStdDevs [ j ] = Math . sqrt ( Math . abs ( xStdDevs [ j ] - xMeans [ j ] * xMeans [ j ] ) ) ; } xMeans [ 0 ] = 0.0 ; xStdDevs [ 0 ] = 1.0 ; for ( int i = 0 ; i < nc ; i ++ ) { for ( int j = 1 ; j <= numRegressors ; j ++ ) { x [ j ] [ i ] = ( x [ j ] [ i ] - xMeans [ j ] ) / xStdDevs [ j ] ; } } double [ ] par = new double [ numRegressors + 1 ] ; double [ ] parStdErr = new double [ numRegressors + 1 ] ; double [ ] coefficients ; par [ 0 ] = Math . log ( ( double ) ny1 / ( double ) ny0 ) ; for ( int j = 1 ; j <= numRegressors ; j ++ ) { par [ j ] = 0.0 ; } double [ ] [ ] arr = new double [ numRegressors + 1 ] [ numRegressors + 2 ] ; double lnV ; double ln1mV ; double llP = 2e+10 ; double ll = 1e+10 ; double llN = 0.0 ; while ( Math . abs ( llP - ll ) > 1e-7 ) { llP = ll ; ll = 0.0 ; for ( int j = 0 ; j <= numRegressors ; j ++ ) { for ( int k = j ; k <= numRegressors + 1 ; k ++ ) { arr [ j ] [ k ] = 0.0 ; } } for ( int i = 0 ; i < nc ; i ++ ) { double q ; double v = par [ 0 ] ; for ( int j = 1 ; j <= numRegressors ; j ++ ) { v += par [ j ] * x [ j ] [ i ] ; } if ( v > 15.0 ) { lnV = - Math . exp ( - v ) ; ln1mV = - v ; q = Math . exp ( - v ) ; v = Math . exp ( lnV ) ; } else { if ( v < - 15.0 ) { lnV = v ; ln1mV = - Math . exp ( v ) ; q = Math . exp ( v ) ; v = Math . exp ( lnV ) ; } else { v = 1.0 / ( 1 + Math . exp ( - v ) ) ; lnV = Math . log ( v ) ; ln1mV = Math . log ( 1.0 - v ) ; q = v * ( 1.0 - v ) ; } } ll = ll - 2.0 * y1 [ i ] * lnV - 2.0 * y0 [ i ] * ln1mV ; for ( int j = 0 ; j <= numRegressors ; j ++ ) { double xij = x [ j ] [ i ] ; arr [ j ] [ numRegressors + 1 ] += xij * ( y1 [ i ] * ( 1.0 - v ) + y0 [ i ] * ( - v ) ) ; for ( int k = j ; k <= numRegressors ; k ++ ) { arr [ j ] [ k ] += xij * x [ k ] [ i ] * q * ( y0 [ i ] + y1 [ i ] ) ; } } } if ( llP == 1e+10 ) { llN = ll ; } for ( int j = 1 ; j <= numRegressors ; j ++ ) { for ( int k = 0 ; k < j ; k ++ ) { arr [ j ] [ k ] = arr [ k ] [ j ] ; } } for ( int i = 0 ; i <= numRegressors ; i ++ ) { double s = arr [ i ] [ i ] ; arr [ i ] [ i ] = 1.0 ; for ( int k = 0 ; k <= numRegressors + 1 ; k ++ ) { arr [ i ] [ k ] = arr [ i ] [ k ] / s ; } for ( int j = 0 ; j <= numRegressors ; j ++ ) { if ( i != j ) { s = arr [ j ] [ i ] ; arr [ j ] [ i ] = 0.0 ; for ( int k = 0 ; k <= numRegressors + 1 ; k ++ ) { arr [ j ] [ k ] = arr [ j ] [ k ] - s * arr [ i ] [ k ] ; } } } } for ( int j = 0 ; j <= numRegressors ; j ++ ) { par [ j ] += arr [ j ] [ numRegressors + 1 ] ; } } double chiSq = llN - ll ; String [ ] sigMarker = new String [ numRegressors ] ; double [ ] pValues = new double [ numRegressors + 1 ] ; double [ ] zScores = new double [ numRegressors + 1 ] ; for ( int j = 1 ; j <= numRegressors ; j ++ ) { par [ j ] = par [ j ] / xStdDevs [ j ] ; parStdErr [ j ] = Math . sqrt ( arr [ j ] [ j ] ) / xStdDevs [ j ] ; par [ 0 ] = par [ 0 ] - par [ j ] * xMeans [ j ] ; double zScore = par [ j ] / parStdErr [ j ] ; double prob = norm ( Math . abs ( zScore ) ) ; pValues [ j ] = prob ; zScores [ j ] = zScore ; } parStdErr [ 0 ] = Math . sqrt ( arr [ 0 ] [ 0 ] ) ; double zScore = par [ 0 ] / parStdErr [ 0 ] ; pValues [ 0 ] = norm ( zScore ) ; zScores [ 0 ] = zScore ; double intercept = par [ 0 ] ; coefficients = par ; return new Result ( targetName , regressorNames , xMeans , xStdDevs , numRegressors , ny0 , ny1 , coefficients , parStdErr , pValues , intercept , ll , sigMarker , chiSq , alpha ) ; }
regresses the single - column target onto the regressors which have been previously set , generating a regression result . < p > the target must be a two - valued variable with values 0 and 1 . < p > this implements an iterative search .
