<?xml version="1.0" encoding="UTF-8"?>
<gretl-functions>
<gretl-function-package name="VCwrapper" needs-time-series-data="true" minver="2021a" lives-in-subdir="true">
<author email="schlicht@lmu.de">Ekkehart Schlicht</author>
<version>1.0</version>
<date>2022-01-12</date>
<description>Time-Varying Coefficients Estimation (VC)</description>
<tags>C32</tags>
<label>Time-varying coefficients (VC)</label>
<menu-attachment>MAINWIN/Model</menu-attachment>
<help>
pdfdoc:VCwrapper.pdf
</help>
<gui-help filename="VCwrapper_help.txt">
VCwrapper(list L) estimates the regression for the first time series in L and takes the following time series as regressors.

VClistplot(list P) plots the list of time series P. P contains pairs of time series: Each time series of an estimated coefficient is followed by the time-series of its standard errors.

VChelp() opens the documentation of VCwrapper in PDF format.
</gui-help>
<data-files count="1">
OkunGer.gdt </data-files>
<gretl-function name="VCwrapper" type="list">
 <params count="3">
  <param name="L" type="list">
<description>Dependent variable and regressors</description>
  </param>
  <param name="vratios" type="string" optional="true">
<description>Initial variance ratios</description>
  </param>
  <param name="VCoptions" type="string" optional="true">
<description>options</description>
  </param>
 </params>
<code>if !exists(vratios)
  matrix VR = ones(nelem(L) - 1)'
  #       VR = 10 .* VR                           # apply default
else
  vratios=strstrip(vratios)
  matrix VR = str2vec(vratios)
endif
if !exists(VCoptions)
  string VCoptions=&quot;&quot;                     # apply default
endif
printf( &quot;The VC program is running. This may take a while ...\n\n&quot;)
flush
string VCtextoutput
# determine to use 64bit or 32bit program
if  $sysinfo.wordlen == 64
  string binpath = $pkgdir ~ &quot;/64bit&quot;	# mirrors package structure
else
  string binpath = $pkgdir ~ &quot;/32bit&quot;	# mirrors package structure
endif
list out = VCexecuter(L,VCoptions, exists(VR) ? VR : {}, binpath, &amp;VCtextoutput)
VCtextoutput = cleanoutput(VCtextoutput)
printf(&quot;\n%s\n\n&quot;,VCtextoutput)
scalar n = nelem(out)/2                     # sort series in pairs of series
list VCresult= {}                           # of coefficients and standard
loop i=1..n                                 # errors
  VCresult += out[i]
  VCresult += out[i+n]
endloop
return VCresult
</code>
</gretl-function>
<gretl-function name="VClistplot" type="void">
 <params count="6">
  <param name="P" type="list">
<description>list of time series</description>
  </param>
  <param name="lwidth" type="string" optional="true">
<description>linewidth</description>
  </param>
  <param name="lcolor" type="string" optional="true">
<description>linecolor (Hex)</description>
  </param>
  <param name="bcolor" type="string" optional="true">
<description>bandcolor (Hex)</description>
  </param>
  <param name="bwidth" type="string" optional="true">
<description>bandwidth</description>
  </param>
  <param name="intens" type="string" optional="true">
<description>intensity ⋲ [.0, 1.] </description>
  </param>
 </params>
<code>#
#### Plot defaults ########################################
string defaultlinewidth = &quot;3&quot;
string defaultcolor = &quot;32a852&quot;
string defaultintensity = &quot;.1&quot;
string defaultbandwidth = &quot;1.96&quot;
###########################################################
if !exists(lwidth)
  string lwidth = defaultlinewidth
endif
if !exists(lcolor)
  string lcolor = defaultcolor
endif
if !exists(bcolor)
  string bcolor = lcolor
endif
if !exists(bwidth)
  string bwidth = defaultbandwidth
endif
if !exists(intens)
  string intens=defaultintensity
endif
scalar ncoeffs = nelem(P)
ncoeffs = ncoeffs / 2             # number of coefficients
loop i=1..ncoeffs                 # bandplots for all coefficients
  VCplot(P[2*i-1],P[2*i],varname(P[2*i-1]),lwidth,lcolor,bcolor,bwidth,intens)
endloop
</code>
</gretl-function>
<gretl-function name="VChelp" type="void">
<code>string VCdir = &quot;@dotdir&quot; ~ &quot;/functions/VCwrapper&quot;
if $sysinfo.os==&quot;windows&quot;
  if $sysinfo.wordlen==64
    string command = &quot;@VCdir&quot; ~ &quot;/64bit/VChelp.exe&quot;
  else
    string command = &quot;@VCdir&quot; ~ &quot;/32bit/VChelp.exe&quot;
  endif
  !  &quot;@command&quot;
