def r_squared(y, estimated):
    """
    Calculate the R-squared error term.

    Args:
        y: 1-d pylab array with length N, representing the y-coordinates of the
            N sample points
        estimated: an 1-d pylab array of values estimated by the regression
            model

    Returns:
        a float for the R-squared error term
    """
    estimated_error = ((y - estimated)**2).sum()
    mean_samples = (y.sum()) / len(y)
    measured_variability = ((y - mean_samples)**2).sum()

    return 1 - estimated_error / measured_variability