fastglmm
Massively scalable generalized linear mixed models
Loading...
Searching...
No Matches
glmmFitResponses.h
Go to the documentation of this file.
1#ifndef GLMM_FIT_RESPONSE_H_
2#define GLMM_FIT_RESPONSE_H_
3
4// if -D USE_R, use RcppArmadillo library
5#ifdef USE_R
6// [[Rcpp::depends(RcppParallel)]]
7// [[Rcpp::depends(RcppParallel)]]
8#include <RcppArmadillo.h>
9#include <RcppParallel.h>
10#else
11#include <armadillo>
12#include <tbb/tbb.h>
13#endif
14
15#include "fastglmm_fit.h"
16#include "ModelFit.h"
17
18using namespace arma;
19using namespace std;
20
21namespace fastglmmLib {
22
23// Order of template variables
24// T1 Y
25// T2 X
26// T3 Z
27template <typename T1, typename T2, typename T3>
29
30 public:
32 const T2 &X,
33 const spectralDecomp<T3> &dcmp,
34 const vec &weights = {},
35 const vec &offset = {},
36 const double &left = -10,
37 const double &right = 10,
38 const double &tol = 1e-5,
39 const double &tol_eta = 1e-7,
40 const int &maxit = 100,
41 const double &lambda = 0,
42 const int &nthreads = 1,
43 const ModelDetail md = LOW);
44
46 const T1 &Y,
47 const vector<string> &ids,
48 const vector<string> &family);
49
50 private:
51 T2 X;
53 vec weights, offset;
54 double left, right, tol, tol_eta;
55 int maxit;
56 double lambda;
57 int nthreads;
58 ModelDetail md;
59
60 uvec idx_drop;
61 T2 X_clean;
62};
63
64
65
66// constructor
67template <typename T1, typename T2, typename T3>
69 const T2 &X,
70 const spectralDecomp<T3> &dcmp,
71 const vec &weights,
72 const vec &offset,
73 const double &left,
74 const double &right,
75 const double &tol,
76 const double &tol_eta,
77 const int &maxit,
78 const double &lambda,
79 const int &nthreads,
80 const ModelDetail md):
81 X(X),
82 dcmp(dcmp),
83 weights(weights),
84 offset(offset),
85 left(left),
86 right(right),
87 tol(tol),
88 tol_eta(tol_eta),
89 maxit(maxit),
90 lambda(lambda),
91 nthreads(nthreads),
92 md(md) {
93
94 // find rows in X with NAN values
95 idx_drop = rows_with_nan(X);
96 X_clean = X;
97 rows_to_zero(X_clean, idx_drop);
98}
99
100
101
102template <typename T1, typename T2, typename T3>
105 const T1 &Y,
106 const vector<string> &ids,
107 const vector<string> &family){
108
109 // store results
110 ModelFitGLMMList result(Y.n_cols, ModelFitGLMM());
111
112 tbb::mutex myMutex; // avoid issue with rdf and hatvalues
113
114 // Parallel part using Thread Building Blocks
115 tbb::task_arena limited_arena(nthreads);
116 limited_arena.execute([&] {
117 tbb::parallel_for(
118 tbb::blocked_range<int>(0, Y.n_cols, 10),
119 [&](const tbb::blocked_range<int>& r){
120
121 disable_parallel_blas();
122
123 T1 y;
124 vec w;
125 uvec idx;
126 // need local version due to reweighting
127 spectralDecomp dcmp_lcl(dcmp);
128
129 // iterate through responses
130 for (int j = r.begin(); j != r.end(); ++j) {
131
132 y = Y.col(j);
133 w = weights;
134
135 idx = unique(join_cols(find_nan(y), idx_drop));
136 y.elem(idx).zeros();
137 w.elem(idx).zeros();
138
139 fastglmm fit = fastglmm<vec, T2, T3>(y, X_clean, dcmp_lcl, w, offset, family[j], md, tol, tol_eta, maxit, lambda);
140
141 tbb::mutex::scoped_lock myLock(myMutex);
142 result.at(j) = fit.get_result();
143 result.at(j).ID = ids[j];
144 }
145 }); });
146
147 return result;
148}
149
150
151} // end namespace
152
153
154#endif
Definition ModelFit.h:355
glmmFitResponses(const T2 &X, const spectralDecomp< T3 > &dcmp, const vec &weights={}, const vec &offset={}, const double &left=-10, const double &right=10, const double &tol=1e-5, const double &tol_eta=1e-7, const int &maxit=100, const double &lambda=0, const int &nthreads=1, const ModelDetail md=LOW)
Definition glmmFitResponses.h:68
ModelFitGLMMList eval(const T1 &Y, const vector< string > &ids, const vector< string > &family)
Definition glmmFitResponses.h:104
Definition spectralDecomp.h:29
Definition CleanData.h:17
ModelDetail
Definition ModelFit.h:26
@ LOW
Definition ModelFit.h:28
vector< ModelFitGLMM > ModelFitGLMMList
Definition ModelFit.h:396