def r_squared(obs, pred, one_to_one=False, log_trans=False):
if log_trans:<EOL><INDENT>obs = np.log(obs)<EOL>pred = np.log(pred)<EOL><DEDENT>if one_to_one:<EOL><INDENT>r_sq = <NUM_LIT:1> - (sum_of_squares(obs, pred) /<EOL>sum_of_squares(obs, np.mean(obs)))<EOL><DEDENT>else:<EOL><INDENT>b0, b1, r, p_value, se = stats.linregress(obs, pred)<EOL>r_sq = r ** <NUM_LIT:2><EOL><DEDENT>return r_sq<EOL>
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 [#]_),
    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.

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

References
----------
.. [#]
   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