lfplot.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. function lfplot(varargin)
  2. % Plot (for one or two dimensions) a locfit() fit.
  3. %
  4. % Usage:
  5. % fit = locfit(x,y);
  6. % lfplot(fit);
  7. %
  8. % Plot the fitted smooth curve, and add a scatterplot of the data.
  9. %
  10. % Required argument:
  11. % fit (produced by locfit()).
  12. %
  13. % Optional arguments:
  14. % 'nodata' - don't add data to plot.
  15. % 'contour' - for 2-d predictors, use contour instead of surf.
  16. % 'direct' - fit directly, instead of using interpolation
  17. % (see the predict() function).
  18. % 'what' - locfit what argument ('coef', 'infl', 'vari', 'band' etc).
  19. % Any additional arguments are passed to Matlab's plot(), contour()
  20. % or surf() function as appropriate.
  21. %
  22. % To add confidence bands, use the lfband() function.
  23. %
  24. % Author: Catherine Loader.
  25. fit = varargin{1};
  26. data = fit.data;
  27. xdata = data.x;
  28. n = size(xdata,1);
  29. d = size(xdata,2);
  30. fali = fit.fit_points.family_link;
  31. ydata = data.y;
  32. wdata = data.weights;
  33. cdata = data.censor;
  34. if (length(cdata)==1) cdata = zeros(n,1); end;
  35. showdata = (fit.evaluation_structure.derivative==0);
  36. ppargs = {};
  37. plotargs = {};
  38. type = 's';
  39. na = 2;
  40. while na <= length(varargin)
  41. inc = 0;
  42. if (strcmp(varargin{na},'contour'))
  43. type = 'c';
  44. inc = 1;
  45. end;
  46. if (strcmp(varargin{na},'what'))
  47. ppargs = {ppargs{:}, 'what', varargin{na+1}};
  48. showdata = 0;
  49. inc = 2;
  50. end;
  51. if (strcmp(varargin{na},'nodata'))
  52. showdata = 0;
  53. inc = 1;
  54. end;
  55. if (strcmp(varargin{na},'direct'))
  56. ppargs = {ppargs{:} 'direct'};
  57. inc = 1;
  58. end;
  59. if (inc==0)
  60. plotargs = {plotargs{:} varargin{na}};
  61. inc = 1;
  62. end;
  63. na = na+inc;
  64. end;
  65. xfit = lfmarg(fit);
  66. yfit = predict(fit,xfit,ppargs{:});
  67. yfit = invlink(yfit,fali);
  68. fam = mod(fali(1),64);
  69. if (fam>4)
  70. ydata = ydata ./ wdata;
  71. end;
  72. if (d==1)
  73. plot(xfit{1},yfit,plotargs{:});
  74. if (showdata)
  75. hold on;
  76. if (length(ydata)==1) ydata = zeros(n,1); end;
  77. plotbyfactor(xdata,ydata,cdata);
  78. hold off;
  79. end;
  80. end;
  81. if (d==2)
  82. x1 = xfit{1};
  83. x2 = xfit{2};
  84. yfit = reshape(yfit,length(x1),length(x2));
  85. if (type=='c')
  86. [C h] = contour(x1,x2,yfit',plotargs{:});
  87. clabel(C,h);
  88. if (showdata)
  89. hold on;
  90. plotbyfactor(xdata(:,1),xdata(:,2),cdata);
  91. hold off;
  92. end;
  93. else
  94. surf(x1,x2,yfit',plotargs{:});
  95. end;
  96. end;
  97. return;