2013-04-24 18 views
8

我需要將z分數轉換爲百分位數。我找到了可以使用的function in the jStat library的引用(jstat.ztest),但jStat文檔似乎比可用庫更先進,因爲currently available version of the library中沒有這樣的功能。尋求一個統計的javascript函數從z分數返回p值

我認爲有一個more recent version of the library on GitHub,其中可能包括ztest函數,但我是一個Linux新手,無法弄清楚如何從指令構建庫。我花了大部分時間學習git bash和cygwin試圖建立圖書館;我終於決定在這裏問一問最好了。

那麼,任何人都可以指向一個JavaScript函數,我會需要什麼? 另外,任何人都可以指向我包含ztest函數的jStat庫的內置版本嗎?

回答

15

我在網上找到了這個論壇,它的功能就像一個魅力。

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; 

    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 sum; 
    } 

而且我不需要爲一個函數包含一個大型庫。

+0

接受你的答案讓人們知道這個問題已經有了答案。 – Yatrix 2013-04-24 16:47:09

+0

感謝提示@yatrix。它說我必須等待2天才能接受我自己的答案。 – burgerB 2013-04-24 16:53:41

2

只需要編輯從保羅的答案代碼雙面t檢驗

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); 
} 
+1

你應該把你的if語句放在這個函數的最開始。否則當z> 6.5時它將返回1,在你的情況下它應該是0。 – Ire 2015-05-27 05:21:07