From c8377117389554c8e914052a46b03aa8bcae8364 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Sun, 4 Oct 2009 16:51:57 +0200 Subject: [PATCH] CORRELATIONS: Properly deal with case weights. Corrected a bug in the weighted covariance calculations, and add a test to check that weights are properly handled. --- src/math/covariance.c | 2 +- tests/command/correlation.sh | 76 ++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/math/covariance.c b/src/math/covariance.c index b2d43427..ba0de0b6 100644 --- a/src/math/covariance.c +++ b/src/math/covariance.c @@ -153,7 +153,7 @@ covariance_accumulate (struct covariance *cov, const struct ccase *c) idx = cm_idx (cov, i, j); if (idx >= 0) { - cov->cm [idx] += val1->f * val2->f; + cov->cm [idx] += val1->f * val2->f * weight; } for (m = 0 ; m < n_MOMENTS; ++m) diff --git a/tests/command/correlation.sh b/tests/command/correlation.sh index bdc74939..d8fcb1cb 100755 --- a/tests/command/correlation.sh +++ b/tests/command/correlation.sh @@ -57,7 +57,7 @@ mkdir -p $TEMPDIR cd $TEMPDIR -activity="create program" +activity="create program 1" cat << EOF > $TESTFILE set format = F11.3. data list notable list /foo * bar * wiz * bang *. @@ -93,13 +93,12 @@ EOF if [ $? -ne 0 ] ; then no_result ; fi -activity="run program" +activity="run program 1" $SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE if [ $? -ne 0 ] ; then no_result ; fi -activity="compare results" +activity="compare results 1" perl -pi -e 's/^\s*$//g' $TEMPDIR/pspp.list -cp $TEMPDIR/pspp.list /tmp diff -b $TEMPDIR/pspp.list - << EOF 1.1 CORRELATIONS. Correlations #========================#=====#=====#=====#=====# @@ -150,4 +149,73 @@ diff -b $TEMPDIR/pspp.list - << EOF EOF if [ $? -ne 0 ] ; then fail ; fi + +# Now test that weights are properly handled. + +activity="create program 2" +cat << EOF > $TESTFILE +set format = F11.3. +data list notable list /foo * bar * wiz * bang * w *. +begin data. +1 0 3 1 1 +3 9 -50 5 2 +3 4 3 203 1 +4 -9 0 -4 1 +98 78 104 2 3 +3 50 -49 200 1 +end data. + +weight by w. + +correlations + variables = foo bar wiz bang + /statistics=descriptives xprod + . + +EOF +if [ $? -ne 0 ] ; then no_result ; fi + + +activity="run program 2" +$SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE +if [ $? -ne 0 ] ; then no_result ; fi + +activity="copy results" +cp $TEMPDIR/pspp.list $TEMPDIR/weighted +if [ $? -ne 0 ] ; then no_result ; fi + +activity="create program 3" +cat << EOF > $TESTFILE +set format = F11.3. +data list notable list /foo * bar * wiz * bang * w *. +begin data. +1 0 3 1 1 +3 9 -50 5 1 +3 9 -50 5 1 +3 4 3 203 1 +4 -9 0 -4 1 +98 78 104 2 1 +98 78 104 2 1 +98 78 104 2 1 +3 50 -49 200 1 +end data. + +weight by w. + +correlations + variables = foo bar wiz bang + /statistics=descriptives xprod + . + +EOF +if [ $? -ne 0 ] ; then no_result ; fi + +activity="run program 3" +$SUPERVISOR $PSPP --testing-mode -o raw-ascii $TESTFILE +if [ $? -ne 0 ] ; then no_result ; fi + +activity="Compare weighted and unweighted results" +diff $TEMPDIR/pspp.list $TEMPDIR/weighted +if [ $? -ne 0 ] ; then fail ; fi + pass; -- 2.30.2