func (*Tool) Run(args []string) { fmt.Println("running logistic regression") n := 1000 p := 10 beta := make([]float64, p) beta[0] = rand.NormFloat64() beta[1] = rand.NormFloat64() x := la.NewMatrix(n, p) y := la.NewVector(n) for i := 0; i < n; i++ { v := randVec(p) var z float64 for j := 0; j < p; j++ { x.Set(i, j, v[j]) z += beta[j] } if z > 0 { y.Set(i, +1) } else { y.Set(i, -1) } } rp := &model.RegressionProblem{ N: n, P: p, Data: x, Response: y, ColumnNames: names("p", p), RowNames: names("x", n), ResponseName: "y", } rc := &model.LogisticRegressionRisk{} pc := model.NewLassoPenalty(p) dv := 0.001 vmax := 0.07 mon := &FixedVMonitor{vmax} oa := &model.RandomAssigner{rp.Data.Rows, 2.0 / 3.0} tt := oa.Assign() results := model.RunGpsFull(rp, tt, dv, rc, pc, mon.Continue) fmt.Println(results) }
func RunCancerCrossVal(rp *model.RegressionProblem) { vmax := 1.65 rc := &model.LinearRegressionRisk{} pc := func() model.PenaltyCalculator { if true { return model.NewElasticNetFamily(1, len(rp.ColumnNames)) } else { return model.NewLassoPenalty(len(rp.ColumnNames)) } }() oa := &model.RandomAssigner{rp.Data.Rows, 2.0 / 3.0} mf := func() model.Continue { return (&model.FixedVMonitor{vmax}).Continue } m := newMon(vmax, rp) defer os.Remove(m.csv) var bestV float64 fd := func(oos gmath.Function) model.Continue { m.oos = oos bestV = func() float64 { x0, x1 := oos.Domain() min := x0 di := 0.03 for i := x0; i < x1; i += di { if v := oos.Eval(i); v < oos.Eval(min) { min = i } } // now back off up to 5% worse tol := 1.05 * oos.Eval(min) for i := min; i >= x0; i -= di { if v := oos.Eval(i); v < tol { min = i } } return min }() return m.Continue } cv := model.RunCrossVal(10, 0.001, rp, rc, pc, mf, oa, fd) fmt.Printf("%v\n", cv.GpsResults) m.Close() r := "/tmp/" + uuid.New() + ".r" defer os.Remove(r) offset := 0.0 factor := 0.5 ioutil.WriteFile(r, []byte(rscript(time.Now().UTC().Format("2006-01-02T15:04:05Z"), m.csv, bestV, m.min, m.max, offset, factor)), os.ModePerm) sh := "/tmp/" + uuid.New() + ".sh" defer os.Remove(sh) ioutil.WriteFile(sh, []byte(shellScript(r)), os.ModePerm) os.Chmod(sh, 0777) check(runCommand(sh)) cmd := exec.Command("evince", "Rplots.pdf") cmd.Start() }