-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStanModel.R
More file actions
63 lines (54 loc) · 1.86 KB
/
Copy pathStanModel.R
File metadata and controls
63 lines (54 loc) · 1.86 KB
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
suppressPackageStartupMessages({
library(rstan)
library(plyr)
source("library.R")
source("stanFunctions.R")
})
infile <- "data.RData"
grid <- "motion_energy.csv"
modelfile <- "SlopeModel.stan.RData"
outfile <- "SlopeModel.fit.RData"
maxll <- mkchain(as.data.frame, .[which.max(.$lp__)[[1]],])
#take a vector of coefficients with the weird names that Stan produces
#and put it into the list data structure that seems to be implied
normalize_coefs <- function(coefs) {
e <- new.env()
vars <- lapply(names(coefs), mkchain(parse(text=.), .[[1]]))
mapply(coefs, vars, FUN=function(c, v) {
if (length(v) > 1) {
n <- as.character(v[[2]])
v[[2]] <- call("$", quote(e), v[[2]])
if (!n %in% ls(e)) e[[n]] <- c()
} else {
v <- call("$", quote(e), v)
}
eval(bquote(.(v) <- .(c)))
})
as.list(e)
}
main <- function(infile="data.RData",
grid="motion_energy.csv",
modelfile="SlopeModel.stan.RData",
outfile="SlopeModel.fit.RData",
iter=2000
) {
iter <- as.numeric(iter)
e <- load2env(modelfile) #contains as least 'model', 'stan_predict'
d <- load2env(infile)
menergy <- read.csv(grid)
chain(d$data, (e$filter_data)(), (e$format_data)(menergy)) -> e$data
fits <- ddply_along(e$data, e$model_split, function(split, chunk) {
stan_data <- e$stan_format(chunk)
fit <- sampling(e$model, data=stan_data, warmup=iter/2, iter=iter)
print(split)
print(fit)
# then also get the max-likelihood parameters. And the Hessian? Nah.
startpoint <- normalize_coefs(maxll(as.data.frame(fit)))
l = optimizing(e$model, stan_data, init=startpoint)
optimized = c(l$par, lp__=list(l$value))
quickdf(list(fit = list(fit), optimized=list(optimized)))
})
e$fits <- asisify(fits)
save(file=outfile, envir=e, list=ls(e))
}
run_as_command()