Hello, I would do the following:
double r2 = 1 - chisq / gsl_stats_tss(y->data, y->stride, y->size); The function is described in the manual, section 21 (Statistics) subsection 1 (Mean, Standard Deviation and Variance): https://www.gnu.org/software/gsl/manual/html_node/Mean-and-standard-deviation-and-variance.html#Mean-and-standard-deviation-and-variance Best, Tim 2016-01-21 18:00 GMT+01:00 <[email protected]>: > Send Help-gsl mailing list submissions to > [email protected] > > To subscribe or unsubscribe via the World Wide Web, visit > https://lists.gnu.org/mailman/listinfo/help-gsl > or, via email, send a message with subject or body 'help' to > [email protected] > > You can reach the person managing the list at > [email protected] > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Help-gsl digest..." > > > Today's Topics: > > 1. how to use gsl_stats_tss after gsl_multifit_linear to get R^2 > (jean-michel richer) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Thu, 21 Jan 2016 17:13:16 +0100 > From: jean-michel richer <[email protected]> > To: <[email protected]> > Subject: [Help-gsl] how to use gsl_stats_tss after gsl_multifit_linear > to get R^2 > Message-ID: <[email protected]> > Content-Type: text/plain; charset=UTF-8; format=flowed > > > Hi, > I am using "gsl_multifit_linear" to perform multiple linear > regression and it works fine, but I would like to get the > R^2 value. In the documentation it is said that it can be > obtained from : > > "R^2 = 1 - \chi^2 / TSS, where the total sum of squares > (TSS) of the observations may be computed from gsl_stats_tss" > > However I don't know how to use "gsl_stats_tss", I didn't find > some example on internet ! > This function has the following prototype : > > double gsl_stats_tss (const double data[], size_t stride, size_t n) > > but what is data, stride and n ? > > The source code I am using is as follows: > =================================================== > > int N = v.size(); > int P = 10; > > gsl_vector *y; // observed data > gsl_matrix *X; // data used to predict : cste + ... > gsl_vector *c; // the coefficients c0, c1, ... > gsl_matrix *cov; > > // allocate space for the matrices and vectors > X = gsl_matrix_alloc(N, P); // this is an input > y = gsl_vector_alloc(N); //this is an input > > c = gsl_vector_alloc(P); //this is an output > cov = gsl_matrix_alloc(P, P); //this is an output > > // fill the matrices X and y > u32 i = 0; > for (auto z : v) { > gsl_matrix_set(X, i, 0, static_cast<f64>(1)); // because > cste > gsl_matrix_set(X, i, 1, > static_cast<f64>(z->get(Instance::K))); > gsl_matrix_set(X, i, 2, > static_cast<f64>(z->get(Instance::L))); > gsl_matrix_set(X, i, 3, > static_cast<f64>(z->get(Instance::R))); > gsl_matrix_set(X, i, 4, > static_cast<f64>(z->get(Instance::N))); > gsl_matrix_set(X, i, 5, > static_cast<f64>(z->get(Instance::G))); > gsl_matrix_set(X, i, 6, > static_cast<f64>(z->get(Instance::S))); > gsl_matrix_set(X, i, 7, > static_cast<f64>(z->get(Instance::RN))); > gsl_matrix_set(X, i, 8, > static_cast<f64>(z->get(Instance::RG))); > gsl_matrix_set(X, i, 9, > static_cast<f64>(z->get(Instance::RB))); > // expected value > gsl_vector_set(y, i, > static_cast<f64>(z->get(Instance::B))); > ++i; > } > > // allocate temporary work space for gsl > gsl_multifit_linear_workspace *work; > work = gsl_multifit_linear_alloc(N, P); > > // now do the fit > gsl_multifit_linear(X, y, c, cov, &chisq, work); > > =================================================== > So after this how can I compute R^2 ? > Thanks for your help in advance. > JM > > > > ------------------------------ > > _______________________________________________ > Help-gsl mailing list > [email protected] > https://lists.gnu.org/mailman/listinfo/help-gsl > > > End of Help-gsl Digest, Vol 146, Issue 1 > **************************************** >
