def regression_apply(row, timepoints, weighted):
"""
:py:meth:`pandas.DataFrame.apply` apply function for calculating
enrichment using linear regression. If *weighted* is ``True`` perform
weighted least squares; else perform ordinary least squares.
Weights for weighted least squares are included in *row*.
Returns a :py:class:`pandas.Series` containing regression coefficients,
residuals, and statistics.
"""
# retrieve log ratios from the row
y = row[['L_{}'.format(t) for t in timepoints]]
# re-scale the x's to fall within [0, 1]
xvalues = [x / float(max(timepoints)) for x in timepoints]
# perform the fit
X = sm.add_constant(xvalues) # fit intercept
if weighted:
W = row[['W_{}'.format(t) for t in timepoints]]
fit = sm.WLS(y, X, weights=W).fit()
else:
fit = sm.OLS(y, X).fit()
# re-format as a data frame row
values = np.concatenate([fit.params, [fit.bse['x1'], fit.tvalues['x1'],
fit.pvalues['x1']], fit.resid])
index = ['intercept', 'slope', 'SE_slope', 't', 'pvalue_raw'] + \
['e_{}'.format(t) for t in timepoints]
return pd.Series(data=values, index=index)
评论列表
文章目录