Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new functions to get list of residuals and predcition #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions regression.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Regression struct {
Formula string
crosses []featureCross
hasRun bool
prediction describe
residuals describe
}

type dataPoint struct {
Expand All @@ -43,6 +45,8 @@ type dataPoint struct {
type describe struct {
obs string
vars map[int]string
pred []float64
res []float64
}

// DataPoints is a slice of *dataPoint
Expand Down Expand Up @@ -82,6 +86,27 @@ func (r *Regression) GetObserved() string {
return r.names.obs
}

func (r *Regression) SetPrediction(prediction []float64) {
r.prediction.pred = prediction
}

// GetPredictions returns a list of the prediction value.
func (r *Regression) GetPredictions() []float64 {
var list []float64
for _, d := range r.data {
list = append(list, d.Predicted)
}
return list
}

// GetResiduals returns a list of the residuals value
func (r *Regression) GetResiduals() []float64 {
return r.residuals.res
}
func (r *Regression) SetResidual(residuals []float64) {
r.residuals.res = residuals
}

// SetVar sets the name of variable i.
func (r *Regression) SetVar(i int, name string) {
if len(r.names.vars) == 0 {
Expand Down Expand Up @@ -268,9 +293,16 @@ func (r *Regression) calcR2() string {

func (r *Regression) calcResiduals() string {
str := fmt.Sprintf("Residuals:\nobserved|\tPredicted|\tResidual\n")
var list []float64
var resList []float64
for _, d := range r.data {
list = append(list, d.Predicted)
resList = append(resList, d.Observed-d.Predicted)
str += fmt.Sprintf("%.2f|\t%.2f|\t%.2f\n", d.Observed, d.Predicted, d.Observed-d.Predicted)
}
r.SetPrediction(list)
r.SetResidual(resList)
// fmt.Println(list)
str += "\n"
return str
}
Expand Down