LAB 1: EXERCISE 1 IS THE CODE FOR 3 DIMENSIONAL PLOTS AND FOR FITTING A MODEL FOR THE MEAM TREND EXERCISE 2 IS THE CODE TO PLOT EACH DATUM VERSUS ITS NEAREST NEIGHBORS, EXERCISE 3 IS THE CODE FOR MEDIAN POLISH EXERCISE 4 IS THE CODE FOR SAMPLE SEMIVARIOGRAMS. EXERCISE 5 IS THE CODE TO PUT MULTIPLE GRAPHS IN ONE PAGE SAS EXERCISE 1: --------------- 3 dimensional plots. IN SAS: options nodate nocenter nonumber ps=67 ls=120; data phdat;infile '/ncsu/st733_info/www/soilph.dat'; input y x ph; x2=x**2;y2=y**2;xy=x*y; x3=x**3; x2y= x2*y; xy2=x*y2; y3=y**3; proc reg data=phdat; model ph = x y; title 'first order'; run; proc reg data=phdat; model ph=x y x2 xy y2; output out=fitvals p=predval; title 'second order polynomial'; run; proc reg data=phdat; model ph=x y x2 xy y2 x3 x2y xy2 y3; output out=fitvals p=predval; title 'three order polynomial'; run; proc g3d data= fitvals; plot y*x=predval; title 'Three Dimensional Surface Plot'; run; proc g3d data=phdat; scatter y*x=ph / shape='balloon' caxis=black; title 'Three Dimensional Scatter Plot'; run; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *** SAS OUTPUT first order Model: MODEL1 Dependent Variable: PH Analysis of Variance Sum of Mean Source DF Squares Square F Value Prob>F Model 2 0.27864 0.13932 3.154 0.0463 Error 118 5.21247 0.04417 C Total 120 5.49111 Root MSE 0.21017 R-square 0.0507 Dep Mean 4.48463 Adj R-sq 0.0347 C.V. 4.68656 Parameter Estimates Parameter Standard T for H0: Variable DF Estimate Error Parameter=0 Prob > |T| INTERCEP 1 4.603860 0.05471358 84.145 0.0001 X 1 -0.005884 0.00604210 -0.974 0.3321 Y 1 -0.013988 0.00604210 -2.315 0.0223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% second order polynomial Model: MODEL1 Dependent Variable: PH Analysis of Variance Sum of Mean Source DF Squares Square F Value Prob>F Model 5 1.27645 0.25529 6.966 0.0001 Error 115 4.21466 0.03665 C Total 120 5.49111 Root MSE 0.19144 R-square 0.2325 Dep Mean 4.48463 Adj R-sq 0.1991 C.V. 4.26880 Parameter Estimates Parameter Standard T for H0: Variable DF Estimate Error Parameter=0 Prob > |T| INTERCEP 1 4.197752 0.10797728 38.876 0.0001 X 1 0.026320 0.02642918 0.996 0.3214 Y 1 0.119825 0.02642918 4.534 0.0001 X2 1 -0.000363 0.00197057 -0.184 0.8540 XY 1 -0.004640 0.00174036 -2.666 0.0088 Y2 1 -0.008831 0.00197057 -4.481 0.0001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% three order polynomial Model: MODEL1 Dependent Variable: PH Analysis of Variance Sum of Mean Source DF Squares Square F Value Prob>F Model 9 1.51521 0.16836 4.700 0.0001 Error 111 3.97590 0.03582 C Total 120 5.49111 Root MSE 0.18926 R-square 0.2759 Dep Mean 4.48463 Adj R-sq 0.2172 C.V. 4.22017 Parameter Estimates Parameter Standard T for H0: Variable DF Estimate Error Parameter=0 Prob > |T| INTERCEP 1 3.888879 0.20600480 18.878 0.0001 X 1 0.104742 0.08482466 1.235 0.2195 Y 1 0.277751 0.08482466 3.274 0.0014 X2 1 -0.014081 0.01372009 -1.026 0.3070 XY 1 -0.011945 0.01059535 -1.127 0.2620 Y2 1 -0.034027 0.01372009 -2.480 0.0146 X3 1 0.000891 0.00072602 1.227 0.2226 X2Y 1 -0.000385 0.00061605 -0.626 0.5328 XY2 1 0.000994 0.00061605 1.614 0.1094 Y3 1 0.001068 0.00072602 1.472 0.1440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SAS EXERCISE 2. -------------- /* CODE TO PLOT EACH DATUM VERSUS ITS NEAREST NEIGHBORS FOR: Detecting spatial outliers. DATA: 1 6 12 14 21 7 10 15 18 28 11 15 21 23 29 16 19 23 11 37 20 25 27 33 39 IN SAS:*/ data matval; input row col value; cards; 1 1 1 1 2 6 1 3 12 1 4 14 1 5 21 2 1 7 2 2 10 2 3 15 2 4 18 2 5 28 3 1 11 3 2 15 3 3 21 3 4 23 3 5 29 4 1 16 4 2 19 4 3 23 4 4 11 4 5 37 5 1 20 5 2 25 5 3 27 5 4 33 5 5 39 ; proc iml; use matval; /* defines current data to read from*/ read all var {row col value}; nval=nrow(row); /* number of values in matrix*/ nrow=max(row); /* number of row */ ncol=max(col); /* number of col */ matval=j(nrow,ncol,0); /* initialize matrix */ /* put values in corresponding position in matrix matval */ do i=1 to nval; matval[row[i,1],col[i,1]]=value[i,1]; end; /* create x,y coordinates for plotting */ npoint=nrow*(ncol-1) +ncol*(nrow-1); /* number of pts to plot */ xpnt=j(npoint,1,0); ypnt=j(npoint,1,0); /*initialize x,y */ ipnt=0; do i=1 to nrow; do j=1 to ncol-1; ipnt=ipnt+1; xpnt[ipnt,1]=matval[i,j]; ypnt[ipnt,1]=matval[i,j+1]; end; end; do j=1 to ncol; do i=1 to nrow-1; ipnt=ipnt+1; xpnt[ipnt,1]=matval[i,j]; ypnt[ipnt,1]=matval[i+1,j]; end; end; create plotdata var {xpnt ypnt}; append; quit; proc plot data=plotdata; plot ypnt*xpnt; run; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SAS OUTPUT: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Plot of YPNT*XPNT. Legend: A = 1 obs, B = 2 obs, etc. 40 + B | A (11,37) A | A (11,33) A YPNT | | A A A A | A A A A | B B 20 + ACB | A AA | BA B | B B B (23,11) | | B | 0 + ---+-------------+-------------+-------------+-------------+-- 0 10 20 30 40 XPNT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SAS EXERCISE 3: -------------- Median polish. IN SAS: proc iml; start= {1 6 12 14 21 0, 7 10 15 18 28 0, 11 15 21 23 29 0, 16 19 23 11 37 0, 20 25 27 33 39 0, 0 0 0 0 0 0}; numcol=ncol(start)-1; numrow=nrow(start)-1; tol=0.001; maxit=50; rows=start; iterate=0; do until(converge|(iterate>=maxit)); ****** sweep medians from rows ******; rowmeds=median(rows[,1:numcol]`); /* calc row medians not using last column. */ rowmeds=rowmeds`; /* transpose to column vector. */ rowmmat=J(1,numcol,1)@rowmeds; /* create matrix to sweep from rows. */ rowsweep=(rows[,1:numcol]-rowmmat); /* sweep medians from all but last column. */ lastcol=rows[,numcol+1]+rowmeds; /* add row medians to last column. */ rowdone=rowsweep||lastcol; /* concatenate last column; done with rows.*/ columns=rowdone; /* now pass to column sweep step. */ iterate=iterate+1; ****** sweep medians from columns ******; colmeds=median(columns[1:numrow,]); colmmat=J(numrow,1,1)@colmeds; colsweep=(columns[1:numrow,]-colmmat); lastrow=columns[numrow+1,]+colmeds; coldone=colsweep//lastrow; rows=coldone; iterate=iterate+1; ****** check to see if tolerance met (only on even iteration) ******; diff=rowdone-coldone; convmat=choose(abs(diff)