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