else
  string command = &quot;@VCdir&quot; ~ &quot;/VCwrapper.pdf&quot;
  ! xdg-open &quot;@command&quot;
endif
</code>
</gretl-function>
<gretl-function name="VCplot" type="void" private="1">
 <params count="8">
  <param name="coeff" type="series">
<description>coefficient</description>
  </param>
  <param name="stderr" type="series">
<description>standard error</description>
  </param>
  <param name="label" type="string" optional="true">
<description>label</description>
  </param>
  <param name="lwidth" type="string" optional="true">
<description>linewidth</description>
  </param>
  <param name="lcolor" type="string" optional="true">
<description>linecolor</description>
  </param>
  <param name="bcolor" type="string" optional="true">
<description>bandcolor</description>
  </param>
  <param name="bwidth" type="string" optional="true">
<description>bandwidth</description>
  </param>
  <param name="intens" type="string" optional="true">
<description>intensity ⋲ [.0, 1.]</description>
  </param>
 </params>
<code>if !exists(label)
  string label=varname(coeff)
endif
if !exists(lwidth)
  string lwidth = &quot;3&quot;
endif
if !exists(lcolor)
  string lcolor = &quot;0X32a852&quot;
else
  lcolor = &quot;0X&quot; ~lcolor
endif
if !exists(bcolor)
  string bcolor = lcolor
else
  bcolor = &quot;0X&quot; ~bcolor
endif
if !exists(bwidth)
  string bwidth = &quot;1.96&quot;
endif
scalar bw =atof(bwidth)
if !exists(intens)
  string intens=&quot;0.1&quot;
endif
plot coeff
  literal set linetype 1 lw &quot;@lwidth&quot;      # line width
  literal set linetype 1 lc rgb &quot;@lcolor&quot;  # line color
  literal set style fill transparent solid @intens noborder
  # intensity
  literal set border 4095                  # border type
  literal set ylabel &quot;@label&quot;              # label
  options band=coeff,stderr,bw band-style=fill,@bcolor
  # band color
  options time-series with-lines           # line type
  options --single-yaxis                   # all ordinates on one axis
end plot --output=display
</code>
</gretl-function>
<gretl-function name="VCoptions" type="string" private="1">
 <params count="1">
  <param name="VCoptions" type="string" optional="true">
<description>options for VC</description>
  </param>
 </params>
<code>if !exists(VCoptions)
  string VCoptions=&quot;&quot;
endif
return VCoptions
</code>
</gretl-function>
<gretl-function name="str2vec" type="matrix" private="1">
 <params count="1">
  <param name="s" type="string"/>
 </params>
<code># transforms a string with space-separated scalars to a vector.
strings se=strsplit(s,&quot; &quot;)
matrix V={}
loop i=1..nelem(se)
  if !(atof(se[i])^2 &gt;= 0 )
    funcerr &quot;Some variance ratio is not a number. Please correct.&quot;
  endif
  V = V ~ atof(se[i])
endloop
return V
</code>
</gretl-function>
<gretl-function name="cleanoutput" type="string" private="1">
 <params count="1">
  <param name="s" type="string" const="true"/>
 </params>
<code># The problem is that the VCtextoutput.txt file
# has weird line endings (CR, seemingly MacOS 9)
# So we cannot directly split on proper newlines.
string out = &quot;&quot;
# First check whether anything comes before the &quot;Iteration&quot;
string temp = strsplit(s, &quot;Iteration&quot;)[1]
out += strlen(temp) ? temp : &quot;&quot;
# Next, skip all the Iteration parts and jump to the summary
out += strstr(s, &quot;Accuracy&quot;)
# Discard the file saving echo message at the end...
out = strsplit(out, &quot;Output saved&quot;)[1]
return out
</code>
</gretl-function>
<gretl-function name="fwdsl" type="string" private="1">
 <params count="1">
  <param name="in" type="string"/>
 </params>
<code># replace backward with forward slashes
return strsub(in, sprintf(&quot;\\&quot;), &quot;/&quot;)
</code>
</gretl-function>
<gretl-function name="L_VR_errorchk" type="void" private="1">
 <params count="2">
  <param name="L" type="list"/>
  <param name="VR" type="matrix"/>
 </params>
<code># checks if number of regressors == number of variance ratios
errorif( rows(VR) &gt; 1 &amp;&amp; cols(VR) &gt; 1, &quot;VR input must be vector&quot;)
errorif( nelem(L) != nelem(VR) + 1, &quot;need same number of VRs as regressors&quot;)
</code>
</gretl-function>
<gretl-function name="storeVCinput" type="void" private="1">
 <params count="3">
  <param name="L" type="list"/>
  <param name="VR" type="matrix"/>
  <param name="fname" type="string" optional="true"/>
 </params>
