/****************************************************************/ /* S A S S A M P L E L I B R A R Y */ /* */ /* NAME: TREGLS1 */ /* TITLE: Linear And Nonlinear Regression Functions */ /* PRODUCT: STAT */ /* SYSTEM: ALL */ /* KEYS: regression analysis, transformations */ /* PROCS: TRANSREG */ /* DATA: */ /* */ /* REF: PROC TRANSREG, LEAST SQUARES EXAMPLE 1 */ /* MISC: YOU MUST LICENSE SAS/IML SOFTWARE TO RUN PROC IML. */ /* */ /* THIS EXAMPLE HAS BEEN MODIFIED SINCE IT APPEARED */ /* IN THE RELEASE 6.06 SAS/STAT USER'S GUIDE */ /* */ /****************************************************************/ title 'Generate Plots for the Nonlinear Regression Function Example'; *---Generate an Artificial Nonlinear Scatterplot ---; *---SAS/IML Software is Required for this Example---; proc iml; n = 500; x = (1:n)`; x = x/(n/200); y = -((x/50)-1.5)##2 + sin(x/8) + sqrt(x)/5 + 2*log(x) + cos(x); x = x - x[:,]; x = -x / sqrt(x[##,]/(n-1)); y = y - y[:,]; y = y / sqrt(y[##,]/(n-1)); all = y || x; create outset from all; append from all; quit; data a; set outset(rename=(col1=y col2=x)); if y<-2 then y=-2 + ranuni(7654321)/2; x1=x; x2=x; x3=x; x4=x; run; *---Predicted Values for the Linear Regression Line---; proc transreg data=a; title 'A Linear Regression Line'; model linear(y)=linear(x); output out=a approximations; id x1-x4; run; *---Predicted Values for the Monotone Regression Function---; proc transreg data=a(rename=(ay=ly)); title 'A Monotone Regression Function'; model linear(y)=mspline(x / nknots=9); output out=a approximations; id x1-x4 ly; run; *---Predicted Values for the Nonmonotone Regression Function---; proc transreg data=a(rename=(ay=my)); title 'A Nonmonotone Regression Function'; model linear(y)=spline(x / nknots=9); output out=a approximations; id x1-x4 ly my; run; *---Plot the Results---; proc plot data=a vpercent=50 hpercent=50 nolegend; title 'Linear, Monotone, and Nonmonotone Regression Functions'; plot y*x1='*' / box haxis=-2 to 2 by 2 vaxis=-2 to 2 by 2; plot ly*x2='r' y*x2='*' / box overlay haxis=-2 to 2 by 2 vaxis=-2 to 2 by 2; plot my*x3='r' y*x3='*' / box overlay haxis=-2 to 2 by 2 vaxis=-2 to 2 by 2; plot ay*x4='r' y*x4='*' / box overlay haxis=-2 to 2 by 2 vaxis=-2 to 2 by 2; label ly = 'Linear Regression' my = 'Monotone Regression' ay = 'Nonmonotone Regression' x1 = 'Nonlinear Scatterplot' x2 = 'r**2 = 0.14580' x3 = 'r**2 = 0.60576' x4 = 'r**2 = 0.89634'; run;