/* Piecewise Linear & Polynomial OLS Overfit eliminating some points */ /* Data from Holton(2003)"Value-at-Risk" */ new; cls; let data[10,2]= 1.1 2.14 1.4 2.60 2.5 1.15 2.7 1.19 3.2 1.88 3.6 1.55 4.1 2.65 4.3 3.80 4.5 4.46 4.9 6.35 ; x=data[.,1]; y=data[.,2]; order={5,1,1}; /* You could try order=1 or order={1,1,1} instead. */ location={1 8, 1 3, 4 8}; eliminate={1,2}; call piecewise1(y,x,order,location,eliminate); proc piecewise1(y,x,order,location,eliminate); local n,z,xall,yall,eindex,x1,i,j,beta,xx,xs,points,npiece,xp,yp; /* sort them out in terms of x */ z=x~y; z=sortc(z,1); x=z[.,1]; y=z[.,2]; /* elimination */ if maxc(eliminate)>rows(x); errorlog "ERROR: Out of range."; retp("."); elseif eliminate==0 or eliminate==zeros(rows(eliminate),1); errorlog "No elimination:"; xall=x; yall=y; else; xall=x; yall=y; eindex=zeros(rows(x),1); eindex[eliminate]=ones(rows(eliminate),1); x=delif(x,eindex); y=delif(y,eindex); endif; n=rows(x); /* main */ if maxc(maxc(location))>n or minc(minc(location))<1; errorlog "ERROR: Location index is out of range. Consider the elimination."; retp("."); endif; if not location[.,1]