# Matlab Toolbox for Bayesian Estimation (MBE)
## Synopsis
This is a Matlab Toolbox for Bayesian Estimation. The basis of the code is a Matlab implementation of Kruschke's R code described in the following paper (Kruschke, 2013), book (Kruschke, 2014) and website (http://www.indiana.edu/~kruschke/BEST/). This toolbox is intended to provide the user with similiar possible analyses as Kruschke's code does, yet makes it applicable in a Matlab-only environment. In addition, I will try to add additional features in the future to make it applicable for more than just a group comparison.
## Code Example
This example uses the data provided in Kruschke's BEST paper (2013).
Run the script mbe_2gr_example.m.
```
%% Load some data
% EXAMPLE DATA (see Kruschke, 2013)
% see http://www.indiana.edu/~kruschke/BEST/ for R code
y1 = [101,100,102,104,102,97,105,105,98,101,100,123,105,103,100,95,102,106,...
109,102,82,102,100,102,102,101,102,102,103,103,97,97,103,101,97,104,...
96,103,124,101,101,100,101,101,104,100,101];
y2 = [99,101,100,101,102,100,97,101,104,101,102,102,100,105,88,101,100,...
104,100,100,100,101,102,103,97,101,101,100,101,99,101,100,100,...
101,100,99,101,100,102,99,100,99];
y = [y1,y2]; % combine data into one vector
x = [ones(1,length(y1)),2*ones(1,length(y2))]; % create group membership code
nTotal = length(y);
%% Specify prior constants, shape and rate for gamma distribution
% See Kruschke (2011) for further description
mu1PriorMean = mean(y);
mu1PriorSD = std(y)*5; % flat prior
mu2PriorMean = mean(y);
mu2PriorSD = std(y)*5;
sigma1PriorMode = std(y);
sigma1PriorSD = std(y)*5;
sigma2PriorMode = std(y);
sigma2PriorSD = std(y)*5;
nuPriorMean = 30;
nuPriorSD = 30;
% Now get shape and rate for gamma distribution
[Sh1, Ra1] = mbe_gammaShRa(sigma1PriorMode,sigma1PriorSD,'mode');
[Sh2, Ra2] = mbe_gammaShRa(sigma2PriorMode,sigma2PriorSD,'mode');
[ShNu, RaNu] = mbe_gammaShRa(nuPriorMean,nuPriorSD,'mean');
% Save prior constants in a structure for later use with matjags
dataList = struct('y',y,'x',x,'nTotal',nTotal,...
'mu1PriorMean',mu1PriorMean,'mu1PriorSD',mu1PriorSD,...
'mu2PriorMean',mu2PriorMean,'mu2PriorSD',mu2PriorSD,...
'Sh1',Sh1,'Ra1',Ra1,'Sh2',Sh2,'Ra2',Ra2,'ShNu',ShNu,'RaNu',RaNu);
```
Now specify the MCMC properties and run JAGS:
```
%% Specify MCMC properties
% Number of MCMC steps that are saved for EACH chain
% This is different to Rjags, where you would define the number of
% steps to be saved for all chains together (in this example 12000)
numSavedSteps = 4000;
% Number of separate MCMC chains
nChains = 3;
% Number of steps that are thinned, matjags will only keep every nth
% step. This does not affect the number of saved steps. I.e. in order
% to compute 10000 saved steps, matjags/JAGS will compute 50000 steps
% If memory isn't an issue, Kruschke recommends to use longer chains
% and no thinning at all.
thinSteps = 5;
% Number of burn-in samples
burnInSteps = 1000;
% The parameters that are to be monitored
parameters = {'mu','sigma','nu'};
%% Initialize the chain
% Initial values of MCMC chains based on data:
mu = [mean(y1),mean(y2)];
sigma = [std(y1),std(y2)];
% Regarding initial values: (1) sigma will tend to be too big if
% the data have outliers, and (2) nu starts at 5 as a moderate value. These
% initial values keep the burn-in period moderate.
% Set initial values for latent variable in each chain
for i=1:nChains
initsList(i) = struct('mu', mu, 'sigma',sigma,'nu',5);
end
%% Specify the JAGS model
% This will write a JAGS model to a text file
% You can also write the JAGS model directly to a text file or use
% one of the models that come with this toolbox
modelString = [' model {\n',...
' for ( i in 1:nTotal ) {\n',...
' y[i] ~ dt( mu[x[i]] , 1/sigma[x[i]]^2 , nu )\n',...
' }\n',...
' mu[1] ~ dnorm( mu1PriorMean , 1/mu1PriorSD^2 ) # prior for mu[1]\n',...
' sigma[1] ~ dgamma( Sh1 , Ra1 ) # prior for sigma[1]\n',...
' mu[2] ~ dnorm( mu2PriorMean , 1/mu2PriorSD^2 ) # prior for mu[2]\n',...
' sigma[2] ~ dgamma( Sh2 , Ra2 ) # prior for sigma[2]\n',...
' nu ~ dgamma( ShNu , RaNu ) # prior for nu \n'...
'}'];
fileID = fopen('mbe_2gr_example.txt','wt');
fprintf(fileID,modelString);
fclose(fileID);
model = fullfile(pwd,'mbe_2gr_example.txt');
%% Run the chains using matjags and JAGS
% In case you have the Parallel Computing Toolbox, use ('doParallel',1)
[samples, stats, mcmcChain] = matjags(...
dataList,...
model,...
initsList,...
'monitorparams', parameters,...
'nChains', nChains,...
'nBurnin', burnInSteps,...
'thin', thinSteps,...
'verbosity',2,...
'nSamples',numSavedSteps);
%% Restructure the output
% This transforms the output of matjags into the format that mbe is
% using
mcmcChain = mbe_restructChains(mcmcChain);
```
Examine the chains with the mbe_diagMCMC() function:
```
mbe_diagMCMC(mcmcChain);
```
You will get these figures:
![alt tag](https://cloud.githubusercontent.com/assets/17763631/14780816/a0c107dc-0ad6-11e6-967e-468e8e553e21.jpg)
Now examine the posterior distributions of the parameters.
```
%% Examine the results
% At this point, we want to use all the chains at once, so we
% need to concatenate the individual chains to one long chain first
mcmcChain = mbe_concChains(mcmcChain);
% Get summary and posterior plots
summary = mbe_2gr_summary(mcmcChain);
% Data has to be in a cell array
data{1} = y1;
data{2} = y2;
mbe_2gr_plots(data,mcmcChain);
```
These are examples of the figures that can be created to show the posterior distributions:
![alt tag](https://cloud.githubusercontent.com/assets/17763631/14780814/a0ad9f44-0ad6-11e6-96fe-9afdd2c2f200.jpg)
![alt tag](https://cloud.githubusercontent.com/assets/17763631/14780815/a0bf847a-0ad6-11e6-8e2c-59fe8143dbec.jpg)
![alt tag](https://cloud.githubusercontent.com/assets/17763631/14780813/a0a8c316-0ad6-11e6-8648-0fdb4dcd4ecc.jpg)
## Functions
#### Examples
* mbe_2gr_example.m
> - This is an example script for a comparison of two groups.
* mbe_2gr_plots.m
> - Makes histogram of data with superimposed posterior prediction check and plots posterior distribution of monitored parameters.
* mbe_2gr_summary.m
> - Computes summary statistics for all parameters of a 2 group comparison.This will only work for a mcmc chain with parameters mu1,mu2,sigma1,sigma2 and nu.
* mbe_1gr_example.m
> - This is an example script for a one group Bayes estimation.
* mbe_1gr_plots.m
> - Makes histogram of data with superimposed posterior prediction check and plots posterior distribution of monitored parameters.
* mbe_1gr_summary.m
> - Computes summary statistics for all parameters.This will only work for a mcmc chain with parameters mu1,sigma1 and nu.
#### MCMC Diagnostics
* mbe_diagMCMC.m
> - Plots autocorrelation, parameter trace, shrink factor and parameter density.
* mbe_tracePlot.m
> - Creates a trace plot for a parameter of a MCMC chain.
* mbe_acfPlot.m
> - Plots autocorrelation of MCMC chain parameter for every chain.
* mbe_gelmanPlot.m
> - This plot shows the evolution of Gelman and Rubin's shrink factor as the number of iterations increases. More than one chain is needed.
* mbe_mcmcDensPlot.m
> - Plots probability density function MCMC chains of one parameter.Also shows the HDI of the parameter for every chain.
#### Posterior Plots
* mbe_plotPost.m
> - Plotting posterior distribution with highest density interval.
* mbe_plotPairs.m
> - Plots matrix of scatter plots for any combination of parameters.
* mbe_plotData.m
> - Plots histogram of observed data. If specified, adds superimposed posterior predictive check. Works only when comparing two groups.
#### Utilities
* mbe_restructChains.m
> Restructures MCMC output (of matjags). Matjags creates multiple structures when more than one chain is used, but stores parameters with the same name in the same variable. This function splits up the parameters and creates one structure with
没有合适的资源?快使用搜索试试~ 我知道了~
Matlab Toolbox for Bayesian Estimation.zip
共57个文件
m:44个
tif:7个
txt:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
1 下载量 83 浏览量
2023-07-20
20:38:15
上传
评论
收藏 502KB ZIP 举报
温馨提示
Matlab Toolbox for Bayesian Estimation.zip
资源推荐
资源详情
资源评论
收起资源包目录
Matlab Toolbox for Bayesian Estimation.zip (57个子文件)
matlab-bayesian-estimation-master
mbe_2gr_example.txt 400B
mbe_plotData.m 4KB
mbe_gelmanPlot.m 1KB
mbe_plotPairs.m 2KB
mbe_1gr_example.txt 195B
mbe_mcmcDensPlot.m 1KB
mbe_2gr_summary.m 2KB
mbe_tracePlot.m 794B
mbe_restructChains.m 3KB
mbe_hdi.m 1KB
mbe_2gr_plots.m 4KB
mbe_diagMCMC.m 2KB
fileExchange
mcmcdiag
custats.m 670B
ChangeLog 3KB
kernel1.m 3KB
ksstat.m 3KB
psrf.m 3KB
kernels.m 2KB
geyer_icse.m 2KB
cipsrf.m 1KB
thin.m 2KB
acorrtime.m 2KB
mpsrf.m 3KB
geyer_imse.m 2KB
cmpsrf.m 1KB
acorr.m 773B
cpsrf.m 1KB
hpdi.m 923B
gbiter.m 4KB
join.m 2KB
gbinit.m 912B
hair.m 1KB
ipsrf.m 2KB
ndhist.m 4KB
score.m 1011B
cusum.m 773B
kernelp.m 2KB
Contents.m 2KB
License.txt 18KB
mbe_plotPost.m 4KB
mbe_summary.m 3KB
mbe_acfPlot.m 1KB
mbe_concChains.m 1KB
exampleFigs
difference_of_means_2gr_example.tif 256KB
parameters_for_2gr_example.tif 3.03MB
post_predictive_2gr_example.tif 1.18MB
effectSize_2gr_example.tif 273KB
correlation_of_parameters_2gr_example.tif 1.79MB
plots_for_1gr_example.tif 2.98MB
diagnostics_for_mu1.tif 3.2MB
mbe_gammaShRa.m 1KB
mbe_1gr_example.m 4KB
mbe_1gr_summary.m 1KB
.gitignore 95B
mbe_2gr_example.m 5KB
mbe_1gr_plots.m 3KB
README.md 10KB
新建文件夹
共 57 条
- 1
资源评论
AbelZ_01
- 粉丝: 875
- 资源: 5441
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功