# regls plotting functions function void regls_fcast_plot (const series y, const series yhat, const bundle b) string tmpname scalar x0 = min(yhat) scalar x1 = max(yhat) scalar xd = 0.025 * (x1 - x0) x0 = (x0 >= 0 && x0 - xd < 0)? 0 : x0 - xd x1 += xd set force_decpoint on outfile --tempfile=tmpname --quiet printf "set title 'actual vs predicted %s (%s)'\n", b.depvar, b.estimator printf "set xlabel 'predicted'\n" printf "set ylabel 'actual'\n" printf "set key left top\n" printf "set xrange [%g:%g]\n", x0, x1 printf "plot '-' using 1:2 title '' w p, \\\n" printf " x title 'actual = predicted' w l\n" print yhat y --data-only printf "e\n" end outfile set force_decpoint off gnuplot --input="@tmpname" --output=display remove(tmpname) end function function void regls_mse_plot (const bundle b, const string output) matrix M = b.crit[,1] ~ b.lfrac matrix B = b.crit scalar ymin = 0.95 * minc(b.crit[,1] - b.crit[,2]) scalar ymin = ymin < 0 ? 0 : ymin scalar x0 = b.lfrac[b.idxmin] scalar y0 = M[b.idxmin,1] string lspec0 = "front center point pt 8 ps 2" scalar x1 = b.lfrac[b.idx1se] scalar y1 = M[b.idx1se,1] string lspec1 = "front center point pt 6 ps 2" plot M options with-lp band=B band-style=dash fit=none literal set termoption enhanced printf "set yrange [%g:1]", ymin literal set xlabel 's = λ/λ_{max}' literal set ylabel 'MSE' printf "set xrange [1:%g]", minc(b.lfrac) literal set logscale x printf "set label \"\" at %g,%g %s", x0, y0, lspec0 printf "set label \"\" at %g,%g %s", x1, y1, lspec1 literal set title 'Mean cross-validation MSE with one-s.e. band' end plot --output="@output" end function function void regls_bic_plot (const bundle b, const string output) matrix M = b.BIC ~ b.lfrac scalar ymin = minc(b.BIC) scalar ymax = maxc(b.BIC) scalar ymargin = 0.05 * (ymax - ymin) ymin -= ymargin ymax += ymargin scalar x0 = b.lfrac[b.idxmin] scalar y0 = b.BIC[b.idxmin] string lspec = "front center point pt 8 ps 2" plot M options with-lp fit=none literal set termoption enhanced printf "set xrange [1:%g]", minc(b.lfrac) printf "set yrange [%g:%g]", ymin, ymax literal set xlabel 's = λ/λ_{max}' literal set ylabel 'BIC' literal set logscale x printf "set label \"\" at %g,%g %s", x0, y0, lspec literal set title 'BIC as a function of s' end plot --output="@output" end function function matrix regls_list2sel (const list X, const list L) matrix m1 = X # will not include const matrix m2 = L # may include const if m2[1] == 0 m2 = m2[2:] endif n1 = nelem(m1) return selifc(seq(1,n1), contains(m1,m2)) end function function void real_regls_coeff_plot (const bundle b, const matrix sel[null], const string output) if cols(b.B) == 1 funcerr "Must have more than one lambda value" endif strings colheads = rnameget(b.B) matrix B if b.stdize colheads = colheads[2:] B = b.B[2:,] else matrix B = b.B endif if exists(sel) colheads = colheads[sel] matrix B = B[sel,] endif colheads += "lfrac" matrix Bs = B' ~ b.lfrac cnameset(Bs, colheads) scalar x0 = b.lfrac[b.idxmin] string est = b.ridge ? "Ridge" : "LASSO" plot Bs options with-lp single-yaxis fit=none literal set termoption enhanced literal set xlabel 's = λ/λ_{max}' literal set key outside right noenhanced printf "set xrange [1:%g]", minc(b.lfrac) literal set logscale x literal set style arrow 1 nohead dt 2 printf "set arrow 1 from %g, graph 0 to %g, graph 1", x0, x0 literal set arrow 1 as 1 printf "set title '%s coefficients as a function of s'", est end plot --output="@output" end function function void regls_coeff_plot (const bundle b, const list L[null], const string fname[null]) string output = exists(fname) ? fname : "display" if exists(L) && nelem(L) > 0 matrix sel = regls_list2sel(b.xlist, L) real_regls_coeff_plot(b, sel, output) else real_regls_coeff_plot(b, null, output) endif end function function void mregls_coeff_plot (const bundle b, const matrix sel[null], const string fname[null]) string output = exists(fname) ? fname : "display" real_regls_coeff_plot(b, sel, output) end function function void regls_criterion_plot (const bundle b, const string fname[null]) if cols(b.B) == 1 funcerr "Must have more than one lambda value" endif string output = exists(fname) ? fname : "display" if inbundle(b, "xvalidate") && b.xvalidate regls_mse_plot(b, output) else regls_bic_plot(b, output) endif end function function void regls_bundle_plot (const bundle *b, int ptype[0:1:0] {"Criterion plot", "Coefficient paths"}, list sub[null]) # for GUI use only, so for regls, not mregls if ptype == 0 regls_criterion_plot(b) else if (exists(sub) && nelem(sub) > 0 && nelem(sub) < 26) regls_coeff_plot(b, sub) else # try not to show more than 25 paths sel = regls_list2sel(b.xlist, b.nzX) real_regls_coeff_plot(b, sel) endif endif end function function matrix regls_plot_check (const bundle *b) if !inbundle(b, "B") return {0} endif # both plots require more than one regularization parameter return cols(b.B) > 1 ? {1,1} : {0,0} end function