-
Notifications
You must be signed in to change notification settings - Fork 1
/
runMCMC.R
83 lines (68 loc) · 2.81 KB
/
runMCMC.R
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
77
78
79
80
81
82
83
runMCMC <- function()
{
stanmodels=NULL
StanModel=rstan::stan_model("FrenchGuianaModel.stan")
name = StanModel@model_name
stanmodels$FrenchGuianaModel= StanModel
customFit <-function(model,data,
iter = 5000,
chains = 4,
warmup = floor(iter/2),
thin = 1,
seed = sample.int(.Machine$integer.max, 1),
init = 'random',
check_data = TRUE,
sample_file = NULL,
diagnostic_file = NULL,
verbose = FALSE,
algorithm = c("NUTS", "HMC", "Fixed_param"),
control = NULL,
include = TRUE,
cores = getOption("mc.cores", 1L),
open_progress = interactive() && !isatty(stdout()) &&
!identical(Sys.getenv("RSTUDIO"), "1"),
show_messages = TRUE,
...){
mdls <- model$name
#NZones <- data$Ncategory
# change this here
# the variable pars should include the priors and the parameters of the model
pars = c(model$priors,
NRegions=NRegions)
newdata = append(data,pars)
F <- rstan::sampling(stanmodels[[mdls]],
data= newdata,
iter = iter,
chains = chains,
warmup = warmup,
thin = thin,
seed = seed,
init = init,
check_data = check_data,
sample_file = sample_file,
diagnostic_file = diagnostic_file,
verbose = verbose,
#algorithm = 'Fixed_param',
algorithm = algorithm,
control = list(adapt_delta = 0.95), # increase adaptive delta
#control= control,
include = include,
cores = cores,
open_progress = open_progress,
show_messages = show_messages)
out <-list(fit = F, data = data, model = model)
return(out)
}
customModel <- function(S,
A=70,
ModelZone=as.array(rep(0,NRegions))){
name = S@model_name
priors <- list(ModelZone = ModelZone, A=A)
me <- list(name = name,
priors = priors)
return(me)
}
model = customModel(S = StanModel)
F1 = customFit(data=data,model=model,chains=4,iter=20000,cores=4)
return(F1)
}