35 #ifndef OPENMS_MATH_STATISTICS_LINEARREGRESSION_H 36 #define OPENMS_MATH_STATISTICS_LINEARREGRESSION_H 43 #include "Wm5Vector2.h" 44 #include "Wm5ApprLineFit2.h" 45 #include "Wm5LinearSystem.h" 83 stand_dev_residuals_(0),
85 stand_error_slope_(0),
117 template <
typename Iterator>
118 void computeRegression(
double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin,
bool compute_goodness =
true);
142 template <
typename Iterator>
143 void computeRegressionWeighted(
double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin,
bool compute_goodness =
true);
146 double getIntercept()
const;
148 double getSlope()
const;
150 double getXIntercept()
const;
152 double getLower()
const;
154 double getUpper()
const;
156 double getTValue()
const;
158 double getRSquared()
const;
160 double getStandDevRes()
const;
162 double getMeanRes()
const;
164 double getStandErrSlope()
const;
166 double getChiSquared()
const;
168 double getRSD()
const;
199 void computeGoodness_(
const std::vector<Wm5::Vector2d>& points,
double confidence_interval_P);
202 template <
typename Iterator>
203 double computeChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin,
double slope,
double intercept);
206 template <
typename Iterator>
207 double computeWeightedChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin,
double slope,
double intercept);
222 double computePointY(
double x,
double slope,
double intercept)
224 return slope * x + intercept;
230 template <
typename Iterator>
233 double chi_squared = 0.0;
234 Iterator xIter = x_begin;
235 Iterator yIter = y_begin;
236 for (; xIter != x_end; ++xIter, ++yIter)
238 chi_squared += std::pow(*yIter - computePointY(*xIter, slope, intercept), 2);
245 template <
typename Iterator>
248 double chi_squared = 0.0;
249 Iterator xIter = x_begin;
250 Iterator yIter = y_begin;
251 Iterator wIter = w_begin;
252 for (; xIter != x_end; ++xIter, ++yIter, ++wIter)
254 chi_squared += *wIter * std::pow(*yIter - computePointY(*xIter, slope, intercept), 2);
260 template <
typename Iterator>
268 bool pass = Wm5::HeightLineFit2<double>(
static_cast<int>(points.size()), &points.front(), slope_, intercept_);
269 chi_squared_ = computeChiSquare(x_begin, x_end, y_begin, slope_, intercept_);
273 if (compute_goodness && points.size() > 2) computeGoodness_(points, confidence_interval_P);
278 "UnableToFit-LinearRegression",
String(
"Could not fit a linear model to the data (") + points.size() +
" points).");
282 template <
typename Iterator>
291 int numPoints =
static_cast<int>(points.size());
292 double sumX = 0, sumY = 0;
293 double sumXX = 0, sumXY = 0;
295 Iterator wIter = w_begin;
297 for (
int i = 0; i < numPoints; ++i, ++wIter)
299 sumX += (*wIter) * points[i].X();
300 sumY += (*wIter) * points[i].Y();
301 sumXX += (*wIter) * points[i].X() * points[i].X();
302 sumXY += (*wIter) * points[i].X() * points[i].Y();
318 bool nonsingular = Wm5::LinearSystem<double>().Solve2(A, B, X);
324 chi_squared_ = computeWeightedChiSquare(x_begin, x_end, y_begin, w_begin, slope_, intercept_);
328 if (compute_goodness && points.size() > 2) computeGoodness_(points, confidence_interval_P);
333 "UnableToFit-LinearRegression",
"Could not fit a linear model to the data");
A more convenient string class.
Definition: String.h:57
virtual ~LinearRegression()
Destructor.
Definition: LinearRegression.h:92
std::vector< Wm5::Vector2d > iteratorRange2Wm5Vectors(Iterator x_begin, Iterator x_end, Iterator y_begin)
Copies the distance(x_begin,x_end) elements starting at x_begin and y_begin into the Wm5::Vector...
Definition: RegressionUtils.h:45
LinearRegression()
Constructor.
Definition: LinearRegression.h:75
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
double rsd_
the relative standard deviation
Definition: LinearRegression.h:195
double upper_
The upper bound of the confidence interval.
Definition: LinearRegression.h:181
double chi_squared_
The value of the Chi Squared statistic.
Definition: LinearRegression.h:193
double computeChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, double slope, double intercept)
Compute the chi squared of a linear fit.
Definition: LinearRegression.h:231
This class offers functions to perform least-squares fits to a straight line model, .
Definition: LinearRegression.h:70
double x_intercept_
The intercept of the fitted line with the x-axis.
Definition: LinearRegression.h:177
double t_star_
The value of the t-statistic.
Definition: LinearRegression.h:183
double stand_error_slope_
The standard error of the slope.
Definition: LinearRegression.h:191
double mean_residuals_
Mean of residuals.
Definition: LinearRegression.h:189
double slope_
The slope of the fitted line.
Definition: LinearRegression.h:175
void computeRegression(double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin, bool compute_goodness=true)
This function computes the best-fit linear regression coefficients of the model for the dataset ...
Definition: LinearRegression.h:261
Exception used if an error occurred while fitting a model to a given dataset.
Definition: Exception.h:677
double stand_dev_residuals_
The standard deviation of the residuals.
Definition: LinearRegression.h:187
double lower_
The lower bound of the confidence interval.
Definition: LinearRegression.h:179
double r_squared_
The squared correlation coefficient (Pearson)
Definition: LinearRegression.h:185
double computeWeightedChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, double slope, double intercept)
Compute the chi squared of a weighted linear fit.
Definition: LinearRegression.h:246
double intercept_
The intercept of the fitted line with the y-axis.
Definition: LinearRegression.h:173
void computeRegressionWeighted(double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin, bool compute_goodness=true)
This function computes the best-fit linear regression coefficients of the model for the weighted da...
Definition: LinearRegression.h:283