openModeller  Version 1.5.0
regression.cpp
Go to the documentation of this file.
1 
36 #include <openmodeller/om.hh>
37 #include <openmodeller/Sample.hh>
38 
39 #include "regression.hh"
40 
42  _dimensions(0),
43  _resamples(0),
44  _paramA(),
45  _paramB(),
46  _paramC()
47 { }
48 
49 
51 {
52  //next two lines commented out since the vars arent used
53  //Scalar y; // outcome (dependent) variable for sample
54  //Scalar xx; // temp storage for x^2
55  Sample x; // predictor (independent) variable for current sample
56  Scalar s_y; // sum of y for all samples
57  Sample s_x; // sum of x for all samples
58  Sample s_xy; // sum of x*y for all samples
59  Sample s_xx; // sum of x^2 for all samples
60  Sample s_xxy; // sum of y*x^2 for all samples
61  Sample s_x4; // sum of x^4 for all samples
62 
63  int n = _resamples = occs->numOccurrences();
64 
65  // number of layers/genes/independent variables
66  _dimensions = (*occs)[0]->environment().size();
67 
68  s_y = 0.0;
69  s_x = s_xy = s_xx = s_xxy = s_x4 = Sample(_dimensions);
70 
71  // iterate through the samples
72  OccurrencesImpl::const_iterator oc_it = occs->begin();
73  OccurrencesImpl::const_iterator oc_end = occs->end();
74 
75  while (oc_it != oc_end)
76  {
77  Scalar y = ( (*oc_it)->abundance() > 0.0 ) ? 1.0 : 0.0;
78  Sample x = (*oc_it)->environment();
79 
80  Sample::iterator xit = x.begin();
81  Sample::iterator end = x.end();
82  Sample::iterator s_xi = s_x.begin();
83  Sample::iterator s_xxi = s_xx.begin();
84  Sample::iterator s_xyi = s_xy.begin();
85  Sample::iterator s_xxyi = s_xxy.begin();
86  Sample::iterator s_x4i = s_x4.begin();
87 
88  s_y += y;
89 
90  //printf("Regression: Resample %5d\n", i);
91 
92  while (xit != end)
93  {
94  Scalar xi = (*xit);
95  Scalar xx = xi * xi;
96 
97  (*s_xi) += xi;
98  (*s_xxi) += xx;
99  (*s_xyi) += xi * y;
100  (*s_xxyi) += xx * y;
101  (*s_x4i) += xx * xx;
102 
103  ++xit;
104  ++s_xi;
105  ++s_xxi;
106  ++s_xyi;
107  ++s_xxyi;
108  ++s_x4i;
109  }
110 
111  oc_it++;
112  }
113 
114  //printf("Sum of abundances: %7.3f\n", s_y);
115 
117 
121  Sample::iterator end = _paramC.end();
122  Sample::iterator s_xi = s_x.begin();
123  Sample::iterator s_xxi = s_xx.begin();
124  Sample::iterator s_xyi = s_xy.begin();
125  Sample::iterator s_xxyi = s_xxy.begin();
126  Sample::iterator s_x4i = s_x4.begin();
127 
128  while (pci != end)
129  {
130  //printf("Regression: ParamIndex (%12d, %12d)\n", pai, end);
131 
132  (*pci) = ( n * (*s_xxyi) - ( (*s_xxi) * s_y ) ) /
133  ( n * (*s_x4i) - ( (*s_xxi) * (*s_xxi) ) );
134 
135  (*pbi) = ( n * (*s_xyi) - ( (*s_xi) * s_y ) ) /
136  ( n * (*s_xxi) - ( (*s_xi) * (*s_xi) ) );
137 
138  (*pai) = ( s_y / n ) - ( (*pbi) * (*s_xi) / n );
139 
140  ++pai;
141  ++pbi;
142  ++pci;
143 
144  ++s_xi;
145  ++s_xxi;
146  ++s_xyi;
147  ++s_xxyi;
148  ++s_x4i;
149  }
150 
151  /*
152  Log::instance()->info("logit: a=%+8.4f b=%+8.4f c=%+8.4f n=%d\n", a, b, c, n);
153  Log::instance()->info("aux: xi=%+8.4f yi=%+8.4f xiyi=%+8.4f xxi=%+8.4f xxiyi=%+8.4f xi4=%+8.4f\n",
154  s_xi, s_yi, s_xiyi, s_xxi, s_xxiyi, s_xi4);
155  */
156 }
157 
Sample _paramB
Definition: regression.hh:56
iterator end()
Definition: Sample.hh:88
double Scalar
Type of map values.
Definition: om_defs.hh:39
Sample _paramA
Definition: regression.hh:55
int _resamples
Definition: regression.hh:54
Sample _paramC
Definition: regression.hh:57
iterator begin()
Definition: Sample.hh:87
void calculateParameters(const OccurrencesPtr &occs)
Definition: regression.cpp:50
Scalar * iterator
Definition: Sample.hh:86
int _dimensions
Definition: regression.hh:53
std::vector< OccurrencePtr >::const_iterator const_iterator
Definition: Occurrences.hh:85
Definition: Sample.hh:25