-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitBugs.m
76 lines (66 loc) · 3.2 KB
/
fitBugs.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function params = fitBugs(set,NPostSamples,knnmdl,mod)
FMt = set.FMt;
tt = set.tt;
id = set.id;
NSubjects = id(end);
NMeasurements = numel(FMt);
NChains = 1;
NBurnin = 10^3;
NSamples = 10^3;
doparallel = 0;
numGroups = 5;
dataStruct = struct('y', FMt, ...
'NSubjects', NSubjects,...
'NMeasurements', NMeasurements,...
'NGroups', numGroups,...
't', tt,...
'gpalpha', 1.6*ones(numGroups,1),...
'id',id);
init0(1:NChains) = struct('alphaL', cell(1, 1),'rL', cell(1, 1),'gp', cell(1, 1),...
'tauShift', cell(1, 1),'yp', cell(1, 1),'g', cell(1, 1),'alpham', cell(1, 1),'alphap', cell(1, 1));
for i=1:NChains
init0(i).alphaL = 0.5*ones(NSubjects,1);
init0(i).g = round(numGroups*rand(NSubjects,1)+0.5);
init0(i).rL = log(mod.rETI(:,1)./(1-mod.rETI(:,1)));
init0(i).tauShift = mod.tauETI(:,1)-1/7;
init0(i).yp = mod.yp(1);
init0(i).alpham = mod.alphamETI(:,1);
init0(i).alphap = mod.alphapETI(:,1);
init0(i).gp = mod.gpETI(:,1);
end;
[samples, stats, ~] = matjags( ...
dataStruct, ... % Observed data
fullfile(pwd, 'proportionalRecovery.txt'), ... % File that contains model definition
init0, ... % Initial values for latent variables
'doparallel' , doparallel, ... % Parallelization flag
'nchains', NChains,... % Number of MCMC chains
'nburnin', NBurnin,... % Number of burnin steps
'nsamples', NSamples, ... % Number of samples to extract
'thin', 1, ... % Thinning parameter
'dic', 0, ... % Do the DIC?
'monitorparams', {'yp','g','gp',...
'tau','r','alpham','alphap'}, ... % List of latent variables to monitor
'savejagsoutput' , 1 , ... % Save command line output produced by JAGS?
'verbosity' , 0 , ... % 0=do not produce any output; 1=minimal text output; 2=maximum text output
'cleanup' , 1); % clean up of temporary files?
randInd = randperm(NSamples,NPostSamples);
params = struct('yp', cell(1, NPostSamples+1),'gp', cell(1, NPostSamples+1),...
'tau', cell(1, NPostSamples+1),'r', cell(1, NPostSamples+1),'alpham', cell(1, NPostSamples+1),...
'alphap', cell(1, NPostSamples+1),'clust', cell(1, NPostSamples+1));
params(1).yp = stats.mean.yp;
params(1).gp = stats.mean.gp;
params(1).tau = stats.mean.tau;
params(1).r = stats.mean.r;
params(1).alpham = stats.mean.alpham;
params(1).alphap = stats.mean.alphap;
params(1).clust = predict(knnmdl,[params(1).r', params(1).tau', params(1).alpham', params(1).alphap']);
for ii=1:NPostSamples
params(ii+1).yp = samples.yp(1,randInd(ii));
params(ii+1).gp = samples.gp(1,randInd(ii),:);
params(ii+1).tau = samples.tau(1,randInd(ii),:);
params(ii+1).r = samples.r(1,randInd(ii),:);
params(ii+1).alpham = samples.alpham(1,randInd(ii),:);
params(ii+1).alphap = samples.alphap(1,randInd(ii),:);
params(ii+1).clust = params(1).clust;
end;
end