And furthering the work of esteemed monk tlm above, the difference is explained completely by whether or not your final step divides by (N-1) or by N.
Data Set Div(N-1) Div N
------------ ---------- -------
condition1 50.73 49.56
condition2 44.34 43.32
condition3 52.05 50.85
condition4 56.39 55.10
use strict;
use warnings;
use List::Util 'sum';
sub sample_sd {
die unless @_ and ref $_[ 0 ] eq 'ARRAY' and @{ $_[ 0 ] } > 1;
my $data = shift;
my $sum = sum @$data;
my $n = @$data;
my $mean = $sum/$n;
my $sq_dev = sum map $_*$_, map $_ - $mean, @$data;
return sqrt( ( $sq_dev )/( $n - 1 ) );
}
sub sample_sd2 {
die unless @_ and ref $_[ 0 ] eq 'ARRAY' and @{ $_[ 0 ] } > 1;
my $data = shift;
my $sum = sum @$data;
my $n = @$data;
my $mean = $sum/$n;
my $sq_dev = sum map $_*$_, map $_ - $mean, @$data;
return sqrt( ( $sq_dev )/( $n ) );
}
while ( <DATA> ) {
my @data = split;
my $label = shift @data;
my $sd = sample_sd( \@data );
my $sd2 = sample_sd2( \@data );
printf "$label %.2f %.2f\n", $sd, $sd2;
}
__DATA__
condition1 397 322 350 346 338 324 461 477 432 373 386 412 475 360.5 3
+84 324 424 319 454.5 412 387 370
condition2 360.5 318.5 356 316.5 330 296.5 444 474 416.5 355 320 401.5
+ 363.5 332 363 324 388 312 344 372 347.5 338
condition3 372 354 358 311 336.5 297 485 461 407.5 365 342 428 494 343
+.5 372 324 379.5 345 389 384 363 346
condition4 324 305.5 364 320 327.5 288.5 432.5 434 447 369 315 397.5 5
+15 339 405 330 396 319 345 381.5 340 317
Update: Which is exactly what the following posts (at least) had attempted to tell you: Re^2: standard deviation accuracy question Re: standard deviation accuracy question and Re^2: standard deviation accuracy question-Scott
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|