代码之家  ›  专栏  ›  技术社区  ›  kwadratens

Delphi中Z得分到P值-单尾到双尾

  •  -3
  • kwadratens  · 技术社区  · 8 年前

    我从网络中的z值中找到了一个现成的p值计算器(对Delphi来说有点难)。不幸的是,它只给出左尾的值,我需要它给出双尾的值。有人能告诉我如何计算或更改代码吗?

    function NormalZ (const X: Extended): Extended;
    { Returns Z(X) for the Standard Normal Distribution as defined by
      Abramowitz & Stegun. This is the function that defines the Standard
      Normal Distribution Curve.
      Full Accuracy of FPU }
    begin
      Result := Exp (- Sqr (X) / 2.0)/Sqrt (2 * Pi);
    end;
    
    function NormalP (const A: Extended): Single;
    {Returns P(A) for the Standard Normal Distribution as defined by
      Abramowitz & Stegun. This is the Probability that a value is less
      than A, i.e. Area under the curve defined by NormalZ to the left
      of A.
      Only handles values A >= 0 otherwise exception raised.
      Accuracy: Absolute Error < 7.5e-8 }
    const
      B1: Extended = 0.319381530;
      B2: Extended = -0.356563782;
      B3: Extended = 1.781477937;
      B4: Extended = -1.821255978;
      B5: Extended = 1.330274429;
    var
      T: Extended;
      T2: Extended;
      T4: Extended;
    begin
      if (A < 0) then
        raise EMathError.Create ('Value must be Non-Negative')
      else
      begin
        T := 1 / (1 + 0.2316419 * A);
        T2 := Sqr (T);
        T4 := Sqr (T2);
        Result := 1.0 - NormalZ (A) * (B1 * T + B2 * T2
          + B3 * T * T2 + B4 * T4 + B5 * T * T4);
      end;
    end;
    

    根据在线计算器,例如: https://www.easycalculation.com/statistics/p-value-for-z-score.php 对于给定的z分数: 0.70710678号 这个密码给了我

    谢谢你的帮助。

    2 回复  |  直到 8 年前
        1
  •  1
  •   David Dubois    8 年前

    你正在寻找的公式可以在你链接的网页的源代码中找到。

    如果X是P函数的结果,则计算2*(1-X)。

    我也从源代码中翻译了Z函数。它从您提供的函数生成不同的结果。我说不出哪个更准确。

    function pzscore(z:extended):extended;
    const
      Z_MAX = 6;
    var
      x, y, w : extended;
    begin
      if z = 0.0 then
        x := 0.0
      else
        begin
          y := 0.5 * abs(z);
          if y > Z_MAX * 0.5 then
            x := 1.0
          else
            if y < 1.0 then
              begin
                w := y * y;
                x := ((((((((0.000124818987 * w
                     - 0.001075204047) * w + 0.005198775019) * w
                     - 0.019198292004) * w + 0.059054035642) * w
                     - 0.151968751364) * w + 0.319152932694) * w
                     - 0.531923007300) * w + 0.797884560593) * y * 2.0;
              end
            else
              begin
                Y := Y - 2.0;
                x := (((((((((((((-0.000045255659 * y
                     + 0.000152529290) * y - 0.000019538132) * y
                     - 0.000676904986) * y + 0.001390604284) * y
                     - 0.000794620820) * y - 0.002034254874) * y
                     + 0.006549791214) * y - 0.010557625006) * y
                     + 0.011630447319) * y - 0.009279453341) * y
                     + 0.005353579108) * y - 0.002141268741) * y
                     + 0.000535310849) * y + 0.999936657524;
              end;
        end;
      if x > 0.0 then
        Result :=  (x + 1.0) * 0.5
      else
        Result :=  (1.0 - x) * 0.5;
    end;
    
        2
  •  0
  •   kwadratens    8 年前

    对不起,我认为这是一个愚蠢的问题-从我得到的反对票来看。好。;)

    实际上,我不认为模式可以在页面代码中,所以我感到羞愧。谢谢你大卫·杜布瓦。规范开挖部分:

    var lp = pzscore(z);
    document.getElementById('lp').value = lp.toFixed(4);
    var rp = 1 - lp;
    document.getElementById('rp').value = rp.toFixed(4);
     var tp = 2 * rp;
    document.getElementById('tp').value = tp.toFixed(4);
     var cl = 1 - tp;
    document.getElementById('cl').value = cl.toFixed(4);
    

    不需要新的函数,上面使用的是与eg SPSS和R(我检查过)兼容的有效函数。

    推荐文章