-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstdSemTutorial.m
More file actions
63 lines (54 loc) · 1.7 KB
/
Copy pathstdSemTutorial.m
File metadata and controls
63 lines (54 loc) · 1.7 KB
1
% stdSemTutorial%% Illustrate relation between standard deviation and SEM.%% 9/15/15 dhb Added some comments.%% Clearclear; close all;%% Set parametersnDraw = 6000;drawMean = 75;drawSD = 10;nBins = 30;% Draw a large number of instances from a normal distribution% and make a histogramtheDraw = normrnd(drawMean,drawSD,nDraw,1);[nPerBin,bins] = hist(theDraw,nBins);binWidth = bins(2)-bins(1);figure; clf; hold onbar(bins,nPerBin);% Generate theoretical fit. The histogram should follow% a normal distribution closelytPoints = bins(1)-binWidth:binWidth/4:bins(nBins)+binWidth;tCurve = binWidth*nDraw*normpdf(tPoints,drawMean,drawSD);plot(tPoints,tCurve,'r');% Generate empirical fit based on sample mean and sample% standard deviation. This should also be close to the% histogram.ßempMean = mean(theDraw);empSD = std(theDraw);eCurve = binWidth*nDraw*normpdf(tPoints,empMean,empSD);plot(tPoints,eCurve,'g');drawnow;%% Generate histogram of means of nDrawsnMeans = 1000;nDraw = 10;theMeans = zeros(nMeans,1);for i = 1:nMeans theMeans(i) = mean(normrnd(drawMean,drawSD,nDraw,1));end[nPerMeanBin,meanBins] = hist(theMeans,nBins);meanBinWidth = meanBins(2)-meanBins(1);figure; clf; hold onbar(meanBins,nPerMeanBin);% Generate theoretical fit for original underlying normal% and based on scaling standard deviation by square root% of number of draws, to get the population standard error% of measurement.mPoints = bins(1)-binWidth:binWidth/4:bins(nBins)+binWidth;mCurve = meanBinWidth*nMeans*normpdf(mPoints,drawMean,drawSD);semCurve = meanBinWidth*nMeans*normpdf(mPoints,drawMean,drawSD/sqrt(nDraw));plot(mPoints,mCurve,'r');plot(mPoints,semCurve,'g');axis([20 120 0 100])drawnow