Click or drag to resize
Accord.NET (logo)

RSquaredLoss Class

R² (r-squared) loss.
Inheritance Hierarchy
SystemObject
  Accord.Math.Optimization.LossesLossBaseDouble, Double, Double
    Accord.Math.Optimization.LossesRSquaredLoss

Namespace:  Accord.Math.Optimization.Losses
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class RSquaredLoss : LossBase<double[][], double[][], double[]>, 
	ILoss<double[]>, ILoss<double[], double>
Request Example View Source

The RSquaredLoss type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyAdjust
Gets whether the adjusted version of the R² measure should be computed instead.
Public propertyExpected
Gets the expected outputs (the ground truth).
(Inherited from LossBaseTInput, TScore, TLoss.)
Public propertyNumberOfInputs
Gets or sets the number of variables being fit in the problem.
Public propertyWeights
Gets or sets the weights associated with each input-output pair.
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLoss(Double)
Computes the loss between the expected values (ground truth) and the given actual values that have been predicted.
Public methodLoss(Double)
Computes the loss between the expected values (ground truth) and the given actual values that have been predicted.
(Overrides LossBaseTInput, TScore, TLossLoss(TScore).)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks

The coefficient of determination is used in the context of statistical models whose main purpose is the prediction of future outcomes on the basis of other related information. It is the proportion of variability in a data set that is accounted for by the statistical model. It provides a measure of how well future outcomes are likely to be predicted by the model.

The R² coefficient of determination is a statistical measure of how well the regression line approximates the real data points. An R² of 1.0 indicates that the regression line perfectly fits the data.

References:

Examples

This example shows how to fit a multiple linear regression model and compute adjusted and non-adjusted versions of the R² coefficient of determination at the end:

// Let's say we would like predict a continuous number from a set 
// of discrete and continuous input variables. For this, we will 
// be using the Servo dataset from UCI's Machine Learning repository 
// as an example: http://archive.ics.uci.edu/ml/datasets/Servo

// Create a Servo dataset
Servo servo = new Servo();
object[][] instances = servo.Instances; // 167 x 4 
double[] outputs = servo.Output;        // 167 x 1

// This dataset contains 4 columns, where the first two are 
// symbolic (having possible values A, B, C, D, E), and the
// last two are continuous.

// We will use a codification filter to transform the symbolic 
// variables into one-hot vectors, while keeping the other two
// continuous variables intact:
var codebook = new Codification<object>()
{
    { "motor", CodificationVariable.Categorical },
    { "screw", CodificationVariable.Categorical },
    { "pgain", CodificationVariable.Continuous },
    { "vgain", CodificationVariable.Continuous },
};

// Learn the codebook
codebook.Learn(instances);

// We can gather some info about the problem:
int numberOfInputs = codebook.NumberOfInputs;   // should be 4 (since there are 4 variables)
int numberOfOutputs = codebook.NumberOfOutputs; // should be 12 (due their one-hot encodings)

// Now we can use it to obtain double[] vectors:
double[][] inputs = codebook.ToDouble().Transform(instances);

// We will use Ordinary Least Squares to create a
// linear regression model with an intercept term
var ols = new OrdinaryLeastSquares()
{
    UseIntercept = true
};

// Use Ordinary Least Squares to estimate a regression model:
MultipleLinearRegression regression = ols.Learn(inputs, outputs);

// We can compute the predicted points using:
double[] predicted = regression.Transform(inputs);

// And the squared error using the SquareLoss class:
double error = new SquareLoss(outputs).Loss(predicted);

// We can also compute other measures, such as the coefficient of determination r² using:
double r2 = new RSquaredLoss(numberOfOutputs, outputs).Loss(predicted); // should be 0.55086630162967354

// Or the adjusted or weighted versions of r² using:
var r2loss = new RSquaredLoss(numberOfOutputs, outputs)
{
    Adjust = true,        
    // Weights = weights; // (uncomment if you have a weighted problem)
};

double ar2 = r2loss.Loss(predicted); // should be 0.51586887058782993

// Alternatively, we can also use the less generic, but maybe more user-friendly method directly:
double ur2 = regression.CoefficientOfDetermination(inputs, outputs, adjust: true); // should be 0.51586887058782993
See Also