, pred, one_to_one=False, log_trans=False)

R^2 value for a regression of observed and predicted data


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.


float :

R^2 value


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.


[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


>>> import numpy as np
>>> import 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)
>>> # 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)
>>> # Generate some other data
>>> y_vals = np.random.normal(x_vals, 1)
>>> # Normal R^2
>>> comp.r_squared(x_vals, y_vals)
>>> # R^2 on to the one to one line
>>> comp.r_squared(x_vals, y_vals, one_to_one=True)