26 #ifndef SHORE_STATISTICS_GLM_HPP__
27 #define SHORE_STATISTICS_GLM_HPP__
33 #include <gsl/gsl_multifit.h>
34 #include <gsl/gsl_cdf.h>
35 #include <gsl/gsl_blas.h>
36 #include <gsl/gsl_linalg.h>
73 void set_modelmatrix(
size_t i,
size_t j,
double value);
76 size_t modelmatrix_rank(
const double eps=1e-7);
78 void set_observation(
size_t i,
double x);
82 double get_coefficient(
size_t i)
const;
84 void calc_residuals();
85 double get_residual(
size_t i)
const;
87 double get_vcov(
size_t i,
size_t j)
const;
91 template<
class T,
size_t rows,
size_t columns>
92 glm(
const T (&design)[rows][columns])
98 for(
size_t i=0;i<m_rows;++i)
100 for(
size_t j=0;j<m_cols;++j)
102 set_modelmatrix(i,j,design[i][j]);
106 m_rank=modelmatrix_rank(1e-7);
108 std::cerr<<
"GLM: columns="<<m_cols
109 <<
", rank="<<m_rank<<
", res. df="<<get_residual_df()<<std::endl;
112 glm(
const std::vector<std::vector<double> >& design);
119 size_t get_residual_df()
const;
122 size_t nrows()
const;
125 size_t ncols()
const;
134 for(
size_t i=0;i<m_rows;++i)
135 set_observation(i,*obs++);
144 for(
size_t i=0;i<m_cols;++i)
145 (*dest++)=get_coefficient(i);
154 for(
size_t i=0;i<m_rows;++i)
155 (*dest++)=get_residual(i);
161 template<
class Twodim>
164 for(
size_t i=0;i<m_cols;++i)
166 for(
size_t j=0;j<m_cols;++j)
168 res[i][j]=get_vcov(i,j);
203 void mmprod(
void *
const A,
void *
const B,
void *
const C);
206 void tcrossprod(
void *
const A,
void *
const B,
void *
const C);
209 void mvprod(
void *
const A,
void *
const x,
void *
const y);
211 void set_lhmatrix(
size_t i,
size_t j,
double x);
213 double get_estimate(
size_t i)
const;
224 template<
class T,
size_t rows,
size_t columns>
225 glht(
const glm*
const m,
const T (&lh)[rows][columns])
227 m_rows(rows),m_cols(columns),
228 m_alternative(ALT_TWOSIDED)
230 if(m_glm.
ncols()!=m_cols)
231 throw "column counts of glm and glht must match";
235 for(
size_t i=0;i<m_rows;++i)
237 for(
size_t j=0;j<m_cols;++j)
239 set_lhmatrix(i,j,lh[i][j]);
244 glht(
const glm*
const m,
const std::vector<std::vector<double> >& lh);
248 size_t nrows()
const;
250 size_t ncols()
const;
252 void set_alternative(
const Alternative alt);
258 void get_estimates(Iter dest)
260 for(
size_t i=0;i<m_rows;++i)
261 (*dest++)=get_estimate(i);
265 void get_stderrs(Iter dest)
267 for(
size_t i=0;i<m_rows;++i)
268 (*dest++)=m_stderr[i];
272 void get_tstats(Iter dest)
274 for(
size_t i=0;i<m_rows;++i)
275 (*dest++)=m_tstats[i];
279 void get_pvalues(Iter dest)
281 for(
size_t i=0;i<m_rows;++i)
282 (*dest++)=m_pvalues[i];
288 #endif // SHORE_STATISTICS_GLM_HPP__