<code># stores the initial varianve ratios and the list comprising the
# dependent variable and the regressors in a CSV file
# &quot;from_gretl_to_VC.csv&quot; that can be  processed by the VCG program.
# Initial variance ratios are given in the second row before
# the observations.
if exists(fname)
  string fn = strstrip(fname)
  scalar lenfn =strlen(fn)
  string endfn = substr(fn,lenfn-3,lenfn)
  if endfn != &quot;.csv&quot;
    funcerr &quot;file must have csv extension&quot;
  endif
elif !exists(fname)
  string fname = &quot;from_gretl_to_VC.csv&quot;
endif
# start with the fake obs
matrix m = 999 ~ 666 ~ vec(VR)'
# actual data
m |= {$obsdate} ~ {L}
cnameset(m, defarray(&quot;date&quot;) + varnames(L))
store &quot;@fname&quot; --matrix=m  # no quiet option possible!
</code>
</gretl-function>
<gretl-function name="checkshell" type="scalar" private="1">
<code>string setbuf
outfile --buffer=setbuf
  set
end outfile
strings setparts = strsplit(setbuf, &quot;\n&quot;)
return nelem(instrings(setparts, &quot; shell_ok = 1&quot;)) # mind the leading blank!
</code>
</gretl-function>
<gretl-function name="VCexecuter" type="list" private="1">
 <params count="5">
  <param name="L" type="list" const="true"/>
  <param name="VCoptions" type="string"/>
  <param name="VR" type="matrix" optional="true"/>
  <param name="binpath" type="string" optional="true"/>
  <param name="textout" type="stringref" optional="true"/>
 </params>
<code>binpath =  strsub(binpath, sprintf(&quot;\\&quot;), &quot;/&quot;)
string path = binpath
string VCdir = strsub($dotdir, sprintf(&quot;\\&quot;), &quot;/&quot;) ~ &quot;/functions/VCwrapper&quot;
string command = &quot;&quot;
# Path refers to the location of vcc[.exe]
# We work with absolute paths since the shell call always refers
# to the workdir, which could be anything.
# For temp files we use the dotdir.
#
# VR are the user-settable variance ratios
errorif( !checkshell(), &quot;shell cmd execution must be enabled in gretl&quot;)
errorif( instring(&quot;osx macos&quot;, $sysinfo.os), &quot;Sorry, no Mac VC executable available&quot;)
errorif( !nelem(L), &quot;need some list members&quot;)
errorif( $windows &amp;&amp; $version &lt; 20212, &quot;Need gretl 2021c or higher on Windows&quot;)
# (Because the shell command argument passing didn't work before.)
if !exists(VR) || !nelem(VR)	# apply default
  matrix VR = ones(nelem(L) - 1)'
  #      VR = 10 .* VR
endif
L_VR_errorchk(L, VR)
# use fwd slashes throughout
string dotdir_fwdslash = fwdsl($dotdir) ~ &quot;/&quot;
# hardcoded temp name
string datafbasename = &quot;gretlVCwrapper&quot;
string dataf   = dotdir_fwdslash ~ datafbasename ~ &quot;.csv&quot;
string resultf = dotdir_fwdslash ~ datafbasename ~ &quot;-VCG.csv&quot; # as per vcg's behavior
# process given prefix (path location)
string path = exists(path) ? fwdsl(path) ~ &quot;/&quot; : &quot;&quot;
path ~= ($windows ? &quot;vcg.exe&quot; : &quot;vcg&quot;)
if $sysinfo.os != &quot;windows&quot;
  ! chmod +x @path
endif
smpl --no-missing L
storeVCinput(L, VR, dataf)
# the call
string stdoutf = dotdir_fwdslash ~ &quot;VCtextoutput.txt&quot;
!  &quot;@path&quot; &quot;@dataf&quot; @VCoptions &gt;  &quot;@stdoutf&quot;
# get the result
append &quot;@resultf&quot; --quiet	# although quiet not working...!
if exists(textout)
  textout = readfile(stdoutf)
endif
return coeff_* stderr_* # hardcoded prefixes from VC
</code>
</gretl-function>
<sample-script>
# sample script for VCwrapper. This opens the example data file and stores it
# in Gretl's working directory

	include VCwrapper.gfn 
	open OkunGer.gdt
    store OkunGerExampleFile.gdt

# This creates a list of the relevant time series -- the time series of the 
# dependent variable first, followed by the time series of the regressors:
    
	list L   = dU gGDP intercept

# This calls the program and gives the list of estimated time seies of
# the regressors along with the time series of their standard errors:

	list VCresult = VCwrapper(L)

# This plots the list:

    VClistplot (VCresult)
</sample-script>
</gretl-function-package>
</gretl-functions>
