|
| 1 | +%% Matlab Toolbox for Bayesian Estimation - One Group Example |
| 2 | +% This is an example script for a one group estimation. |
| 3 | + |
| 4 | +% Largely based on R code introduced in the following paper: |
| 5 | +% Kruschke, J.K., Bayesian Estimation supersedes the t-test. |
| 6 | +% Journal of Experimental Psychology: General, Vol 142(2), May 2013, 573-603. |
| 7 | +% see http://www.indiana.edu/~kruschke/BEST/ for R code |
| 8 | + |
| 9 | +% Johann-Wolfgang-Goethe University, Frankfurt |
| 10 | +% Created: 2016-04-25 |
| 11 | +% Version: v1.0 (2016-04-25) |
| 12 | +%------------------------------------------------------------------------- |
| 13 | +clear;clc; |
| 14 | + |
| 15 | +%% Load some data |
| 16 | +% EXAMPLE DATA (see Kruschke, 2013) |
| 17 | +% Run this script one time with the small sample. |
| 18 | +y = [101,100,102,104,102,97,105,105,98,101,100,123,105,103,100,95,102,106,... |
| 19 | + 109,102,82,102,100,102,102,101,102,102,103,103,97,97,103,101,97,104,... |
| 20 | + 96,103,124,101,101,100,101,101,104,100,101]; |
| 21 | +nTotal = length(y); |
| 22 | + |
| 23 | +% % Then run it again but use the n=500 sample below to see the change |
| 24 | +% % of the parameters and HDI. |
| 25 | +% y = trnd(2,500,1); |
| 26 | +% y = (y-mean(y))/sqrt(mean((y-mean(y)).^2))*6+102; |
| 27 | +% nTotal = length(y); |
| 28 | + |
| 29 | +%% Specify prior constants, shape and rate for gamma distribution |
| 30 | +% See Kruschke (2013) for further description |
| 31 | +muM = mean(y); |
| 32 | +muP = 0.000001 * 1/std(y)^2; |
| 33 | +sigmaLow = std(y)/1000; |
| 34 | +sigmaHigh = std(y)*1000; |
| 35 | + |
| 36 | +% Save prior constants in a structure for later use with matjags |
| 37 | +dataList = struct('y',y,'nTotal',nTotal,... |
| 38 | + 'muM',muM,'muP',muP,'sigmaLow',sigmaLow,'sigmaHigh',sigmaHigh); |
| 39 | + |
| 40 | +%% Specify MCMC properties |
| 41 | +% Number of MCMC steps that are saved for EACH chain |
| 42 | +% This is different to Rjags, where you would define the number of |
| 43 | +% steps to be saved for all chains together (in this example 12000) |
| 44 | +numSavedSteps = 30000; |
| 45 | + |
| 46 | +% Number of separate MCMC chains |
| 47 | +nChains = 3; |
| 48 | + |
| 49 | +% Number of steps that are thinned, matjags will only keep every nth |
| 50 | +% step. This does not affect the number of saved steps. I.e. in order |
| 51 | +% to compute 10000 saved steps, matjags/JAGS will compute 50000 steps |
| 52 | +% If memory isn't an issue, Kruschke recommends to use longer chains |
| 53 | +% and no thinning at all. |
| 54 | +thinSteps = 1; |
| 55 | + |
| 56 | +% Number of burn-in samples |
| 57 | +burnInSteps = 1000; |
| 58 | + |
| 59 | +% The parameters that are to be monitored |
| 60 | +parameters = {'mu','sigma','nu'}; |
| 61 | + |
| 62 | +%% Initialize the chain |
| 63 | +% Initial values of MCMC chains based on data: |
| 64 | +mu = mean(y); |
| 65 | +sigma = std(y); |
| 66 | +% Regarding initial values: (1) sigma will tend to be too big if |
| 67 | +% the data have outliers, and (2) nu starts at 5 as a moderate value. These |
| 68 | +% initial values keep the burn-in period moderate. |
| 69 | + |
| 70 | +% Set initial values for latent variable in each chain |
| 71 | +for i=1:nChains |
| 72 | + initsList(i) = struct('mu', mu, 'sigma',sigma,'nu',5); |
| 73 | +end |
| 74 | + |
| 75 | +%% Specify the JAGS model |
| 76 | +% This will write a JAGS model to a text file |
| 77 | +% You can also write the JAGS model directly to a text file |
| 78 | + |
| 79 | +modelString = [' model {\n',... |
| 80 | + ' for ( i in 1:nTotal ) {\n',... |
| 81 | + ' y[i] ~ dt( mu , tau, nu )\n',... |
| 82 | + ' }\n',... |
| 83 | + ' mu ~ dnorm( muM , muP ) \n',... |
| 84 | + ' tau <- 1/pow(sigma , 2)\n',... |
| 85 | + ' sigma ~ dunif( sigmaLow , sigmaHigh )\n',... |
| 86 | + ' nu ~ dexp( 1/30 )\n'... |
| 87 | + '}']; |
| 88 | +fileID = fopen('mbe_1gr_example.txt','wt'); |
| 89 | +fprintf(fileID,modelString); |
| 90 | +fclose(fileID); |
| 91 | +model = fullfile(pwd,'mbe_1gr_example.txt'); |
| 92 | + |
| 93 | +%% Run the chains using matjags and JAGS |
| 94 | +% In case you have the Parallel Computing Toolbox, use ('doParallel',1) |
| 95 | +[~, ~, mcmcChain] = matjags(... |
| 96 | + dataList,... |
| 97 | + model,... |
| 98 | + initsList,... |
| 99 | + 'monitorparams', parameters,... |
| 100 | + 'nChains', nChains,... |
| 101 | + 'nBurnin', burnInSteps,... |
| 102 | + 'thin', thinSteps,... |
| 103 | + 'verbosity',1,... |
| 104 | + 'nSamples',numSavedSteps); |
| 105 | + |
| 106 | +%% Restructure the output |
| 107 | +% This transforms the output of matjags into the format that mbe is |
| 108 | +% using |
| 109 | +mcmcChain = mbe_restructChains(mcmcChain); |
| 110 | + |
| 111 | +%% Examine the chains |
| 112 | +mbe_diagMCMC(mcmcChain); |
| 113 | + |
| 114 | +%% Examine the results |
| 115 | +% At this point, we want to use all the chains at once, so we |
| 116 | +% need to concatenate the individual chains to one long chain first |
| 117 | +mcmcChain = mbe_concChains(mcmcChain); |
| 118 | +% Get summary and posterior plots; comparisonValue = 100 |
| 119 | +summary = mbe_1gr_summary(mcmcChain,100); |
| 120 | +% Data has to be in a cell array and the vectors have to be column vectors |
| 121 | +data = {y'}; |
| 122 | +mbe_1gr_plots(data,mcmcChain,100); |
0 commit comments