regression

package module
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 9 Imported by: 0

README

regression v2

Go Reference License

Multiple linear regression analysis library written in Go

NOTE: This project is originally based on https://github.com/sajari/regression

installation

$ go get github.com/anyappinc/regression/v2

example usage

Import the package, create a regression and add data to it. You can use as many variables as you like, in the below example there are 3 variables for each observation.

package main

import (
	"fmt"

	"github.com/anyappinc/regression/v2"
)

func main() {
	r := regression.NewRegression()
	r.SetObjectiveVariableLabel("Murders per annum per 1,000,000 inhabitants")
	r.SetExplanatoryVariableLabel(0, "Inhabitants")
	r.SetExplanatoryVariableLabel(1, "Percent with incomes below $5000")
	r.SetExplanatoryVariableLabel(2, "Percent unemployed")
	r.AddObservations(
		regression.NewObservation(11.2, []float64{587000, 16.5, 6.2}),
		regression.NewObservation(13.4, []float64{643000, 20.5, 6.4}),
		regression.NewObservation(40.7, []float64{635000, 26.3, 9.3}),
		regression.NewObservation(5.3, []float64{692000, 16.5, 5.3}),
		regression.NewObservation(24.8, []float64{1248000, 19.2, 7.3}),
		regression.NewObservation(12.7, []float64{643000, 16.5, 5.9}),
		regression.NewObservation(20.9, []float64{1964000, 20.2, 6.4}),
		regression.NewObservation(35.7, []float64{1531000, 21.3, 7.6}),
		regression.NewObservation(8.7, []float64{713000, 17.2, 4.9}),
		regression.NewObservation(9.6, []float64{749000, 14.3, 6.4}),
		regression.NewObservation(14.5, []float64{7895000, 18.1, 6}),
		regression.NewObservation(26.9, []float64{762000, 23.1, 7.4}),
		regression.NewObservation(15.7, []float64{2793000, 19.1, 5.8}),
		regression.NewObservation(36.2, []float64{741000, 24.7, 8.6}),
		regression.NewObservation(18.1, []float64{625000, 18.6, 6.5}),
		regression.NewObservation(28.9, []float64{854000, 24.9, 8.3}),
		regression.NewObservation(14.9, []float64{716000, 17.9, 6.7}),
		regression.NewObservation(25.8, []float64{921000, 22.4, 8.6}),
		regression.NewObservation(21.7, []float64{595000, 20.2, 8.4}),
		regression.NewObservation(25.7, []float64{3353000, 16.9, 6.7}),
	)
	model, err := r.Run()
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("Regression formula:\n%v\n", model.FormulaString())
}

Note: You can also add observations one by one.

Once calculated, you can look at the R^2, Standard Error, ANOVA, Coefficients, etc. e.g.

// Get the coefficient for the "Inhabitants" variable 0:
c := model.ExplanatoryVars[0].Coeff

You can also use the model to predict new observation

prediction, err := model.Predict([]float64{587000, 16.5, 6.2})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNearSingular    = &ConditionError{isExactlySingular: false}
	ErrExactlySingular = &ConditionError{isExactlySingular: true}
)
View Source
var (
	// ErrNotEnoughObservations signals that there weren't enough observations to train the model.
	ErrNotEnoughObservations = errors.New("not enough observations")
	// ErrTooManyExplanatoryVars signals that there are too many explanatory variables for the number of observations being made.
	ErrTooManyExplanatoryVars = errors.New("not enough observations to support this many explanatory variables")
	// ErrNoExplanatoryVars signals that there is no explanatory variables to train the model.
	ErrNoExplanatoryVars = errors.New("no explanatory variables to train the models")
	// ErrInvalidArgument signals that any of given arguments to call the function was invalid.
	ErrInvalidArgument = errors.New("invalid argument")
)

Functions

func NewObservation

func NewObservation(o float64, es []float64) *observation

NewObservation creates a well formed *observation used for training.

Types

type ANOVA

type ANOVA struct {
	RegressionSumOfSquares    float64 // 回帰の平方和
	RegressionDegreeOfFreedom int     // 回帰の自由度
	RegressionMeanOfSquares   float64 // 回帰の平均平方
	RegressionFstat           float64 // 回帰のF値
	RegressionProb            float64 // 回帰の有意確率
	ResidualSumOfSquares      float64 // 残差の平方和
	ResidualDegreeOfFreedom   int     // 残差の自由度
	ResidualMeanOfSquares     float64 // 残差の平均平方
	TotalSumOfSquares         float64 // 合計の平方和
	TotalDegreeOfFreedom      int     // 合計の自由度
}

ANOVA : 分散分析の結果

type ConditionError added in v2.2.0

type ConditionError struct {
	Hint *ConditionErrorHint
	// contains filtered or unexported fields
}

func (ConditionError) Error added in v2.2.0

func (e ConditionError) Error() string

func (ConditionError) Is added in v2.2.0

func (e ConditionError) Is(err error) bool

func (ConditionError) Unwrap added in v2.2.0

func (e ConditionError) Unwrap() error

type ConditionErrorHint added in v2.2.0

type ConditionErrorHint struct {
	ExplanatoryVars []ExplanatoryVarHint
}

type ExplanatoryVarHint added in v2.2.0

type ExplanatoryVarHint struct {
	OriginalIndex int     // 元々のインデックス
	Label         string  // 名称
	Coeff         float64 // 偏回帰係数 B
	VIF           float64 // 共線性の統計量 VIF
}

