Code covered by the BSD License

# Introduction to Statistics Toolbox - July 2007 Webinar Files

### Jiro Doke (view profile)

17 Jul 2007 (Updated )

Presentation and demo files for the "Introduction to Statistics Toolboxâ„˘" Webinar (July 12, 2007)

myDistFit(y,arg_2)
```function myDistFit(y,arg_2)
%MYDISTFIT    Create plot of datasets and fits
%   MYDISTFIT(Y,ARG_2)
%   Creates a plot, similar to the plot in the main distribution fitting
%   window, using the data that you provide as input.  You can
%   apply this function to the same data you used with dfittool
%   or with different data.  You may want to edit the function to
%   customize the code and this help message.
%
%   Number of datasets:  2
%   Number of fits:  2

% Copyright 2007 The MathWorks, Inc.

% This function was automatically generated on 10-Jul-2007 20:37:45

% Data from dataset "Car":
%    Y = y

% Data from dataset "Truck":
%    Y = arg_2 (originally data.mpg(data.C_H == 'H' & data.car_truck == 'T'))

% Force all inputs to be column vectors
y = y(:);
arg_2 = arg_2(:);

% Set up figure to receive datasets and fits
f_ = clf;
figure(f_);
set(f_,'Units','Pixels','Position',[522 518 680 468.45]);
legh_ = []; legt_ = {};   % handles and text for legend
ax_ = newplot;
set(ax_,'Box','on');
hold on;

% --- Plot data originally in dataset "Car"
t_ = ~isnan(y);
Data_ = y(t_);
[F_,X_] = ecdf(Data_,'Function','cdf'...
);  % compute empirical cdf
Bin_.rule = 1;
[C_,E_] = dfswitchyard('dfhistbins',Data_,[],[],Bin_,F_,X_);
[N_,C_] = ecdfhist(F_,X_,'edges',E_); % empirical pdf from cdf
h_ = bar(C_,N_,'hist');
set(h_,'FaceColor','none','EdgeColor',[0.333333 0 0.666667],...
'LineStyle','-', 'LineWidth',1);
xlabel('Data');
ylabel('Density')
legh_(end+1) = h_;
legt_{end+1} = 'Car';
% --- Plot data originally in dataset "Truck"
t_ = ~isnan(arg_2);
Data_ = arg_2(t_);
[F_,X_] = ecdf(Data_,'Function','cdf'...
);  % compute empirical cdf
Bin_.rule = 1;
[C_,E_] = dfswitchyard('dfhistbins',Data_,[],[],Bin_,F_,X_);
[N_,C_] = ecdfhist(F_,X_,'edges',E_); % empirical pdf from cdf
h_ = bar(C_,N_,'hist');
set(h_,'FaceColor','none','EdgeColor',[0.333333 0.666667 0],...
'LineStyle','-', 'LineWidth',1);
xlabel('Data');
ylabel('Density')
legh_(end+1) = h_;
legt_{end+1} = 'Truck';

% Nudge axis limits beyond data limits
xlim_ = get(ax_,'XLim');
if all(isfinite(xlim_))
xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
set(ax_,'XLim',xlim_)
end

x_ = linspace(xlim_(1),xlim_(2),100);

% --- Create fit "My Fit (Car)"

% Fit this distribution to get parameter values
t_ = ~isnan(y);
Data_ = y(t_);
% To use parameter estimates from the original fit:
%     p_ = [ 35.51946551774, 3.630660553248];
p_ = mle(Data_, 'dist','logistic', 'alpha',0.05);  % Fit Logistic distribution
y_ = pdf('logistic',x_,p_(1), p_(2));
h_ = plot(x_,y_,'Color',[1 0 0],...
'LineStyle','-', 'LineWidth',2,...
'Marker','none', 'MarkerSize',6);
legh_(end+1) = h_;
legt_{end+1} = 'My Fit (Car)';

% --- Create fit "My Fit (Truck)"

% Fit this distribution to get parameter values
t_ = ~isnan(arg_2);
Data_ = arg_2(t_);
% To use parameter estimates from the original fit:
%     p_ = [ 26.74753801238, 2.567781867539];
p_ = mle(Data_, 'dist','logistic', 'alpha',0.05);  % Fit Logistic distribution
y_ = pdf('logistic',x_,p_(1), p_(2));
h_ = plot(x_,y_,'Color',[0 0 1],...
'LineStyle','-', 'LineWidth',2,...
'Marker','none', 'MarkerSize',6);
legh_(end+1) = h_;
legt_{end+1} = 'My Fit (Truck)';

hold off;
leginfo_ = {'Orientation', 'vertical', 'Location', 'NorthEast'};
h_ = legend(ax_,legh_,legt_,leginfo_{:});  % create legend
set(h_,'Interpreter','none');
```