Added comments to exported model
[pspp-builds.git] / src / regression.q
index b672897435040fb730b6ac92aa3b32044bca36af..28289622aaded41975b27df35eda4830b4d8c2bf 100644 (file)
@@ -485,14 +485,22 @@ subcommand_export (int export, pspp_linreg_cache *c)
       assert (fp != NULL);
       fp = fopen (handle_get_filename (model_file), "w");
       fprintf (fp, "#include <string.h>\n\n");
-      fprintf (fp, "double\npspp_reg_estimate (const double *var_vals, const char *[] var_names)\n{\n\tchar *model_depvars[%d] = {", c->n_indeps);
+      fprintf (fp, "/*\n   Estimate the mean of Y, the dependent variable for\n");
+      fprintf (fp, "   the linear model of the form \n\n");
+      fprintf (fp, "      Y = b0 + b1 * X1 + b2 * X2 + ... + bk * X2 + error\n\n");
+      fprintf (fp, "   where X1, ..., Xk are the independent variables\n");
+      fprintf (fp, "   whose values are stored in var_vals and whose names, \n");
+      fprintf (fp, "   as known by PSPP, are stored in var_names. The estimated \n");
+      fprintf (fp, "   regression coefficients (i.e., the estimates of b0,...,bk) \n");
+      fprintf (fp, "   are stored in model_coeffs.\n*/\n");
+      fprintf (fp, "double\npspp_reg_estimate (const double *var_vals, const char *var_names[])\n{\n\tchar *model_depvars[%d] = {", c->n_indeps);
       for (i = 1; i < c->n_indeps; i++)
        {
          coeff = c->coeff[i];
-         fprintf (fp, "%s,\n\t\t", coeff.v->name);
+         fprintf (fp, "\"%s\",\n\t\t", coeff.v->name);
        }
       coeff = c->coeff[i];
-      fprintf (fp, "%s};\n\t", coeff.v->name);
+      fprintf (fp, "\"%s\"};\n\t", coeff.v->name);
       fprintf (fp, "double model_coeffs[%d] = {", c->n_indeps);
       for (i = 1; i < c->n_indeps; i++)
        {
@@ -502,11 +510,11 @@ subcommand_export (int export, pspp_linreg_cache *c)
       coeff = c->coeff[i];
       fprintf (fp, "%.15e};\n\t", coeff.estimate);
       coeff = c->coeff[0];
-      fprintf (fp, "double estimate = %.15e\n\t", coeff.estimate);
+      fprintf (fp, "double estimate = %.15e;\n\t", coeff.estimate);
       fprintf (fp, "int i;\n\tint j;\n\n\t");
       fprintf (fp, "for (i = 0; i < %d; i++)\n\t", c->n_indeps);
       fprintf (fp, "{\n\t\tfor (j = 0; j < %d; j++)\n\t\t", c->n_indeps);
-      fprintf (fp, "{\n\t\t\tif (strcmp (var_names[i], model_names[j]) == 0)\n");
+      fprintf (fp, "{\n\t\t\tif (strcmp (var_names[i], model_depvars[j]) == 0)\n");
       fprintf (fp, "\t\t\t{\n\t\t\t\testimate += var_vals[i] * model_coeffs[j];\n");
       fprintf (fp, "\t\t\t}\n\t\t}\n\t}\n\treturn estimate;\n}\n");
       fclose (fp);