# Source directory was `/home/acgamst/src'.
#
# Existing files will *not* be overwritten unless `c' is specified.
#
# This shar contains:
# length mode name
#   
# 939 rwrwr pav/pav.c
# 1662 rwrwr pav/pav.s
# 976 rwrwr pav/spav.s
# 3336 rwrwr pav/index.html
# 102 rwrwr pav/pav.title
# 1527 rwrwr pav/pav.README
#
# directory
if test ! d 'pav'; then
echo 'x ' 'creating directory' 'pav'
mkdir 'pav'
fi
# pav/pav.README
if test f 'pav/pav.README' ; then
echo 'x ' SKIPPING 'pav/pav.README' '(file already exists)'
else
echo 'x ' extracting 'pav/pav.README' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'pav/pav.README'

pav

X
pav is an implementation of the onedimensional pooladjacentviolators
algorithm for monotone (piecewiseconstant) regression
X

X
included in this release are:
X
pav.README  this file
index.html  more information on pav
X spav.s  the pures implementation of pav (slow)
X pav.s  the swrapper for the c implementation, and
X pav.c  the c implementation of pav
X

X
the function prototype for (s)pav is: function(x, y, dir = "d")
where x is the dependent variable and y the independent variable.
dir specifies the direction of y with respect to x and must be
either "d" for down (y decreases as x increases) or "u" for up
(y increases as x increases).
X

X
to use the pures implementation of pav:
X
simple read spav into s using source and call the function
spav(x,y,dir) with x,y, and dir as above.
X

X
to use the c implementation of pav:
X
1. compile the csource into object code
X cc c pav.c
X should work on most unix systems
2. read the swrapper into s using source
X source("pav.s")
3. call the wrapper function pav
X pav(x,y,dir)
X with x,y, and dir as above.
X
the wrapper function will take care of dynamically
loading the object code on the fly.
X
take a look at index.html for more information.
X

X
pav, version 0.1 (september 1999)
anthony gamst (acgamst@osiris.ucsd.edu)
X
COPYRIGHT NOTICE
X
You may distribute these functions freely as long as you do
so without financial gain, that you acknowledge the source,
and communicate improvements to the author.
X
The author is willing to help with problems. Send email to:
acgamst@osiris.ucsd.edu
X
X
X
X
X
SHAR_EOF
fi
# pav/pav.c
if test f 'pav/pav.c' ; then
echo 'x ' SKIPPING 'pav/pav.c' '(file already exists)'
else
echo 'x ' extracting 'pav/pav.c' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'pav/pav.c'
/* pav (object for dyn.open) version 0.1, September 1999
X created by Anthony Gamst (acgamst@osiris.ucsd.edu) */
X
void flatten ( double *vec, int len )
{ /* flatten a vector */
X int i;
X double m = 0.0;
X for ( i = 0 ; i < len ; i++ ) {
X m += vec[i];
X } /* add up the entries */
X m = m/len; /* the mean */
X for ( i = 0 ; i < len ; i++ ) {
X vec[i] = m;
X } /* replace entries with mean */
} /* ends flatten */
X
/* for decreasing monotone smooths */
void pav ( double *result, int *length )
{ /* the forwardbackward pav algorithm */
X int d, i, j;
X for ( i = 0 ; i < (*length1) ; i++ ) {
X if ( result[i] < result[i+1] ) {
X result[i] = (result[i]+result[i+1])/2.0;
X result[i+1] = result[i];
X for ( j = i1 ; j >= 0 ; j ) {
X if ( result[j] < result[i+1] ) {
X d = (i+1)j+1;
X flatten ( &result[j] , d );
X } else {
X break;
X } /* backflatten */
} }} } /* ends pav */
SHAR_EOF
fi
# pav/pav.s
if test f 'pav/pav.s' ; then
echo 'x ' SKIPPING 'pav/pav.s' '(file already exists)'
else
echo 'x ' extracting 'pav/pav.s' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'pav/pav.s'
# swrapper for pav object, version 0.1, september 1999
# created by anthony gamst (acgamst@osiris.ucsd.edu)
X
# the swrapper takes care of the following:
# checks that x and y are numeric
# checks that dir is valid
# selects the standard order for y
# removes missing values from x and/or y
# orders y with respect to x
# loads and calls the pav object, and
# returns the results
X
pav_function(x, y, dir = "d")
{ # y is the data to be smoothed, x specifies the order, and
X # dir = "u" (up, increasing) or "d" (down, decreasing) specifies
X # the direction. if x is missing, use the sequence 1:length(y)
X
X # check for numeric data
X if ( !is.numeric(y)  !is.numeric(x) ) {
X cat("x (if applicable) and y must both be numeric.\n")
X stop("")
X } # just in case things are out of order
X
X # check that dir is valid
X if ( dir != "u" & dir != "d" ) {
X cat("Error: Invalid direction.\n")
X cat("dir must be either \"d\" (for y decreasing in x)")
X cat(" or \"u\" (for y increasing in x)\n")
X stop("")
X } # picky, should be improved
X
X # if increasing, reorder
X if ( dir == "u" ) y_(1*y) # y increasing in x
X
X # remove any missing observations
X a_x[!is.na(x)]
X b_y[!is.na(x)]
X a_a[!is.na(b)]
X b_b[!is.na(b)]
X
X # order the variables
X b_b[order(a,b)] # handle ties by reversal
X a_sort(a)
X
X # check to see if the pavobject is loaded
X if ( !is.loaded("pav") ) {
X dyn.open("pav.o")
X } # and load it if it's not
X
X # finally, do the averaging ...
X n_length(b)
X b_.C("pav",
X y = as.double(b),
X as.integer(n))$y
X
X # return the answer
X if ( dir == "u" ) b_(1*b) # reorder
X return(list(x=a,y=b))
X
} # endpavwrapper
X
SHAR_EOF
fi
# pav/spav.s
if test f 'pav/spav.s' ; then
echo 'x ' SKIPPING 'pav/spav.s' '(file already exists)'
else
echo 'x ' extracting 'pav/spav.s' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'pav/spav.s'
# pool adjacent violators, version 0.1, september 1999
# created by anthony gamst (acgamst@osiris.ucsd.edu)
# straight S implementation
X
spav_function(x,y,dir="d")
{ # pav in y in direction dir ("u"p or "d"own)
X # with order defined by x
X
X # first remove missing observations
X a_x[!is.na(x)]
X b_y[!is.na(x)]
X a_a[!is.na(b)]
X b_b[!is.na(b)]
X
X # then check direction
X if ( dir == "u" ) b_(1*b)
X
X # then order the variables
X b_b[order(a,1*b)] # handle ties by reversal
X a_sort(a)
X
X # finally, do the averaging ...
X n_length(b)
X for ( i in 1:(n1) ) {
X if ( b[i] < b[i+1] ) {
X # forward
X b[i]_(b[i]+b[i+1])/2
X b[i+1]_b[i]
X # backward
X if ( i != 1 ) {
X for ( j in (i1):1 ) {
X if ( b[j] < b[i+1] ) b[j:(i+1)]_mean(b[j:(i+1)])
X else break # by mon we need go no further back
X }
X } # endbackward
X } # endforward
X }
X
X # and return the answer
X if ( dir == "u" ) b_(1*b)
X return(list(x=a,y=b))
X
} # endspav
X
SHAR_EOF
fi
# pav/index.html
if test f 'pav/index.html' ; then
echo 'x ' SKIPPING 'pav/index.html' '(file already exists)'
else
echo 'x ' extracting 'pav/index.html' '(text)'
sed 's/^X//' << 'SHAR_EOF' > 'pav/index.html'
X
X Directions for Installing and Using PAV
To use the pav regression package in Splus ...

