X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fstats%2Fregression.q;h=2c259d0f904c1834c7d7e1614fe4f47084bab5ee;hb=2cf38ce51a9f34961d68a75e0b312a591b5c9abf;hp=1d31d1845e02e20064610bf3886cd8d11c71f5a0;hpb=73f67789df91a09ee91976434fb15c2ee1fb5e78;p=pspp-builds.git diff --git a/src/language/stats/regression.q b/src/language/stats/regression.q index 1d31d184..2c259d0f 100644 --- a/src/language/stats/regression.q +++ b/src/language/stats/regression.q @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2005 Free Software Foundation, Inc. + Copyright (C) 2005, 2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -148,9 +148,9 @@ reg_stats_r (pspp_linreg_cache * c) assert (c != NULL); rsq = c->ssm / c->sst; adjrsq = 1.0 - (1.0 - rsq) * (c->n_obs - 1.0) / (c->n_obs - c->n_indeps); - std_error = sqrt ((c->n_indeps - 1.0) / (c->n_obs - 1.0)); + std_error = sqrt (pspp_linreg_mse (c)); t = tab_create (n_cols, n_rows, 0); - tab_dim (t, tab_natural_dimensions); + tab_dim (t, tab_natural_dimensions, NULL); tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1); tab_hline (t, TAL_2, 0, n_cols - 1, 1); tab_vline (t, TAL_2, 2, 0, n_rows - 1); @@ -160,10 +160,10 @@ reg_stats_r (pspp_linreg_cache * c) tab_text (t, 2, 0, TAB_CENTER | TAT_TITLE, _("R Square")); tab_text (t, 3, 0, TAB_CENTER | TAT_TITLE, _("Adjusted R Square")); tab_text (t, 4, 0, TAB_CENTER | TAT_TITLE, _("Std. Error of the Estimate")); - tab_float (t, 1, 1, TAB_RIGHT, sqrt (rsq), 10, 2); - tab_float (t, 2, 1, TAB_RIGHT, rsq, 10, 2); - tab_float (t, 3, 1, TAB_RIGHT, adjrsq, 10, 2); - tab_float (t, 4, 1, TAB_RIGHT, std_error, 10, 2); + tab_double (t, 1, 1, TAB_RIGHT, sqrt (rsq), NULL); + tab_double (t, 2, 1, TAB_RIGHT, rsq, NULL); + tab_double (t, 3, 1, TAB_RIGHT, adjrsq, NULL); + tab_double (t, 4, 1, TAB_RIGHT, std_error, NULL); tab_title (t, _("Model Summary")); tab_submit (t); } @@ -193,7 +193,7 @@ reg_stats_coeff (pspp_linreg_cache * c) t = tab_create (n_cols, n_rows, 0); tab_headers (t, 2, 0, 1, 0); - tab_dim (t, tab_natural_dimensions); + tab_dim (t, tab_natural_dimensions, NULL); tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1); tab_hline (t, TAL_2, 0, n_cols - 1, 1); tab_vline (t, TAL_2, 2, 0, n_rows - 1); @@ -205,14 +205,14 @@ reg_stats_coeff (pspp_linreg_cache * c) tab_text (t, 5, 0, TAB_CENTER | TAT_TITLE, _("t")); tab_text (t, 6, 0, TAB_CENTER | TAT_TITLE, _("Significance")); tab_text (t, 1, 1, TAB_LEFT | TAT_TITLE, _("(Constant)")); - tab_float (t, 2, 1, 0, c->intercept, 10, 2); + tab_double (t, 2, 1, 0, c->intercept, NULL); std_err = sqrt (gsl_matrix_get (c->cov, 0, 0)); - tab_float (t, 3, 1, 0, std_err, 10, 2); - tab_float (t, 4, 1, 0, 0.0, 10, 2); + tab_double (t, 3, 1, 0, std_err, NULL); + tab_double (t, 4, 1, 0, 0.0, NULL); t_stat = c->intercept / std_err; - tab_float (t, 5, 1, 0, t_stat, 10, 2); + tab_double (t, 5, 1, 0, t_stat, NULL); pval = 2 * gsl_cdf_tdist_Q (fabs (t_stat), 1.0); - tab_float (t, 6, 1, 0, pval, 10, 2); + tab_double (t, 6, 1, 0, pval, NULL); for (j = 0; j < c->n_coeffs; j++) { struct string tstr; @@ -240,32 +240,32 @@ reg_stats_coeff (pspp_linreg_cache * c) /* Regression coefficients. */ - tab_float (t, 2, this_row, 0, c->coeff[j]->estimate, 10, 2); + tab_double (t, 2, this_row, 0, c->coeff[j]->estimate, NULL); /* Standard error of the coefficients. */ std_err = sqrt (gsl_matrix_get (c->cov, j + 1, j + 1)); - tab_float (t, 3, this_row, 0, std_err, 10, 2); + tab_double (t, 3, this_row, 0, std_err, NULL); /* Standardized coefficient, i.e., regression coefficient if all variables had unit variance. */ beta = pspp_coeff_get_sd (c->coeff[j]); beta *= c->coeff[j]->estimate / c->depvar_std; - tab_float (t, 4, this_row, 0, beta, 10, 2); + tab_double (t, 4, this_row, 0, beta, NULL); /* Test statistic for H0: coefficient is 0. */ t_stat = c->coeff[j]->estimate / std_err; - tab_float (t, 5, this_row, 0, t_stat, 10, 2); + tab_double (t, 5, this_row, 0, t_stat, NULL); /* P values for the test statistic above. */ pval = 2 * gsl_cdf_tdist_Q (fabs (t_stat), (double) (c->n_obs - c->n_coeffs)); - tab_float (t, 6, this_row, 0, pval, 10, 2); + tab_double (t, 6, this_row, 0, pval, NULL); ds_destroy (&tstr); } tab_title (t, _("Coefficients")); @@ -281,7 +281,7 @@ reg_stats_anova (pspp_linreg_cache * c) int n_cols = 7; int n_rows = 4; const double msm = c->ssm / c->dfm; - const double mse = c->sse / c->dfe; + const double mse = pspp_linreg_mse (c); const double F = msm / mse; const double pval = gsl_cdf_fdist_Q (F, c->dfm, c->dfe); @@ -290,7 +290,7 @@ reg_stats_anova (pspp_linreg_cache * c) assert (c != NULL); t = tab_create (n_cols, n_rows, 0); tab_headers (t, 2, 0, 1, 0); - tab_dim (t, tab_natural_dimensions); + tab_dim (t, tab_natural_dimensions, NULL); tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1); @@ -309,23 +309,23 @@ reg_stats_anova (pspp_linreg_cache * c) tab_text (t, 1, 3, TAB_LEFT | TAT_TITLE, _("Total")); /* Sums of Squares */ - tab_float (t, 2, 1, 0, c->ssm, 10, 2); - tab_float (t, 2, 3, 0, c->sst, 10, 2); - tab_float (t, 2, 2, 0, c->sse, 10, 2); + tab_double (t, 2, 1, 0, c->ssm, NULL); + tab_double (t, 2, 3, 0, c->sst, NULL); + tab_double (t, 2, 2, 0, c->sse, NULL); /* Degrees of freedom */ - tab_text (t, 3, 1, TAB_RIGHT | TAT_PRINTF, "%g", c->dfm); - tab_text (t, 3, 2, TAB_RIGHT | TAT_PRINTF, "%g", c->dfe); - tab_text (t, 3, 3, TAB_RIGHT | TAT_PRINTF, "%g", c->dft); + tab_text_format (t, 3, 1, TAB_RIGHT, "%g", c->dfm); + tab_text_format (t, 3, 2, TAB_RIGHT, "%g", c->dfe); + tab_text_format (t, 3, 3, TAB_RIGHT, "%g", c->dft); /* Mean Squares */ - tab_float (t, 4, 1, TAB_RIGHT, msm, 8, 3); - tab_float (t, 4, 2, TAB_RIGHT, mse, 8, 3); + tab_double (t, 4, 1, TAB_RIGHT, msm, NULL); + tab_double (t, 4, 2, TAB_RIGHT, mse, NULL); - tab_float (t, 5, 1, 0, F, 8, 3); + tab_double (t, 5, 1, 0, F, NULL); - tab_float (t, 6, 1, 0, pval, 8, 3); + tab_double (t, 6, 1, 0, pval, NULL); tab_title (t, _("ANOVA")); tab_submit (t); @@ -381,7 +381,7 @@ reg_stats_bcov (pspp_linreg_cache * c) n_rows = 2 * (c->n_indeps + 1); t = tab_create (n_cols, n_rows, 0); tab_headers (t, 2, 0, 1, 0); - tab_dim (t, tab_natural_dimensions); + tab_dim (t, tab_natural_dimensions, NULL); tab_box (t, TAL_2, TAL_2, -1, TAL_1, 0, 0, n_cols - 1, n_rows - 1); tab_hline (t, TAL_2, 0, n_cols - 1, 1); tab_vline (t, TAL_2, 2, 0, n_rows - 1); @@ -398,8 +398,8 @@ reg_stats_bcov (pspp_linreg_cache * c) { col = (i <= k) ? k : i; row = (i <= k) ? i : k; - tab_float (t, k + 2, i, TAB_CENTER, - gsl_matrix_get (c->cov, row, col), 8, 3); + tab_double (t, k + 2, i, TAB_CENTER, + gsl_matrix_get (c->cov, row, col), NULL); } } tab_title (t, _("Coefficient Correlations")); @@ -542,7 +542,7 @@ regression_trns_free (void *t_) Gets the predicted values. */ static int -regression_trns_pred_proc (void *t_, struct ccase *c, +regression_trns_pred_proc (void *t_, struct ccase **c, casenumber case_idx UNUSED) { size_t i; @@ -563,12 +563,12 @@ regression_trns_pred_proc (void *t_, struct ccase *c, n_vals = (*model->get_vars) (model, vars); vals = xnmalloc (n_vals, sizeof (*vals)); - output = case_data_rw (c, model->pred); - assert (output != NULL); + *c = case_unshare (*c); + output = case_data_rw (*c, model->pred); for (i = 0; i < n_vals; i++) { - vals[i] = case_data (c, vars[i]); + vals[i] = case_data (*c, vars[i]); } output->f = (*model->predict) ((const struct variable **) vars, vals, model, n_vals); @@ -581,7 +581,7 @@ regression_trns_pred_proc (void *t_, struct ccase *c, Gets the residuals. */ static int -regression_trns_resid_proc (void *t_, struct ccase *c, +regression_trns_resid_proc (void *t_, struct ccase **c, casenumber case_idx UNUSED) { size_t i; @@ -603,14 +603,15 @@ regression_trns_resid_proc (void *t_, struct ccase *c, n_vals = (*model->get_vars) (model, vars); vals = xnmalloc (n_vals, sizeof (*vals)); - output = case_data_rw (c, model->resid); + *c = case_unshare (*c); + output = case_data_rw (*c, model->resid); assert (output != NULL); for (i = 0; i < n_vals; i++) { - vals[i] = case_data (c, vars[i]); + vals[i] = case_data (*c, vars[i]); } - obs = case_data (c, model->depvar); + obs = case_data (*c, model->depvar); output->f = (*model->residual) ((const struct variable **) vars, vals, obs, model, n_vals); free (vals); @@ -688,17 +689,21 @@ subcommand_save (struct dataset *ds, int save, pspp_linreg_cache ** models) for (lc = models; lc < models + cmd.n_dependent; lc++) { - assert (*lc != NULL); - assert ((*lc)->depvar != NULL); - if (cmd.a_save[REGRESSION_SV_RESID]) - { - reg_save_var (ds, "RES", regression_trns_resid_proc, *lc, - &(*lc)->resid, n_trns); - } - if (cmd.a_save[REGRESSION_SV_PRED]) + if (*lc != NULL) { - reg_save_var (ds, "PRED", regression_trns_pred_proc, *lc, - &(*lc)->pred, n_trns); + if ((*lc)->depvar != NULL) + { + if (cmd.a_save[REGRESSION_SV_RESID]) + { + reg_save_var (ds, "RES", regression_trns_resid_proc, *lc, + &(*lc)->resid, n_trns); + } + if (cmd.a_save[REGRESSION_SV_PRED]) + { + reg_save_var (ds, "PRED", regression_trns_pred_proc, *lc, + &(*lc)->pred, n_trns); + } + } } } } @@ -821,7 +826,7 @@ prepare_categories (struct casereader *input, struct moments_var *mom) { int n_data; - struct ccase c; + struct ccase *c; size_t i; assert (vars != NULL); @@ -832,7 +837,7 @@ prepare_categories (struct casereader *input, cat_stored_values_create (vars[i]); n_data = 0; - for (; casereader_read (input, &c); case_destroy (&c)) + for (; (c = casereader_read (input)) != NULL; case_unref (c)) { /* The second condition ensures the program will run even if @@ -841,7 +846,7 @@ prepare_categories (struct casereader *input, */ for (i = 0; i < n_vars; i++) { - const union value *val = case_data (&c, vars[i]); + const union value *val = case_data (c, vars[i]); if (var_is_alpha (vars[i])) cat_value_update (vars[i], val); else @@ -861,39 +866,6 @@ coeff_init (pspp_linreg_cache * c, struct design_matrix *dm) pspp_coeff_init (c->coeff, dm); } -/* - Put the moments in the linreg cache. - */ -static void -compute_moments (pspp_linreg_cache * c, struct moments_var *mom, - struct design_matrix *dm, size_t n) -{ - size_t i; - size_t j; - double weight; - double mean; - double variance; - double skewness; - double kurtosis; - /* - Scan the variable names in the columns of the design matrix. - When we find the variable we need, insert its mean in the cache. - */ - for (i = 0; i < dm->m->size2; i++) - { - for (j = 0; j < n; j++) - { - if (design_matrix_col_to_var (dm, i) == (mom + j)->v) - { - moments1_calculate ((mom + j)->m, &weight, &mean, &variance, - &skewness, &kurtosis); - pspp_linreg_set_indep_variable_mean (c, (mom + j)->v, mean); - pspp_linreg_set_indep_variable_sd (c, (mom + j)->v, sqrt (variance)); - } - } - } -} - static bool run_regression (struct casereader *input, struct cmd_regression *cmd, struct dataset *ds, pspp_linreg_cache **models) @@ -901,7 +873,7 @@ run_regression (struct casereader *input, struct cmd_regression *cmd, size_t i; int n_indep = 0; int k; - struct ccase c; + struct ccase *c; const struct variable **indep_vars; struct design_matrix *X; struct moments_var *mom; @@ -911,13 +883,14 @@ run_regression (struct casereader *input, struct cmd_regression *cmd, assert (models != NULL); - if (!casereader_peek (input, 0, &c)) + c = casereader_peek (input, 0); + if (c == NULL) { casereader_destroy (input); return true; } - output_split_file_values (ds, &c); - case_destroy (&c); + output_split_file_values (ds, c); + case_unref (c); if (!v_variables) { @@ -949,16 +922,16 @@ run_regression (struct casereader *input, struct cmd_regression *cmd, const struct variable *dep_var; struct casereader *reader; casenumber row; - struct ccase c; + struct ccase *c; size_t n_data; /* Number of valid cases. */ dep_var = cmd->v_dependent[k]; n_indep = identify_indep_vars (indep_vars, dep_var); reader = casereader_clone (input); reader = casereader_create_filter_missing (reader, indep_vars, n_indep, - MV_ANY, NULL); + MV_ANY, NULL, NULL); reader = casereader_create_filter_missing (reader, &dep_var, 1, - MV_ANY, NULL); + MV_ANY, NULL, NULL); n_data = prepare_categories (casereader_clone (reader), indep_vars, n_indep, mom); @@ -973,7 +946,8 @@ run_regression (struct casereader *input, struct cmd_regression *cmd, { lopts.get_indep_mean_std[i] = 1; } - models[k] = pspp_linreg_cache_alloc (X->m->size1, X->m->size2); + models[k] = pspp_linreg_cache_alloc (dep_var, (const struct variable **) indep_vars, + X->m->size1, n_indep); models[k]->depvar = dep_var; /* For large data sets, use QR decomposition. @@ -987,18 +961,18 @@ run_regression (struct casereader *input, struct cmd_regression *cmd, The second pass fills the design matrix. */ reader = casereader_create_counter (reader, &row, -1); - for (; casereader_read (reader, &c); case_destroy (&c)) + for (; (c = casereader_read (reader)) != NULL; case_unref (c)) { for (i = 0; i < n_indep; ++i) { const struct variable *v = indep_vars[i]; - const union value *val = case_data (&c, v); + const union value *val = case_data (c, v); if (var_is_alpha (v)) design_matrix_set_categorical (X, row, v, val); else design_matrix_set_numeric (X, row, v, val); } - gsl_vector_set (Y, row, case_num (&c, dep_var)); + gsl_vector_set (Y, row, case_num (c, dep_var)); } /* Now that we know the number of coefficients, allocate space