Logistic Regression: Fix crash if categorical variable has no distinct values 20121114030503/pspp
authorJohn Darrington <john@darrington.wattle.id.au>
Wed, 14 Nov 2012 10:47:09 +0000 (11:47 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Wed, 14 Nov 2012 10:47:09 +0000 (11:47 +0100)
src/language/stats/logistic.c
tests/language/stats/logistic.at

index 91a16488bd2d548716487425ecfd804b4e2b5754..f85b6ec39ef43e88509d056f261d386a0c23f473 100644 (file)
@@ -560,6 +560,23 @@ run_lr (const struct lr_spec *cmd, struct casereader *input,
   if (NULL == beta_hat)
     return false;
 
+  
+  for (i = 0; i < cmd->n_cat_predictors; ++i)
+    {
+      if (1 >= categoricals_n_count (work.cats, i))
+       {
+         struct string str;
+         ds_init_empty (&str);
+         
+         interaction_to_string (cmd->cat_predictors[i], &str);
+
+         msg (ME, _("Category %s does not have at least two distinct values. Logistic regression will not be run."),
+              ds_cstr(&str));
+         ds_destroy (&str);
+         return false;
+       }
+    }
+
   output_depvarmap (cmd, &work);
 
   case_processing_summary (&work);
index 7db121338b9c2ac5741b3f892ad38af2d4572610..b4d9eef458a2c736ec93c036cbc6baef48149f9f 100644 (file)
@@ -1,4 +1,3 @@
-
 AT_BANNER([LOGISTIC REGRESSION])
 
 dnl These examples are adapted from
@@ -1009,3 +1008,23 @@ Step 1,read,.098,.025,15.199,1,.000,1.103
 ])
 
 AT_CLEANUP
+
+
+dnl Check that it doesn't crash if a categorical variable
+dnl has only one distinct value
+AT_SETUP([LOGISTIC REGRESSION identical categories])
+
+AT_DATA([crash.sps], [dnl
+data list notable list /y x1 x2*.
+begin data
+0 1 1
+1 2 1
+end data.
+
+logistic regression y with x1 x2
+       /categorical = x2.
+])
+
+AT_CHECK([pspp -O format=csv crash.sps], [1], [ignore])
+
+AT_CLEANUP