# lfplot

## PURPOSE

Plot (for one or two dimensions) a locfit() fit.

## SYNOPSIS

function lfplot(varargin)

## DESCRIPTION

     Plot (for one or two dimensions) a locfit() fit.

     Usage:
       fit = locfit(x,y);
       lfplot(fit);

     Plot the fitted smooth curve, and add a scatterplot of the data.

     Required argument:
       fit  (produced by locfit()).

     Optional arguments:
       'nodata'  - don't add data to plot.
       'contour' - for 2-d predictors, use contour instead of surf.
       'direct'  - fit directly, instead of using interpolation
                   (see the predict() function).
       'what'    - locfit what argument ('coef', 'infl', 'vari', 'band' etc).
       Any additional arguments are passed to Matlab's plot(), contour()
         or surf() function as appropriate.

     To add confidence bands, use the lfband() function.

     Author: Catherine Loader.

## CROSS-REFERENCE INFORMATION

This function calls:

- invlink inverse link function for locfit.
- lfmarg computes grid margins from a locfit object, used for plotting.
- plotbyfactor
- predict Interpolate a fit produced by locfit().

This function is called by:

- fig11_1 Local Regression and Likelihood, Figure 11.1
- fig11_4 Local Regression and Likelihood, Figure 11.4.
- fig11_5 Local Regression and Likelihood, Figure 11.5.
- fig1_1 Local Regression and Likelihood, Figure 1.1.
- fig2_2 Local Regression and Likelihood, Figure 2.2.
- fig2_3 Local Regression and Likelihood, Figure 2.3.
- fig2_4 Local Regression and Likelihood, Figure 2.4.
- fig2_5 Local Regression and Likelihood, Figure 2.5.
- fig2_6 Local Regression and Likelihood, Figure 2.6.
- fig3_1 Local Regression and Likelihood, Figure 3.1.
- fig4_1 Local Regression and Likelihood, Figure 4.1.
- fig4_2 Local Regression and Likelihood, Figure 4.2.
- fig5_1 Local Regression and Likelihood, Figure 5.1.
- fig5_2 Local Regression and Likelihood, Figure 5.2.
- fig5_3 Local Regression and Likelihood, Figure 5.3.
- fig5_4 Local Regression and Likelihood, Figure 5.4.
- fig5_5 Local Regression and Likelihood, Figure 5.5.
- fig6_2 Local Regression and Likelihood, Figure 6.2.
- fig6_3 Local Regression and Likelihood, Figure 6.3.
- fig6_5 Local Regression and Likelihood, Figure 6.5.
- fig6_7 Local Regression and Likelihood, Figure 6.6.
- fig7_1 Local Regression and Likelihood, Figure 7.1.
- fig7_2 Local Regression and Likelihood, Figure 7.2.
- fig7_3 Local Regression and Likelihood, Figure 7.3.
- fig7_4 Local Regression and Likelihood, Figure 7.4.
- fig8_1 Local Regression and Likelihood, Figure 8.1.
- fig8_3 Local Regression and Likelihood, Figure 8.3.
- fig9_2 Local Regression and Likelihood, Figure 9.2.
- lfex1 Local Estimation a spike firing rate (in spikes per unit time).
- lfex2 Model the success probability of successive trials of a monkey
- lfgui LFGUI M-file for lfgui.fig
- den_jack Function to compute smooth estimates of the mean of x using locfit,

## SOURCE CODE

    0001 function lfplot(varargin)
    0002
    0003 % Plot (for one or two dimensions) a locfit() fit.
    0004 %
    0005 % Usage:
    0006 %   fit = locfit(x,y);
    0007 %   lfplot(fit);
    0008 %
    0009 % Plot the fitted smooth curve, and add a scatterplot of the data.
    0010 %
    0011 % Required argument:
    0012 %   fit  (produced by locfit()).
    0013 %
    0014 % Optional arguments:
    0015 %   'nodata'  - don't add data to plot.
    0016 %   'contour' - for 2-d predictors, use contour instead of surf.
    0017 %   'direct'  - fit directly, instead of using interpolation
    0018 %               (see the predict() function).
    0019 %   'what'    - locfit what argument ('coef', 'infl', 'vari', 'band' etc).
    0020 %   Any additional arguments are passed to Matlab's plot(), contour()
    0021 %     or surf() function as appropriate.
    0022 %
    0023 % To add confidence bands, use the lfband() function.
    0024 %
    0025 % Author: Catherine Loader.
    0026
    0027 fit = varargin{1};
    0028 data = fit.data;
    0029 xdata = data.x;
    0030 n = size(xdata,1);
    0031 d = size(xdata,2);
    0032 fali = fit.fit_points.family_link;
    0033 ydata = data.y;
    0034 wdata = data.weights;
    0035 cdata = data.censor;
    0036 if (length(cdata)==1) cdata = zeros(n,1); end;
    0037 showdata = (fit.evaluation_structure.derivative==0);
    0038 ppargs = {};
    0039 plotargs = {};
    0040
    0041 type = 's';
    0042 na = 2;
    0043 while na <= length(varargin)
    0044   inc = 0;
    0045   if (strcmp(varargin{na},'contour'))
    0046     type = 'c';
    0047     inc = 1;
    0048   end;
    0049   if (strcmp(varargin{na},'what'))
    0050     ppargs = {ppargs{:}, 'what', varargin{na+1}};
    0051     showdata = 0;
    0052     inc = 2;
    0053   end;
    0054   if (strcmp(varargin{na},'nodata'))
    0055     showdata = 0;
    0056     inc = 1;
    0057   end;
    0058   if (strcmp(varargin{na},'direct'))
    0059     ppargs = {ppargs{:} 'direct'};
    0060     inc = 1;
    0061   end;
    0062   if (inc==0)
    0063     plotargs = {plotargs{:} varargin{na}};
    0064     inc = 1;
    0065   end;
    0066   na = na+inc;
    0067 end;
    0068
    0069 xfit = lfmarg(fit);
    0070 yfit = predict(fit,xfit,ppargs{:});
    0071 yfit = invlink(yfit,fali);
    0072 fam = mod(fali(1),64);
    0073 if (fam>4)
    0074   ydata = ydata ./ wdata;
    0075 end;
    0076
    0077 if (d==1)
    0078   plot(xfit{1},yfit,plotargs{:});
    0079   if (showdata)
    0080     hold on;
    0081     if (length(ydata)==1) ydata = zeros(n,1); end;
    0082     plotbyfactor(xdata,ydata,cdata);
    0083     hold off;
    0084   end;
    0085 end;
    0086
    0087 if (d==2)
    0088   x1 = xfit{1};
    0089   x2 = xfit{2};
    0090   yfit = reshape(yfit,length(x1),length(x2));
    0091   if (type=='c')
    0092     [C h] = contour(x1,x2,yfit',plotargs{:});
    0093     clabel(C,h);
    0094     if (showdata)
    0095       hold on;
    0096       plotbyfactor(xdata(:,1),xdata(:,2),cdata);
    0097       hold off;
    0098     end;
    0099   else
    0100     surf(x1,x2,yfit',plotargs{:});
    0101   end;
    0102 end;
    0103
    0104 return;