Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update #4

Merged
merged 3 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Encoding: UTF-8
LazyData: true
URL: https://atsa-es.github.io/atsar/
BugReports: https://github.com/atsa-es/atsar/issues
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
SystemRequirements: GNU make
Biarch: true
VignetteBuilder: knitr
12 changes: 12 additions & 0 deletions R/fit_stan.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ fit_stan <- function(y, x = NA, model_name = NA,
n_pos <- length(row_indx_pos)
y <- y[which(!is.na(y))]

est_A <- rep(1, M)
for(i in 1:max(marss$states)) {
indx <- which(marss$states==i)
est_A[indx[1]] <- 0
}
est_A <- which(est_A > 0)
est_A <- c(est_A, 0)
n_A <- length(est_A) - 1

data = list("N"=N,"M"=M, "y"=y,
"states"=marss$states, "S" = max(marss$states), "obsVariances"=marss$obsVariances,
"n_obsvar" = max(marss$obsVariances), "proVariances" = proVariances,
Expand All @@ -194,6 +203,8 @@ fit_stan <- function(y, x = NA, model_name = NA,
"n_pos" = n_pos,
"col_indx_pos" = col_indx_pos,
"row_indx_pos" = row_indx_pos,
"est_A" = est_A,
"n_A" = n_A,
"y_int"=round(y),
"family"=1,
"est_trend" = as.numeric(est_trend),
Expand All @@ -202,6 +213,7 @@ fit_stan <- function(y, x = NA, model_name = NA,
pars = c("pred", "log_lik","sigma_process","sigma_obs","x0")
if(marss$est_B) pars = c(pars, "B")
if(marss$est_trend) pars = c(pars, "U")
if(n_A > 0) pars = c(pars,"A")
}
if (map_estimation == FALSE) {
out <- rstan::sampling(
Expand Down
28 changes: 15 additions & 13 deletions inst/stan/marss.stan
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ data {
int<lower=0> row_indx_pos[n_pos];
int<lower=0> est_trend;
int<lower=0> est_B;
int<lower=0> n_A;
int<lower=0> est_A[n_A+1];
vector[n_pos] y; // data
int y_int[n_pos];
int family; // 1 = normal, 2 = binomial, 3 = poisson, 4 = gamma, 5 = lognormal
Expand All @@ -23,6 +25,7 @@ parameters {
vector[S] pro_dev[N-1];
vector[n_trends * est_trend] U;
matrix[S*est_B,S*est_B] B;
vector[n_A] A; // offsets
real<lower=0> sigma_process[S];
real<lower=0> sigma_obs[n_obsvar];
}
Expand All @@ -32,6 +35,10 @@ transformed parameters {
//matrix[N, S] x;
matrix[S,S] Bmat;
vector[S] Uvec;
vector[M] Avec;

for(i in 1:M) Avec[i] = 0;
for(i in 1:n_A) Avec[est_A[i]] = A[i];

for(i in 1:S) {
if(est_trend) {
Expand All @@ -58,32 +65,27 @@ transformed parameters {
x[t,] = x[t,] + Uvec;
}
}
// random walk in states
// for(s in 1:S) {
// x[1,s] = x0[s]; // initial state, vague prior below
// for(t in 2:N) {
// x[t,s] = x[t-1,s] + pro_dev[t-1,s];
// if(est_trend == 1) x[t,s] = x[t,s] + U[trends[s]];
// }
// }

// map predicted states to time series
for(m in 1:M) {
for(t in 1:N) {
pred[t,m] = x[t,states[m]];
pred[t,m] = x[t,states[m]] + Avec[m];
}
}
}
model {
//x0 ~ normal(0,10);
x0 ~ normal(0, 3); // initial states
A ~ normal(0, 3); // A offsets

for(i in 1:n_obsvar) {
sigma_obs[i] ~ student_t(3,0,2);
sigma_obs[i] ~ student_t(3,0,1); // observation var sigma
}
for(s in 1:n_provar) {
sigma_process[s] ~ student_t(3,0,2); // process var
sigma_process[s] ~ student_t(3,0,1); // process var sigma
}
if(est_trend==1){
for(i in 1:n_trends) {
U[i] ~ normal(0,1);
U[i] ~ normal(0,1); // optional trends
}
}
for(s in 1:S) {
Expand Down
Binary file modified src/atsar.so
Binary file not shown.
Loading
Loading