macroeco.compare.r_squared¶
- macroeco.compare.r_squared(obs, pred, one_to_one=False, log_trans=False)¶
R^2 value for a regression of observed and predicted data
Parameters: obs : iterable
Observed data
pred : iterable
Predicted data
one_to_one : bool
If True, calculates the R^2 based on the one-to-one line (see [1]), and if False, calculates the standard R^2 based on a linear regression. Default False.
log_trans : bool
If True, log transforms obs and pred before R^2 calculation.
Returns: float :
R^2 value
Notes
Using the traditional R^2 to compare the fit of observed and predicted values may be misleading as the relationship may not be one-to-one but the R^2 value may be quite high. The one-to-one option alleviates this problem. Note that with the one-to-one option R^2 can be negative.
References
[1] White, E., Thibault, K., & Xiao, X. (2012). Characterizing the species abundance distributions across taxa and ecosystems using a simple maximum entropy model. Ecology, 93(8), 1772-8 Examples
>>> import numpy as np >>> import macroeco.compare as comp
>>> # Generate some data >>> x_vals = np.linspace(1, 20, num=100) >>> y_vals = np.random.normal(4 + x_vals*2, 1)
>>> # Standard R^2 >>> comp.r_squared(x_vals, y_vals) 0.99336568326291697
>>> # R^2 about the 1:1 line, will be a poor fit (possibly negative) >>> comp.r_squared(x_vals, y_vals, one_to_one=True) -6.8621799432144988
>>> # Generate some other data >>> y_vals = np.random.normal(x_vals, 1)
>>> # Normal R^2 >>> comp.r_squared(x_vals, y_vals) 0.97651897660174425
>>> # R^2 on to the one to one line >>> comp.r_squared(x_vals, y_vals, one_to_one=True) 0.97591430200514639