"Fossies" - the Fresh Open Source Software Archive

Member "gretl-2020e/addons/regls/regls_helpers.inp" (11 Aug 2020, 2145 Bytes) of package /linux/misc/gretl-2020e.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Charmm source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "regls_helpers.inp": 2020d_vs_2020e.

    1 # get MSE and R^2
    2 
    3 function matrix regls_get_stats (const series y, const series yhat)
    4    scalar SSR = sum((y - yhat)^2)
    5    scalar SST = sum((y - mean(y))^2)
    6    matrix ret = {SSR/$nobs, 1 - SSR/SST}
    7    cnameset(ret, "MSE R^2")
    8    return ret
    9 end function
   10 
   11 function scalar regls_pc_correct (const series y, const series yhat)
   12    series yr = round(yhat)
   13    return 100 * sum(yr == y) / $nobs
   14 end function
   15 
   16 # get prediction from glmnet, plus non-zero coeff vector
   17 
   18 function series glmnet_pred (matrix *Rb, list X)
   19    matrix sel = Rb .!= 0
   20    Rb = selifr(Rb, sel)
   21    matrix mRX = X
   22    list RX = selifr(mRX', sel)
   23    rnameset(Rb, varnames(RX))
   24    return lincomb(RX, Rb)
   25 end function
   26 
   27 # construct a fixed folds vector for use with glmnet
   28 
   29 function matrix regls_foldvec (int nobs, int nf)
   30    matrix foldvec = zeros(nobs, 1)
   31    scalar fsize = nobs / nf
   32    scalar r = 1
   33    loop i=1..nf -q
   34       foldvec[r:r+fsize-1] = i
   35       r += fsize
   36    endloop
   37    return foldvec
   38 end function
   39 
   40 # print summary statistics when multiple lambda values are used
   41 
   42 function void regls_multiprint (const matrix B, const matrix lambda,
   43                                 const series y, list X,
   44                 const matrix crit[null])
   45 
   46    if exists(crit)
   47       printf "\n       lambda     R^2    sum(|b|)   df   criterion\n"
   48    else
   49       printf "\n       lambda     R^2    sum(|b|)   df\n"
   50    endif
   51    scalar haveconst = 0
   52    if rows(B) == nelem(X) + 1
   53       haveconst = 1
   54    elif rows(B) != nelem(X)
   55       funcerr "B and X are not conformable"
   56    endif
   57    loop i=1..cols(B) -q
   58       if haveconst
   59          matrix bi = B[2:,i]
   60      series yhat = lincomb(X, bi) + B[1,i]
   61       else
   62          matrix bi = B[,i]
   63      series yhat = lincomb(X, bi)
   64       endif
   65       scalar l1 = sumc(abs(bi))
   66       scalar df = sumc(bi .!= 0)
   67       m = regls_get_stats(y, yhat)
   68       R2 = m[2]
   69       if R2 < 0 && R2 > -1.0e16
   70          # avoid printing -0.0000
   71          R2 = 0
   72       endif
   73       if exists(crit)
   74          printf "%#13g  %.4f  %#10g  %3d  %#10g\n", lambda[i], R2, l1, df, crit[i]
   75       else
   76          printf "%#13g  %.4f  %#10g  %3d\n", lambda[i], R2, l1, df
   77       endif
   78    endloop
   79    printf "\n"
   80 end function