X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fregression.q;h=b672897435040fb730b6ac92aa3b32044bca36af;hb=b9799cdd10b30ea96d9178b7a0d48504d052228c;hp=02a675f74293cb04d11b9e95cb68b7b61f8472d2;hpb=48dd2c7e82ecd7629109484ed873bcb67ec8c655;p=pspp-builds.git diff --git a/src/regression.q b/src/regression.q index 02a675f7..b6728974 100644 --- a/src/regression.q +++ b/src/regression.q @@ -40,8 +40,9 @@ #include "var.h" #include "vfm.h" -/* (headers) */ +#define REG_LARGE_DATA 1000 +/* (headers) */ /* (specification) "REGRESSION" (regression_): @@ -63,6 +64,7 @@ f, defaults, all; + export=custom; ^dependent=varlist; ^method=enter. */ @@ -75,6 +77,12 @@ static struct cmd_regression cmd; */ size_t *indep_vars; +/* + File where the model will be saved if the EXPORT subcommand + is given. + */ +struct file_handle *model_file; + /* Return value for the procedure. */ @@ -463,6 +471,68 @@ subcommand_statistics (int *keywords, pspp_linreg_cache * c) statistics_keyword_output (reg_stats_tol, keywords[tol], c); statistics_keyword_output (reg_stats_selection, keywords[selection], c); } +static void +subcommand_export (int export, pspp_linreg_cache *c) +{ + FILE *fp; + size_t i; + struct pspp_linreg_coeff coeff; + + if (export) + { + assert (c != NULL); + assert (model_file != NULL); + assert (fp != NULL); + fp = fopen (handle_get_filename (model_file), "w"); + fprintf (fp, "#include \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); + } + coeff = c->coeff[i]; + 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++) + { + coeff = c->coeff[i]; + fprintf (fp, "%.15e,\n\t\t", coeff.estimate); + } + 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, "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, "\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); + } +} +static int +regression_custom_export (struct cmd_regression *cmd) +{ + /* 0 on failure, 1 on success, 2 on failure that should result in syntax error */ + if (!lex_force_match ('(')) + return 0; + + if (lex_match ('*')) + model_file = NULL; + else + { + model_file = fh_parse (); + if (model_file == NULL) + return 0; + } + + if (!lex_force_match (')')) + return 0; + + return 1; +} int cmd_regression (void) @@ -644,11 +714,19 @@ run_regression (const struct casefile *cf, void *cmd_ UNUSED) (const struct variable *) design_matrix_col_to_var (X, i); assert (lcache->coeff[j].v != NULL); } + /* + For large data sets, use QR decomposition. + */ + if (n_data > sqrt (n_indep) && n_data > REG_LARGE_DATA) + { + lcache->method = PSPP_LINREG_SVD; + } /* Find the least-squares estimates and other statistics. */ pspp_linreg ((const gsl_vector *) Y, X->m, &lopts, lcache); subcommand_statistics (cmd.a_statistics, lcache); + subcommand_export (cmd.sbc_export, lcache); gsl_vector_free (Y); design_matrix_destroy (X); pspp_linreg_cache_free (lcache);