Skip to main content
Known Participant
August 28, 2016
Answered

need a custom calculation script to calculate standard deviation

  • August 28, 2016
  • 1 reply
  • 3920 views

need a custom calculation script to calculate standard deviation from 8 Rows , this will be a .pdf form the input variable data points will be from users using this template as a form.

I can get the average from the simple calculation but struggling with the script for the standard deviation. Any help would be appreciated.

Header 1Header 2Header 3Header 4Header 5Header 6Header 7Header 8Header 9Header 11
sample1Row1sample2Row1sample3Row1sample4Row1sample5Row1sample6Row1sample7Row1sample8Row1Averagestddeviation
This topic has been closed for replies.
Correct answer try67

so if I take sample1Row1, sample2Row1,..... sample8Row1 = 1,2,3,4,5,6,7,8

the average would be 1+2+3+4+5+6+7+8 / 8 = 4.5

The desired output should be :

Stddev = sqrt {[summation [(x - 4.5)^2] / (8-1)} = sqrt { [sum [(1-4.5)^2 +.........+(8-4.5)^2] ] / (8-1)

                                                                           = sqrt { [sum (12.25 + 6.25 + 2.25 + 0.25 + 0.25 +2.25 +6.25+12.25 )] / (8-1)}

                                                                          = sqrt { 42/7}

                                                                          = 6


So the formula is not:

stddev = sqrt (sum (sampleXRow1 - average)^2 / (n-1))

But:

stddev = sqrt (sum ((sampleXRow1 - average)^2) / (n-1))

So the code should be something like this:

var avg = Number(this.getField("AverageRow1").valueAsString);

var sum = 0;

var n = 0;

for (var i=1; i<=8; i++) {

    var v = this.getField("sample"+i+"Row1").valueAsString;

    if (v!="") {

        sum+=Math.pow((Number(v)-avg),2);

        n++;

    }

}

if (n==0 || n==1) event.value = "";

else event.value = Math.sqrt(sum / (n-1));

1 reply

try67
Community Expert
Community Expert
August 29, 2016

What's the formula that you want to use?

Known Participant
August 29, 2016

stddev = sqrt (sum (sampleXRow1 - average)^2 / (n-1))

try67
Community Expert
Community Expert
August 29, 2016

Not 100% sure about this code, but give it a try...

var avg = Number(this.getField("Average").valueAsString);

var sum = 0;

var n = 0;

for (var i=1; i<=8; i++) {

    var v = this.getField("sample"+i+"Row1").valueAsString;

    if (v!="") {

        sum+=(Number(v)-avg);

        n++;

    }

}

if (n==0 || n==1) event.value = "";

else event.value = Math.sqrt(Math.pow(sum,2) / (n-1));

I assume that "n" stands for the number of (filled-in) fields.

I'm not sure how are you calculating the average, though, and if you're including empty fields in this calculation...

Edit: Syntax error fixed