type ExplanatoryVarResult

type ExplanatoryVarResult struct {
	OriginalIndex      int     // 元々のインデックス
	Label              string  // 名称
	Coeff              float64 // 偏回帰係数 B
	StandardError      float64 // 偏回帰係数の標準誤差(非標準化係数 標準誤差)
	StandardizedCoeff  float64 // 標準化偏回帰係数 β
	TStat              float64 // t値
	Prob               float64 // 有意確率(p値)
	Correlation        float64 // ゼロ次相関(通常の相関係数)
	PartialCorrelation float64 // 偏相関
	PartCorrelation    float64 // 部分相関
	Tolerance          float64 // 共線性の統計量 許容度 TOL
	VIF                float64 // 共線性の統計量 VIF
}

ExplanatoryVarResult : 回帰分析により算出された説明変数の結果

type InterceptResult

type InterceptResult struct {
	Value         float64 // 定数(y切片)の値
	StandardError float64 // 標準誤差(非標準化係数 標準誤差)
	TStat         float64 // t値
}

InterceptResult : 回帰分析により算出された定数(y切片)の結果

type Model

type Model struct {
	NumOfObservations             int                    // 分析に用いた観測値の数
	NumOfExplanatoryVars          int                    // 分析に用いた説明変数の数
	DisregardedExplanatoryVarsSet map[int]struct{}       // 分析に使用されなかったインデックスのセット
	UnexplainedVariation          float64                // 残差変動
	ExplainedVariation            float64                // 回帰変動
	TotalVariation                float64                // 全変動
	R                             float64                // 重相関係数
	R2                            float64                // 決定係数
	AdjustedR2                    float64                // 自由度調整済み決定係数
	StandardError                 float64                // 回帰の標準誤差(推定値の標準偏差)
	ANOVA                         *ANOVA                 // 分散分析
	ObjectiveVarLabel             string                 // 目的変数の名称
	Intercept                     *InterceptResult       // 定数(y切片)の分析結果
	ExplanatoryVars               []ExplanatoryVarResult // 各説明変数の分析結果
	// contains filtered or unexported fields
}

Model : 回帰モデル

func (*Model) FormulaString

func (m *Model) FormulaString() string

FormulaString : 回帰モデル式を文字列で取得する

func (*Model) GetObservationsAnalysis

func (m *Model) GetObservationsAnalysis() *ObservationsAnalysis

GetObservationsAnalysis : 観測値に関する分析結果を取得する

func (*Model) Predict

func (m *Model) Predict(vars []float64) (float64, error)

Predict calculates the predicted value

type ObservationsAnalysis

type ObservationsAnalysis struct {
	ObjectiveVarsDense                 *mat.Dense // 目的変数の観測値の行列
	ExplanatoryVarsDense               *mat.Dense // 説明変数の観測値の行列
	MeanOfObjectiveVars                float64    // 目的変数の観測値の平均
	StandardDeviationOfObjectiveVars   float64    // 目的変数の観測値の標準偏差
	MeansOfExplanatoryVars             []float64  // 各説明変数の観測値の平均
	StandardDeviationOfExplanatoryVars []float64  // 各説明変数の観測値の標準偏差
	PredictedVals                      []float64  // 予測値
	Residuals                          []float64  // 残差
}

ObservationsAnalysis : 観測値に関する分析結果

type Regression

type Regression struct {
	// contains filtered or unexported fields
}

Regression is the exposed data structure for interacting with the API.

func NewRegression

func NewRegression() *Regression

NewRegression initializes the structure and returns it for interacting with regression APIs.

func (*Regression) AddObservations

func (r *Regression) AddObservations(observations ...*observation) error

AddObservations adds observations.

func (*Regression) BackwardElimination

func (r *Regression) BackwardElimination(p float64, forcedExpVarsIndexesSet map[int]struct{}) ([]Model, error)

BackwardElimination : 変数減少法で解析する

`forcedExpVarsIndexesSet`に指定したインデックスの説明変数は強制投入(必ず使用)される

func (*Regression) DisregardIndex

func (r *Regression) DisregardIndex(idx int)

DisregardIndex adds given index to the disregarding set

func (*Regression) GetExplanatoryVariableLabel

func (r *Regression) GetExplanatoryVariableLabel(i int) string

GetExplanatoryVariableLabel gets the label of i-th explanatory variable.

func (*Regression) GetObjectiveVariableLabel

func (r *Regression) GetObjectiveVariableLabel() string

GetObjectiveVariableLabel gets the label of the objective variable.

func (*Regression) ResetDisregarding

func (r *Regression) ResetDisregarding()

ResetDisregarding : 無視する説明変数の設定をリセットする

func (*Regression) Run

func (r *Regression) Run() (*Model, error)

Run calculates a model using QR decomposition.

func (*Regression) SetExplanatoryVariableLabel

func (r *Regression) SetExplanatoryVariableLabel(i int, label string)

SetExplanatoryVariableLabel sets the label of i-th explanatory variable.

func (*Regression) SetObjectiveVariableLabel

func (r *Regression) SetObjectiveVariableLabel(label string)

SetObjectiveVariableLabel sets the label of the objective variable.

func (*Regression) ValidateExplanatoryVars added in v2.2.0

func (r *Regression) ValidateExplanatoryVars() []int

ValidateExplanatoryVars returns indexes of invalid explanatory variables. It considers an explanatory variable is not valid if is has all same observed values.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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