Seeking a statistical javascript function to return p-value from a z-score

前端 未结 3 1346
日久生厌
日久生厌 2020-12-06 11:38

I need to convert z-scores to percentile. I found reference to a function in the jStat library that I could use (jstat.ztest), but the jStat documentation seems to be ahead

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 12:29

    Just editing the code from Paul's answer for a two-sided t-test

    function GetZPercent(z) 
    {
    //z == number of standard deviations from the mean
    
    //if z is greater than 6.5 standard deviations from the mean
    //the number of significant digits will be outside of a reasonable 
    //range
    if ( z < -6.5)
      return 0.0;
    if( z > 6.5) 
      return 1.0;
    
    if (z > 0) { z = -z;}
    
    var factK = 1;
    var sum = 0;
    var term = 1;
    var k = 0;
    var loopStop = Math.exp(-23);
    while(Math.abs(term) > loopStop) 
    {
      term = .3989422804 * Math.pow(-1,k) * Math.pow(z,k) / (2 * k + 1) / Math.pow(2,k) * Math.pow(z,k+1) / factK;
      sum += term;
      k++;
      factK *= k;
    
    }
    sum += 0.5;
    
    return (2*sum);
    }
    

提交回复
热议问题