glm

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2024 License: BSD-3-Clause Imports: 4 Imported by: 1

README

GLM = general linear model

GLM contains results and parameters for running a general linear model, which is a general form of multivariate linear regression, supporting multiple independent and dependent variables.

Make a NewGLM and then do Run() on a tensor IndexView with the relevant data in columns of the table.

Fitting Methods

Standard QR Decomposition

The standard algorithm involves eigenvalue computation using QR Decomposition. TODO.

Iterative Batch Mode Least Squares

Batch-mode gradient descent is used and the relevant parameters can be altered from defaults before calling Run as needed.

This mode supports Ridge (L2 norm) and Lasso (L1 norm) forms of regression, which add different forms of weight decay to the LMS cost function.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GLM

type GLM struct {
	// Coeff are the coefficients to map from input independent variables
	// to the dependent variables.  The first, outer dimension is number of
	// dependent variables, and the second, inner dimension is number of
	// independent variables plus one for the offset (b) (last element).
	Coeff tensor.Float64

	// mean squared error of the fitted values relative to data
	MSE float64

	// R2 is the r^2 total variance accounted for by the linear model,
	// for each dependent variable = 1 - (ErrVariance / ObsVariance)
	R2 []float64

	// Observed variance of each of the dependent variables to be predicted.
	ObsVariance []float64

	// Variance of the error residuals per dependent variables
	ErrVariance []float64

	//	optional names of the independent variables, for reporting results
	IndepNames []string

	//	optional names of the dependent variables, for reporting results
	DepNames []string

	// ZeroOffset restricts the offset of the linear function to 0,
	// forcing it to pass through the origin.  Otherwise, a constant offset "b"
	// is fit during the model fitting process.
	ZeroOffset bool

	// learning rate parameter, which can be adjusted to reduce iterations based on
	// specific properties of the data, but the default is reasonable for most "typical" data.
	LRate float64 `default:"0.1"`

	// tolerance on difference in mean squared error (MSE) across iterations to stop
	// iterating and consider the result to be converged.
	StopTolerance float64 `default:"0.0001"`

	// Constant cost factor subtracted from weights, for the L1 norm or "Lasso"
	// regression.  This is good for producing sparse results but can arbitrarily
	// select one of multiple correlated independent variables.
	L1Cost float64

	// Cost factor proportional to the coefficient value, for the L2 norm or "Ridge"
	// regression.  This is good for generally keeping weights small and equally
	// penalizes correlated independent variables.
	L2Cost float64

	// CostStartIter is the iteration when we start applying the L1, L2 Cost factors.
	// It is often a good idea to have a few unconstrained iterations prior to
	// applying the cost factors.
	CostStartIter int `default:"5"`

	// maximum number of iterations to perform
	MaxIters int `default:"50"`

	// Table of data
	Table *table.IndexView

	// tensor columns from table with the respective variables
	IndepVars, DepVars, PredVars, ErrVars tensor.Tensor

	// Number of independent and dependent variables
	NIndepVars, NDepVars int
}

GLM contains results and parameters for running a general linear model, which is a general form of multivariate linear regression, supporting multiple independent and dependent variables. Make a NewGLM and then do Run() on a tensor table.IndexView with the relevant data in columns of the table. Batch-mode gradient descent is used and the relevant parameters can be altered from defaults before calling Run as needed.

func NewGLM

func NewGLM() *GLM

func (*GLM) Coeffs

func (glm *GLM) Coeffs() string

Coeffs returns a string describing the coefficients

func (*GLM) Defaults

func (glm *GLM) Defaults()

func (*GLM) Run

func (glm *GLM) Run()

Run performs the multi-variate linear regression using data SetTable function, learning linear coefficients and an overall static offset that best fits the observed dependent variables as a function of the independent variables. Initial values of the coefficients, and other parameters for the regression, should be set prior to running.

func (*GLM) SetTable

func (glm *GLM) SetTable(ix *table.IndexView, indepVars, depVars, predVars, errVars string) error

SetTable sets the data to use from given indexview of table, where each of the Vars args specifies a column in the table, which can have either a single scalar value for each row, or a tensor cell with multiple values. predVars and errVars (predicted values and error values) are optional.

func (*GLM) Variance

func (glm *GLM) Variance() string

Variance returns a description of the variance accounted for by the regression equation, R^2, for each dependent variable, along with the variances of observed and errors (residuals), which are used to compute it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL