Skip to main content
Known Participant
Aug 28, 2016 11:51 PM UTC
Answered

need a custom calculation script to calculate standard deviation

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Aug 29, 2016 3:51 PM UTC

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));

22 replies

try67
Community Expert
Community Expert
Aug 29, 2016 9:03 AM UTC

What's the formula that you want to use?

Translate
Known Participant
Aug 29, 2016 1:37 PM UTC

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

Translate
try67
Community Expert
Community Expert
Aug 29, 2016 1:43 PM UTC

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

Translate