Skip to content
Snippets Groups Projects
Commit 3da1f6cf authored by Niklas Selke's avatar Niklas Selke
Browse files

Added OLS and quantile regression.

parent 3b22c43e
Branches
Tags
2 merge requests!5Modified the test for the 'value_count' statistic. Now all available sampling...,!3Niklas issue009 feat calculate quantile regression
......@@ -4,7 +4,8 @@ This module contains the following function:
calculate_trend - calculate the trend using the requested method
"""
import statsmodels.formula.api as smf
from toarstats.trends.ols import ols
from toarstats.trends.quant_reg import quant_reg
def calculate_trend(method, data, formula="value ~ datetime", quantiles=None):
......@@ -22,4 +23,22 @@ def calculate_trend(method, data, formula="value ~ datetime", quantiles=None):
:param quantiles: a single quantile or a list of quantiles to
calculate, these must be between 0 and 1; only
needed when ``method="quant"``
:raises ValueError: raised if the ``method`` parameter is not
recognized
:return: The result of the fit or a list of fit results if
``method="quant"`` and multiple quantiles are given
"""
if method == "OLS":
fit = ols(data, formula)
elif method == "quant":
try:
fit = [quant_reg(data, formula, quantile)
for quantile in quantiles]
except TypeError:
fit = quant_reg(data, formula, quantiles)
else:
raise ValueError(f"{method} is not recognized, must be 'OLS' or"
" 'quant'")
return fit
"""Ordinary least squares (OLS) linear regression calculation.
This module contains the following function:
ols - calculate the OLS linear regression
"""
import statsmodels.formula.api as smf
def ols(data, formula):
"""Calculate the OLS linear regression.
:param data: data containing a list of date time values and
associated parameter values on which to calculate the
trend
:param formula: the formula specifying the model
:return: The result of the fit
"""
model = smf.ols(formula, data)
fit = model.fit()
return fit
"""Quantile regression calculation.
This module contains the following function:
quant_reg - calculate the OLS linear regression
"""
import statsmodels.formula.api as smf
def quant_reg(data, formula, quantile):
"""Calculate the quantile regression.
:param data: data containing a list of date time values and
associated parameter values on which to calculate the
trend
:param formula: the formula specifying the model
:param quantile: a single quantile, must be between 0 and 1
:return: The result of the fit
"""
model = smf.quantreg(formula, data)
fit = model.fit(q=quantile)
return fit
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment