35 #ifndef OPENMS_MATH_STATISTICS_LINEARREGRESSION_H 36 #define OPENMS_MATH_STATISTICS_LINEARREGRESSION_H 42 #include "Wm5Vector2.h" 43 #include "Wm5ApprLineFit2.h" 44 #include "Wm5LinearSystem.h" 84 stand_dev_residuals_(0),
86 stand_error_slope_(0),
113 template <
typename Iterator>
114 void computeRegression(
double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin);
132 template <
typename Iterator>
133 void computeRegressionWeighted(
double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin);
136 double getIntercept()
const;
138 double getSlope()
const;
140 double getXIntercept()
const;
142 double getLower()
const;
144 double getUpper()
const;
146 double getTValue()
const;
148 double getRSquared()
const;
150 double getStandDevRes()
const;
152 double getMeanRes()
const;
154 double getStandErrSlope()
const;
156 double getChiSquared()
const;
158 double getRSD()
const;
189 void computeGoodness_(
const std::vector<Wm5::Vector2d>& points,
double confidence_interval_P);
192 template <
typename Iterator>
193 double computeChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin,
double slope,
double intercept);
196 template <
typename Iterator>
197 double computeWeightedChiSquare(Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin,
double slope,
double intercept);
212 double computePointY(
double x,
double slope,
double intercept)
214 return slope * x + intercept;
220 template <
typename Iterator>
223 double chi_squared = 0.0;
224 Iterator xIter = x_begin;
225 Iterator yIter = y_begin;
226 for (; xIter != x_end; ++xIter, ++yIter)
228 chi_squared += std::pow(*yIter - computePointY(*xIter, slope, intercept), 2);
235 template <
typename Iterator>
238 double chi_squared = 0.0;
239 Iterator xIter = x_begin;
240 Iterator yIter = y_begin;
241 Iterator wIter = w_begin;
242 for (; xIter != x_end; ++xIter, ++yIter, ++wIter)
244 chi_squared += *wIter * std::pow(*yIter - computePointY(*xIter, slope, intercept), 2);
250 template <
typename Iterator>
258 bool pass = Wm5::HeightLineFit2<double>(
static_cast<int>(points.size()), &points.front(), slope_, intercept_);
259 chi_squared_ = computeChiSquare(x_begin, x_end, y_begin, slope_, intercept_);
263 computeGoodness_(points, confidence_interval_P);
267 throw Exception::UnableToFit(__FILE__, __LINE__, __PRETTY_FUNCTION__,
"UnableToFit-LinearRegression",
"Could not fit a linear model to the data");
271 template <
typename Iterator>
280 int numPoints = points.size();
281 double sumX = 0, sumY = 0;
282 double sumXX = 0, sumXY = 0;
284 Iterator wIter = w_begin;
286 for (
int i = 0; i < numPoints; ++i, ++wIter)
288 sumX += (*wIter) * points[i].X();
289 sumY += (*wIter) * points[i].Y();
290 sumXX += (*wIter) * points[i].X() * points[i].X();
291 sumXY += (*wIter) * points[i].X() * points[i].Y();
307 bool nonsingular = Wm5::LinearSystem<double>().Solve2(A, B, X);
313 chi_squared_ = computeWeightedChiSquare(x_begin, x_end, y_begin, w_begin, slope_, intercept_);
317 computeGoodness_(points, confidence_interval_P);
321 throw Exception::UnableToFit(__FILE__, __LINE__, __PRETTY_FUNCTION__,
"UnableToFit-LinearRegression",
"Could not fit a linear model to the data");
virtual ~LinearRegression()
Destructor.
Definition: LinearRegression.h:93
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:76
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
double rsd_
the relative standard deviation
Definition: LinearRegression.h:185
double upper_
The upper bound of the confidence interval.
Definition: LinearRegression.h:171
double chi_squared_
The value of the Chi Squared statistic.
Definition: LinearRegression.h:183
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:221
void computeRegressionWeighted(double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin, Iterator w_begin)
This function computes the best-fit linear regression coefficients of the model for the weighted da...
Definition: LinearRegression.h:272
This class offers functions to perform least-squares fits to a straight line model, .
Definition: LinearRegression.h:71
double x_intercept_
The intercept of the fitted line with the x-axis.
Definition: LinearRegression.h:167
double t_star_
The value of the t-statistic.
Definition: LinearRegression.h:173
double stand_error_slope_
The standard error of the slope.
Definition: LinearRegression.h:181
double mean_residuals_
Mean of residuals.
Definition: LinearRegression.h:179
double slope_
The slope of the fitted line.
Definition: LinearRegression.h:165
Exception used if an error occurred while fitting a model to a given dataset.
Definition: Exception.h:662
double stand_dev_residuals_
The standard deviation of the residuals.
Definition: LinearRegression.h:177
double lower_
The lower bound of the confidence interval.
Definition: LinearRegression.h:169
double r_squared_
The squared correlation coefficient (Pearson)
Definition: LinearRegression.h:175
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:236
double intercept_
The intercept of the fitted line with the y-axis.
Definition: LinearRegression.h:163
void computeRegression(double confidence_interval_P, Iterator x_begin, Iterator x_end, Iterator y_begin)
This function computes the best-fit linear regression coefficients of the model for the dataset ...
Definition: LinearRegression.h:251