function StatsPractice % Coded by Nigel Reuel on 9.5.2017 % This script shows basic stat functions with Matlab % % First step import data [num, txt, ~] = xlsread('ClassData.xlsx'); m = 10; % One example of univariate data = time it takes to commute Time = num(:,1); AvgTime = mean(Time) MedianTime = median(Time) TimewOutlier = [Time; 300]; AvgTime2 = mean(TimewOutlier) MedianTime2 = median(TimewOutlier) % Use measures of spread StdevTime = std(Time) IQRtime = iqr(Time) % Demo the boxplot boxplot(Time) % Let's show boxplots that are grouped boxplot(Time,txt) % Another way to look at univariate data is the histogram hist(Time) ylabel('Frequency') xlabel('Time (min)') % If you want to specify the bins % Way 1 - number of bins nbins = 5; hist(Time,nbins) ylabel('Frequency') xlabel('Time (min)') % Way 2 - specify bin centers binctr = [0:2:30]; hist(Time,binctr) % You can report out the heights of the bars (counts) count = hist(Time,binctr) % Now let's practice distributions and how to use them % Data for time to failure of an electronic piece life = [ 6.2 16.1 16.3 19.0 12.2 8.1 8.8 5.9 7.3 8.2 ... 16.1 12.8 9.8 11.3 5.1 10.8 6.7 1.2 8.3 2.3 ... 4.3 2.9 14.8 4.6 3.1 13.6 14.5 5.2 5.7 6.5 ... 5.3 6.4 3.5 11.4 9.3 12.4 18.3 15.9 4.0 10.4 ... 8.7 3.0 12.1 3.9 6.5 3.4 8.5 0.9 9.9 7.9]; % First pass we will not define the bins hist(life) ylabel('Frequency') xlabel('Time (min)') % Next let's set our bins binwidth = 2; binctrs = [1:binwidth:19]; counts = hist(life,binctrs) xlabel('Time to Failure') ylabel('Frequency') % Now let's fit the data to a Weibull Distribution paramEst = wblfit(life) % So now we have the parameters for the Weibull Dist (a,b) % To plot the fit distribution, we need to normalize the y axis n = length(life); prob = counts/(n*binwidth); bar(binctrs,prob) X = linspace(0,20,100); Y = wblpdf(X,paramEst(1),paramEst(2)); hold on plot(X,Y,'r') % Let's say your boss wants you to simulate the process, % You can use the model to simulate random outputs OneMeasurment = wblrnd(paramEst(1),paramEst(2)) end