X  Download both
X pav.c and
X pav.s
if you don't already have them.
X
X
X  Compile pav.c to object code
(the command
X cc c pav.c
X should work on most UNIX systems)
X
X
X  Start Splus and readin the wrapper function
(the Scommand
X source("pav.s")
X should work)
X
X
X  Finally, call pav
X from within Splus
(the function will take care of loading
X the object code for you)
X
A short description of what pav does ...
X  pav
X produces a monotone (piecewiseconstant) regression of y on x.
X  If there is no xvariable and y is already in
X (some predetermined) order,
just use
X 1:length(y)
X in place of x. (Both x and y must be numeric.)
The function prototype for the wrapper is
pav(x,y,dir="d")
X  y is the data
X to be smoothed,
X  x is the ordering
X (dependent) variable, and
X  dir specifies the
X direction of the smooth, y
X increasing in x
X (dir = "u") or
X y decreasing in
X x
X (dir = "d")
X
The function returns a list with two components
X  x
X (the ordering variable with missing observations removed), and
X  y
X (the monotone smoothed version of the independent variable,
X with missing observations removed)
X
Here are some examples ...
X # first, example data
X x < runif(100)
X y < (x^2)+rnorm(100,sd=0.2)
X z < (1.3*sqrt(x))+rnorm(100,sd=0.2)
X
X # now, pavexamples
X pav(x,y,dir="u")$y # monotone version of y in xorder
X plot(x,y,pch=1)
X lines(pav(x,y,"u"),lty=2) # a plot of the smooth
X
X sz < z[order(x)]
X tx < 1:length(sz)
X pav(tx,sz,"d")$y # example with "constructed" xvariable
X pav(tx,sz)$y # same as above
X
X plot(x,z)
X lines(pav(x,z))
X pav(x,z) # returns x sorted and a monotone z
X
SHAR_EOF
fi
# cleanup ...
rm fr _sh01113
exit 0