X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fstats%2Fglm.c;h=e4b3c17b21bb66ec4f46a30a52114668059cd90b;hb=68ae62595cb0507622afda5568a3a20e28857f88;hp=92e35ccd27e1f5f4c1f8082d63d402e90f05ca4d;hpb=72d873a1af4914c2bfe1cdda1cb0108da243f534;p=pspp diff --git a/src/language/stats/glm.c b/src/language/stats/glm.c index 92e35ccd27..e4b3c17b21 100644 --- a/src/language/stats/glm.c +++ b/src/language/stats/glm.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2010, 2011 Free Software Foundation, Inc. + Copyright (C) 2010, 2011, 2012 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 @@ -267,7 +267,7 @@ cmd_glm (struct lexer *lexer, struct dataset *ds) } glm.ss_type = lex_integer (lexer); - if (1 > glm.ss_type && 3 < glm.ss_type ) + if (1 > glm.ss_type || 3 < glm.ss_type ) { msg (ME, _("Only types 1, 2 & 3 sums of squares are currently implemented")); goto error; @@ -326,6 +326,7 @@ cmd_glm (struct lexer *lexer, struct dataset *ds) free (glm.factor_vars); for (i = 0 ; i < glm.n_interactions; ++i) interaction_destroy (glm.interactions[i]); + free (glm.interactions); free (glm.dep_vars); @@ -386,7 +387,7 @@ fill_submatrix (const gsl_matrix * cov, gsl_matrix * submatrix, bool *dropped_f) static void ssq_type1 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd) { - gsl_matrix *cm = covariance_calculate_unnormalized (cov); + const gsl_matrix *cm = covariance_calculate_unnormalized (cov); size_t i; size_t k; bool *model_dropped = xcalloc (covariance_dim (cov), sizeof (*model_dropped)); @@ -446,7 +447,6 @@ ssq_type1 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd) free (model_dropped); free (submodel_dropped); - gsl_matrix_free (cm); } /* @@ -456,7 +456,7 @@ ssq_type1 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd) static void ssq_type2 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd) { - gsl_matrix *cm = covariance_calculate_unnormalized (cov); + const gsl_matrix *cm = covariance_calculate_unnormalized (cov); size_t i; size_t k; bool *model_dropped = xcalloc (covariance_dim (cov), sizeof (*model_dropped)); @@ -510,7 +510,6 @@ ssq_type2 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd) free (model_dropped); free (submodel_dropped); - gsl_matrix_free (cm); } /* @@ -520,7 +519,7 @@ ssq_type2 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd) static void ssq_type3 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd) { - gsl_matrix *cm = covariance_calculate_unnormalized (cov); + const gsl_matrix *cm = covariance_calculate_unnormalized (cov); size_t i; size_t k; bool *model_dropped = xcalloc (covariance_dim (cov), sizeof (*model_dropped)); @@ -567,8 +566,6 @@ ssq_type3 (struct covariance *cov, gsl_vector *ssq, const struct glm_spec *cmd) gsl_matrix_free (model_cov); } free (model_dropped); - - gsl_matrix_free (cm); } @@ -590,8 +587,7 @@ run_glm (struct glm_spec *cmd, struct casereader *input, struct covariance *cov; ws.cats = categoricals_create (cmd->interactions, cmd->n_interactions, - cmd->wv, cmd->exclude, - NULL, NULL, NULL, NULL); + cmd->wv, cmd->exclude, MV_ANY); cov = covariance_2pass_create (cmd->n_dep_vars, cmd->dep_vars, ws.cats, cmd->wv, cmd->exclude); @@ -657,7 +653,9 @@ run_glm (struct glm_spec *cmd, struct casereader *input, } { - gsl_matrix *cm = covariance_calculate_unnormalized (cov); + const gsl_matrix *ucm = covariance_calculate_unnormalized (cov); + gsl_matrix *cm = gsl_matrix_alloc (ucm->size1, ucm->size2); + gsl_matrix_memcpy (cm, ucm); // dump_matrix (cm); @@ -686,7 +684,6 @@ run_glm (struct glm_spec *cmd, struct casereader *input, break; } // dump_matrix (cm); - gsl_matrix_free (cm); } @@ -716,6 +713,8 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) const struct fmt_spec *wfmt = cmd->wv ? var_get_print_format (cmd->wv) : &F_8_0; + double intercept_ssq; + double ssq_effects; double n_total, mean; double df_corr = 1.0; double mse = 0; @@ -733,6 +732,7 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) msg (MW, "GLM is experimental. Do not rely on these results."); t = tab_create (nc, nr); + tab_set_format (t, RC_WEIGHT, wfmt); tab_title (t, _("Tests of Between-Subjects Effects")); tab_headers (t, heading_columns, 0, heading_rows, 0); @@ -767,20 +767,20 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) mse = gsl_vector_get (ws->ssq, 0) / (n_total - df_corr); - const double intercept_ssq = pow2 (mean * n_total) / n_total; + intercept_ssq = pow2 (mean * n_total) / n_total; - double ssq_effects = 0.0; + ssq_effects = 0.0; if (cmd->intercept) { const double df = 1.0; const double F = intercept_ssq / df / mse; tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Intercept")); - tab_double (t, 1, r, 0, intercept_ssq, NULL); - tab_double (t, 2, r, 0, 1.00, wfmt); - tab_double (t, 3, r, 0, intercept_ssq / df, NULL); - tab_double (t, 4, r, 0, F, NULL); + tab_double (t, 1, r, 0, intercept_ssq, NULL, RC_OTHER); + tab_double (t, 2, r, 0, 1.00, NULL, RC_WEIGHT); + tab_double (t, 3, r, 0, intercept_ssq / df, NULL, RC_OTHER); + tab_double (t, 4, r, 0, F, NULL, RC_OTHER); tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr), - NULL); + NULL, RC_PVALUE); r++; } @@ -790,6 +790,8 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) double df = categoricals_df (ws->cats, f); double ssq = gsl_vector_get (ws->ssq, f + 1); + double F; + ssq_effects += ssq; if (! cmd->intercept) @@ -798,18 +800,18 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) ssq += intercept_ssq; } - const double F = ssq / df / mse; + F = ssq / df / mse; interaction_to_string (cmd->interactions[f], &str); tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, ds_cstr (&str)); ds_destroy (&str); - tab_double (t, 1, r, 0, ssq, NULL); - tab_double (t, 2, r, 0, df, wfmt); - tab_double (t, 3, r, 0, ssq / df, NULL); - tab_double (t, 4, r, 0, F, NULL); + tab_double (t, 1, r, 0, ssq, NULL, RC_OTHER); + tab_double (t, 2, r, 0, df, NULL, RC_WEIGHT); + tab_double (t, 3, r, 0, ssq / df, NULL, RC_OTHER); + tab_double (t, 4, r, 0, F, NULL, RC_OTHER); tab_double (t, 5, r, 0, gsl_cdf_fdist_Q (F, df, n_total - df_corr), - NULL); + NULL, RC_PVALUE); r++; } @@ -817,19 +819,21 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) /* Model / Corrected Model */ double df = df_corr; double ssq = ws->total_ssq - gsl_vector_get (ws->ssq, 0); + double F; + if ( cmd->intercept ) df --; else ssq += intercept_ssq; - const double F = ssq / df / mse; - tab_double (t, 1, heading_rows, 0, ssq, NULL); - tab_double (t, 2, heading_rows, 0, df, wfmt); - tab_double (t, 3, heading_rows, 0, ssq / df, NULL); - tab_double (t, 4, heading_rows, 0, F, NULL); + F = ssq / df / mse; + tab_double (t, 1, heading_rows, 0, ssq, NULL, RC_OTHER); + tab_double (t, 2, heading_rows, 0, df, NULL, RC_WEIGHT); + tab_double (t, 3, heading_rows, 0, ssq / df, NULL, RC_OTHER); + tab_double (t, 4, heading_rows, 0, F, NULL, RC_OTHER); tab_double (t, 5, heading_rows, 0, - gsl_cdf_fdist_Q (F, df, n_total - df_corr), NULL); + gsl_cdf_fdist_Q (F, df, n_total - df_corr), NULL, RC_PVALUE); } { @@ -837,15 +841,15 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) const double ssq = gsl_vector_get (ws->ssq, 0); const double mse = ssq / df; tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Error")); - tab_double (t, 1, r, 0, ssq, NULL); - tab_double (t, 2, r, 0, df, wfmt); - tab_double (t, 3, r++, 0, mse, NULL); + tab_double (t, 1, r, 0, ssq, NULL, RC_OTHER); + tab_double (t, 2, r, 0, df, NULL, RC_WEIGHT); + tab_double (t, 3, r++, 0, mse, NULL, RC_OTHER); } { tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Total")); - tab_double (t, 1, r, 0, ws->total_ssq + intercept_ssq, NULL); - tab_double (t, 2, r, 0, n_total, wfmt); + tab_double (t, 1, r, 0, ws->total_ssq + intercept_ssq, NULL, RC_OTHER); + tab_double (t, 2, r, 0, n_total, NULL, RC_WEIGHT); r++; } @@ -853,8 +857,8 @@ output_glm (const struct glm_spec *cmd, const struct glm_workspace *ws) if (cmd->intercept) { tab_text (t, 0, r, TAB_LEFT | TAT_TITLE, _("Corrected Total")); - tab_double (t, 1, r, 0, ws->total_ssq, NULL); - tab_double (t, 2, r, 0, n_total - 1.0, wfmt); + tab_double (t, 1, r, 0, ws->total_ssq, NULL, RC_OTHER); + tab_double (t, 2, r, 0, n_total - 1.0, NULL, RC_WEIGHT); } tab_submit (t); @@ -880,71 +884,11 @@ dump_matrix (const gsl_matrix * m) - -/* Match a variable. - If the match succeeds, the variable will be placed in VAR. - Returns true if successful */ -static bool -lex_match_variable (struct lexer *lexer, const struct glm_spec *glm, const struct variable **var) -{ - if (lex_token (lexer) != T_ID) - return false; - - *var = parse_variable_const (lexer, glm->dict); - - if ( *var == NULL) - return false; - return true; -} - -/* An interaction is a variable followed by {*, BY} followed by an interaction */ -static bool -parse_design_interaction (struct lexer *lexer, struct glm_spec *glm, struct interaction **iact) -{ - const struct variable *v = NULL; - assert (iact); - - switch (lex_next_token (lexer, 1)) - { - case T_ENDCMD: - case T_SLASH: - case T_COMMA: - case T_ID: - case T_BY: - case T_ASTERISK: - break; - default: - return false; - break; - } - - if (! lex_match_variable (lexer, glm, &v)) - { - interaction_destroy (*iact); - *iact = NULL; - return false; - } - - assert (v); - - if ( *iact == NULL) - *iact = interaction_create (v); - else - interaction_add_variable (*iact, v); - - if ( lex_match (lexer, T_ASTERISK) || lex_match (lexer, T_BY)) - { - return parse_design_interaction (lexer, glm, iact); - } - - return true; -} - static bool parse_nested_variable (struct lexer *lexer, struct glm_spec *glm) { const struct variable *v = NULL; - if ( ! lex_match_variable (lexer, glm, &v)) + if ( ! lex_match_variable (lexer, glm->dict, &v)) return false; if (lex_match (lexer, T_LPAREN)) @@ -965,7 +909,7 @@ static bool parse_design_term (struct lexer *lexer, struct glm_spec *glm) { struct interaction *iact = NULL; - if (parse_design_interaction (lexer, glm, &iact)) + if (parse_design_interaction (lexer, glm->dict, &iact)) { /* Interaction parsing successful. Add to list of interactions */ glm->interactions = xrealloc (glm->interactions, sizeof *glm->interactions * ++glm->n_interactions);