def R_run_loess(x, y, span=0.75): """ Predict y as function of x. Takes two numpy vectors. """ # Ensure that Inf/-Inf values are substituted x[utils.where_null(x)] = robj.NA_Real y[utils.where_null(x)] = robj.NA_Real data = robj.DataFrame({"x": x, "y": y}) loess_fit = r.loess("y ~ x", data=data, span=span, family="symmetric") correction_factor = np.array(list(r.predict(loess_fit, x))) corrected_y = \ np.array(list(y)) - correction_factor return corrected_y, correction_factor
def run_lowess(X, Y, frac=0.75, missing="none"): """ Y ~ X lowess. Parameters: ----------- X: X values Y: Y values frac: fraction of data used to estimate each y-value. missing: how to handle missing values (by default "drop" them). """ X[utils.where_null(X)] = np.nan Y[utils.where_null(Y)] = np.nan # Lowess takes Y values first fitted_Y = lowess(Y, X, return_sorted=False, frac=frac, missing=missing) return fitted_Y