function scat3dcomp(option);

if nargin == 0,
  option = 1;
  N = 10;
  residT=1;
  rho = 0;
  scatfig = figure('Name', 'Surface in Two Predictor', 'Color', 'Cyan');
  %colormap(repmat([.75 .75 .75], 64,1));
  %colormap('summer');
  colormap('hsv');
else
   scatfig = gcf;
   scat3d = get(scatfig, 'UserData');
   x = scat3d.x;
   y = scat3d.y;
   z = scat3d.z;
   N = scat3d.N;
   residT=scat3d.residT;  
   rho=scat3d.rho;
end;

if option == 10,
   entval = str2num( get(findobj('tag','hmxedit'),'string') );
   if (entval < 5 ) 
      set( findobj('tag','hmxedit'),'string', '5');
      entval = 5;
   elseif (entval > 100)
      set( findobj('tag','hmxedit'),'string', '100');
      entval = 100;
   end;
   N = entval;
   scat3d.N = N;
   option = 1;
end;

if option == 11,
  rhoval = str2num( get(findobj('tag','hmxrho'),'string') );
  if (rhoval < -1 ) 
      set( findobj('tag','hmxrho'),'string', '-1');
      rhoval = -1;
   elseif (rhoval > 1)
      set( findobj('tag','hmxrho'),'string', '1');
      rhoval = 1;
   end;
   rho = rhoval;
   scat3d.rho = rho;
   option = 1;
end;


if option==7,
   residT=1;
   option=1;
elseif option==8,
   residT=2;
   option=1;
end;

if option < 4,
subplot(residT,2,1)
x = normrnd(0,1,N,1);
%y = normrnd(0,1,N,1);
y = rho * ((x - mean(x)) / std(x)) + sqrt(1-rho^2) * normrnd(0,1,N,1);
if option==1,
 z = x*0.5 + y*0.2 + normrnd(0,1,N,1);
 z = z +  abs(min(z));
end;
if option == 2,
 z = x*0.5 + y*0.2 + 1*x.*y + normrnd(0,1,N,1);
 z = z +  abs(min(z));
end;
if option ==3,
 z = .5* x.*x +  .2*y.*y  + normrnd(0,1,N,1);
 z = z +  abs(min(z));
end;
 stem3(x,y,z);
 set(gca,'XLim', [-3, 3], 'YLim', [-3, 3],'ZLim', [0, max(z)])
 xlabel('x')
 ylabel('y')
 zlabel('z')
end;

subplot(residT,2,2)
plot3(x,y,z,'bo');
set(gca,'XLim', [-3, 3], 'YLim', [-3, 3],'ZLim', [-.1*range(z), max(z)+(.1*range(z))]);
 xlabel('x')
 ylabel('y')
 zlabel('z')
grid on;


[k l ] = meshgrid(x,y);
k1 = -2.5:.1:2.5;
l1 = -2.5:.1:2.5;
[k2 l2] = meshgrid(k1,l1);

if option == 4,
 [b, bint, r, rint, stats] = regress(z,[ones(N,1) x y], .05);
 hold on
 zpred2 = b(1) + b(2)*x +  b(3)*y;
 line([x x]', [y y]', [z zpred2]');

 zpred3 = b(1) + b(2)*k2 +  b(3)*l2;
 surf(k2, l2, zpred3), shading interp
 title(sprintf('lin r^2=%.3f', [stats(1) ]))
end;

if option ==5,
 [b, bint, r, rint, stats] = regress(scat3d.z,[ones(N,1) x y x.*y], .05);
 hold on
 zpred2 = b(1) + b(2)*x +  b(3)*y + b(4)*x.*y;
 line([x x]', [y y]', [z zpred2]');

 zpred3 = b(1) + b(2)*k2 +  b(3)*l2 + b(4)*k2.*l2;
 surf(k2, l2, zpred3), shading interp
 title(sprintf('int r^2=%.3f', [stats(1) ]))
end;

if option ==6,
 [b, bint, r, rint, stats] = regress(z,[ones(N,1) x y x.*x y.*y], .05);
 hold on
 zpred2 = b(1) + b(2)*x + b(3)*x + b(4)*x.*x +  b(5)*y.*y;
 line([x x]', [y y]', [z zpred2]');

 zpred3 = b(1) + b(2)*k2 + b(3)*l2 + b(4)*k2.*k2 +  b(5)*l2.*l2;
 surf(k2, l2, zpred3), shading interp
 title(sprintf('lin r^2=%.3f', [stats(1) ]))

end;

  grid on;
  hold off;
  rotate3d on;

if residT==2,
subplot(residT,2,3)
plot(x,zpred2-z,'bo');
set(gca,'XLim', [-3, 3], 'YLim', [-3, 3]);
 xlabel('x')
 ylabel('resid')
grid on;
subplot(residT,2,4)
plot(y,zpred2-z,'bo');
set(gca,'XLim', [-3, 3], 'YLim', [-3, 3]);
 xlabel('y')
 ylabel('resid')
grid on;
end;

scat3d.x = x;
scat3d.y = y;
scat3d.z = z; 
scat3d.N = N;
scat3d.residT=residT;
scat3d.rho=rho;
%set(scatfig, 'UserData', scat3d, 'HandleVisibility', 'callback');
set(scatfig, 'UserData', scat3d)


