📄 univgui.m
字号:
function univgui(arg)
% UNIVGUI Distribution Shapes - Univariate
%
% This GUI function allows one to explore the univariate distributions in
% the data set. These are the distributions of the columns of the data
% matrix X.
%
% One can call it from the edagui GUI or stand-alone from the command
% line. To call from the command line use
%
% univgui
%
% Exploratory Data Analysis Toolbox, April 2005
% Martinez and Martinez, Exploratory Data Analysis with MATLAB
% CRC Press
% First set up the layout if it does not exist.
flg = findobj('tag','univgui');
if isempty(flg)
% then create the gui
dsulayout
elseif nargin == 0
% bring it forward
figure(flg)
end
if nargin == 0
arg = ' ';
end
if strcmp(arg,'dispbox')
% Display the side-by-side boxplots.
dispbox
elseif strcmp(arg,'disphist')
% Display the histograms of the columns. These will be displayed in a
% matrix-like layout.
disphist
elseif strcmp(arg,'dispqq')
% Display q-q plots of the columns of the data matrix. These will be
% displayed in a matrix-like layout.
dispqq
elseif strcmp(arg,'close')
% in other gui's we will do some housekeeping. With this gui, we do not
% have to do so. Obviously, the user will want to keep the data from
% the loadgui for other applications. That is the purpose.
tg = findobj('tag','univgui');
H = get(tg,'userdata');
if ~isempty(H.plots)
button = questdlg('Closing this GUI will close the associated plot windows.',...
'Closing GUI Warning','OK','Cancel','Cancel');
if strcmp(button,'Cancel')
return
else
close(H.plots)
end
end
delete(tg)
end
%%%%%%%%%%%%%%%%%%%%%%%%% SUB FUNCTIONs %%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dispbox
% This function displays the side-by-side boxplots.
% Get the data matrix. Get the GUI info.
ud = get(0,'userdata');
if isempty(ud.X)
errordlg('You must load up some data first.')
return
end
[n,p] = size(ud.X);
tg = findobj('tag','univgui');
H = get(tg,'userdata');
% Get the chosen type of plot.
boxchoice = get(H.popbox,'value');
% Get the dimensions to plot.
dims = get(H.dimbox,'string');
try
if strcmp('all',lower(dims))
% Wants to plot all dimensions
dim = 1:p;
else
% Wants just a subset - convert to numbers.
eval(['dim = [' dims '];'])
end
catch
errordlg('Data entry error in edit box.','Data Enty Error')
return
end
% Now do the plots based on the choice.
switch boxchoice
case 1
% This is the regular boxplots
hf = figure;
set(hf,'numbertitle','off','name','EDA: Boxplots')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
boxplot(ud.X(:,dim));
case 2
% This is the nothed boxplots
hf = figure;
set(hf,'numbertitle','off','name','EDA: Notched Boxplots')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
boxplot(ud.X(:,dim),1);
case 3
% Box-percentile plot
hf = boxprct(ud.X(:,dim));
set(hf,'numbertitle','off','name','EDA: Box-Percentile Plot')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
case 4
% Histplot
hf = boxp(ud.X(:,dim),'hp');
set(hf,'numbertitle','off','name','EDA: Histplots')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
function disphist
% This function displays the histograms.
% The plots for this choice will depend on whether the user chooses to have
% plot all of the dimensions (shown in a plotmatrix format) or one
% dimension (shown in one axis only).
%
% Get the data matrix. Get the GUI info.
ud = get(0,'userdata');
if isempty(ud.X)
errordlg('You must load up some data first.')
return
end
[n,p] = size(ud.X);
tg = findobj('tag','univgui');
H = get(tg,'userdata');
% Get the chosen type of plot.
histchoice = get(H.pophist,'value');
% Get the dimensions to plot.
dims = get(H.dimhist,'string');
try
if strcmp('all',dims)
% Wants to plot all dimensions
dim = 1:p;
else
% Wants just a subset - convert to numbers.
eval(['dim = [' dims '];'])
end
catch
errordlg('Data entry error in edit box.','Data Enty Error')
return
end
% Now do the plots based on the choice.
pp = length(dim);
% Get the layout of the subplots.
nr = round(sqrt(pp));
nc = ceil(pp/nr);
% Get the bandwidths. Will be a vector of bandwidths - based on number
% wanting to plot.
switch histchoice
case 1
% Use Normal reference rule (really Scott's rule)
sig = std(ud.X(:,dim));
bw = 3.5*sig*n^(-1/3);
hf = figure;
set(hf,'numbertitle','off','name','EDA: Probability Density Histogram - Normal Reference Rule')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
case 2
% Freedman-Diaconis Rule
for i = 1:pp
ti = dim(i);
q = quartiles(ud.X(:,ti));
IQR = q(3) - q(1);
bw(i) = 2*IQR*n^(-1/3);
end
hf = figure;
set(hf,'numbertitle','off','name','EDA: Probability Density Histogram - Freedman/Diaconis Rule')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
case 3
% Sturge's Rule
k = round(1 + log2(n));
rng = max(ud.X(:,dim)) - min(ud.X(:,dim));
bw = rng/k;
hf = figure;
set(hf,'numbertitle','off','name','EDA: Probability Density Histogram - Sturges Rule')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
end
% Now that we have the bandwidths - do the histogram and plots.
for i = 1:pp
subplot(nr,nc,i);
ti = dim(i);
% Am going to cheat and convert bandwidth to number of bins. Then use
% the hist function. Err on the side of too many bins.
rng = max(ud.X(:,ti)) - min(ud.X(:,ti));
k = ceil(rng/bw(i));
[nuk,xk] = hist(ud.X(:,ti),k);
h = xk(2) - xk(1);
bar(xk,nuk/(n*h), 1, 'w')
if isnumeric(ud.varlab)
title(num2str(ud.varlab(ti)));
elseif iscell(ud.varlab)
title(ud.varlab{ti});
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dispqq
% This function displays the q-q plots
% Get the data matrix. Get the GUI info.
ud = get(0,'userdata');
if isempty(ud.X)
errordlg('You must load up some data first.')
return
end
[n,p] = size(ud.X);
tg = findobj('tag','univgui');
H = get(tg,'userdata');
% Get the chosen type of plot.
qqchoice = get(H.popqqt,'value');
% Get the dimensions to plot.
dims = get(H.dimqq,'string');
try
if strcmp('all',dims)
% Wants to plot all dimensions
dim = 1:p;
else
% Wants just a subset - convert to numbers.
eval(['dim = [' dims '];'])
end
catch
errordlg('Data entry error in edit box.','Data Enty Error')
return
end
% Now do the plots based on the choice.
pp = length(dim);
% Get the layout of the subplots.
nr = round(sqrt(pp));
nc = ceil(pp/nr);
% Get the bandwidths. Will be a vector of bandwidths - based on number
% wanting to plot.
% This will generate the random variables to plot against.
% This will be a matrix of values.
randmat = zeros(n,pp);
switch qqchoice
case 1
% Normal distribution
randmat = randn(n,pp);
hf = figure;
set(hf,'numbertitle','off','name','EDA: QQ Plot - Normal Distribution')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
case 2
% Exponential distribution
% First estimate the parameters. Use those in the rng function.
parmhat = expfit(ud.X(:,dim));
for i = 1:pp
ti = dim(i);
randmat(:,i) = exprnd(parmhat(i),n,1);
end
hf = figure;
set(hf,'numbertitle','off','name','EDA: QQ Plot - Exponential Distribution')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
case 3
% Gamma distribution
for i = 1:pp
ti = dim(i);
phat = gamfit(ud.X(:,ti));
randmat(:,i) = gamrnd(phat(1),phat(2),n,1);
end
hf = figure;
set(hf,'numbertitle','off','name','EDA: QQ Plot - Gamma Distribution')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
case 4
% Chi-square distribution
randmat = chi2rnd(n-1,n,pp);
hf = figure;
set(hf,'numbertitle','off','name','EDA: QQ Plot - Chi-Square Distribution')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
case 5
% Lognormal distribution
for i = 1:pp
ti = dim(i);
phat = lognfit(ud.X(:,ti));
randmat(:,i) = lognrnd(phat(1),phat(2),n,1);
end
hf = figure;
set(hf,'numbertitle','off','name','EDA: QQ Plot - Lognormal Distribution')
% Upon figure close, this should delete from the array.
set(hf,'CloseRequestFcn',...
'tg = findobj(''tag'',''univgui''); H = get(tg,''userdata''); H.plots(find(H.plots == gcf)) = []; set(tg,''userdata'',H); delete(gcf)')
H.plots = [H.plots, hf];
set(tg,'userdata',H)
case 6
% Uniform distribution
for i = 1:pp
ti = dim(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -