How to predict for future times (multiple regression with hierarchial time series component)

191 views
Skip to first unread message

UD1989

unread,
Jul 18, 2015, 2:58:52 PM7/18/15
to stan-...@googlegroups.com
Hi All

I'm modeling a housing sales dataset with 3 grouped predictors and 3 individual predictors. One of the indivdual predictors is the 'last sales average value of this group' to capture the time trend in the series (there could be more than one transactions on a given date hence the average) The data is sparse as far as the time series is concerned- i.e. sales  may occur months or even years apart. I'm using the fixed parameter mode and generated quantities module to generate esimations for new data, but can't figure out how to predict ahead in time since the periods aren't fixed for every group. For example, group A might have had the last transactions in January 15; predicting one period ahead by taking the last period sales would generate an estimate for February, not one period ahead of current date i.e. August. Can anyone suggest how to handle this ?

On a separate note, I am trying to use the estimates to be exposed via a Shiny App. The Stan code (in fixed param mode and without a model) takes about 30 seconds to generate the estimates which is quite fast but not fast for a user-facing application.  I suppose the latency is due to the connection between R and STAN as discussed in some threads i read in this forum, but is there any way I could improve the performance?

Any help will be appreciated

Thanks and Regards

Bob Carpenter

unread,
Jul 18, 2015, 3:26:29 PM7/18/15
to stan-...@googlegroups.com

> On Jul 18, 2015, at 2:58 PM, UD1989 <upasa...@gmail.com> wrote:
>
> Hi All
>
> I'm modeling a housing sales dataset with 3 grouped predictors and 3 individual predictors. One of the indivdual predictors is the 'last sales average value of this group' to capture the time trend in the series (there could be more than one transactions on a given date hence the average) The data is sparse as far as the time series is concerned- i.e. sales may occur months or even years apart. I'm using the fixed parameter mode and generated quantities module to generate esimations for new data, but can't figure out how to predict ahead in time since the periods aren't fixed for every group. For example, group A might have had the last transactions in January 15; predicting one period ahead by taking the last period sales would generate an estimate for February, not one period ahead of current date i.e. August. Can anyone suggest how to handle this ?

You can just keep predicting ahead from wherever your last observation
is. Presumably the latent parameters in the time series are something
like baseline prices or something.

Or you could generalize and use something like a Gaussian process, where
you don't necessarily need exact spacing.

> On a separate note, I am trying to use the estimates to be exposed via a Shiny App. The Stan code (in fixed param mode and without a model) takes about 30 seconds to generate the estimates which is quite fast but not fast for a user-facing application. I suppose the latency is due to the connection between R and STAN as discussed in some threads i read in this forum, but is there any way I could improve the performance?

It would help to see how you're running it. If you precompile the
model first and it still takes 30 seconds and you're just using RNGs,
you're probably generating too many samples. How high an n_eff do you need?

There's no latency in the connection between R and Stan per se,
just that it can take a long time to load big list data structures
into R no matter where they come from.

It always helps to send us more rather than less as it's hard to guess
what people might be doing.

- Bob

UD1989

unread,
Jul 18, 2015, 3:44:42 PM7/18/15
to stan-...@googlegroups.com
Thanks for your reply Bob. I'm using the means of posterior estimates of a pre-fitted model as inputs in the  data block to estimate for previously unseen data (script attached)
pred_shiny.stan

UD1989

unread,
Jul 18, 2015, 3:52:45 PM7/18/15
to stan-...@googlegroups.com
Also, I should perhaps note that the "model run time" stamp that comes up in the console after running stan is complete is actually less than a second, but the wait time occurs whent the console shows "compiling into C++ ...." Its stuck at this message for nearly all of those 30 seconds.

On Sunday, July 19, 2015 at 3:26:29 AM UTC+8, Bob Carpenter wrote:

Andrew Gelman

unread,
Jul 18, 2015, 4:18:48 PM7/18/15
to stan-...@googlegroups.com
Regarding speed, are you using stan.R which automatically saves the compiled version of the model?  Otherwise a lot of those 30 seconds are wasted in compilation.  Also the stan() function on stan.R runs things in parallel, which cuts down computation time by a factor of 4 for me.


On Jul 18, 2015, at 2:58 PM, UD1989 <upasa...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bob Carpenter

unread,
Jul 18, 2015, 4:52:40 PM7/18/15
to stan-...@googlegroups.com
Why not do full Bayes, either yourself by extracting after
Stan runs, or within the generated quantities block in R?

You don't get proper uncertainty estimates doing inference with
posterior means.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
> <pred_shiny.stan>

UD1989

unread,
Jul 19, 2015, 2:10:24 AM7/19/15
to stan-...@googlegroups.com, gel...@stat.columbia.edu

Thanks professor Gelman. Yes I'm using stan() function in R. How do i confirm if its saving the compiled version of the model? I attach the screenshot above of compiler view when stan() is called on the attached model. If i understand the message correctly, re-compilation is taking place?

UD1989

unread,
Jul 19, 2015, 2:18:29 AM7/19/15
to stan-...@googlegroups.com
Yes I'm aware of that....I intend to extend to full bayes within the generated quatities block-for now I was tryig to figure out the time aspect since I expect time taken to increase even more after I go full bayes....

BTW, could you explain what you said when you meant "You can just keep predicting ahead from wherever your last observation is" How exactly do I do that (pointing out a resource with similar example would be helpful)

Thanks a ton for taking the time to help!

Regards

UD1989

unread,
Jul 19, 2015, 6:03:33 AM7/19/15
to stan-...@googlegroups.com, gel...@stat.columbia.edu
Never mind professor Gelman...I figured it out....the model is precompiling now and my time issue is resolved!!! Thanks for your help in pointing out that i never "saved" my model!

Bob Carpenter

unread,
Aug 2, 2015, 3:32:38 PM8/2/15
to stan-...@googlegroups.com
I don't have anything to point to, but it should be easy.
Just follow the generative model. For instance, you can
predict in the generated quantities block. If it's a simple
AR(1) model like in the manual, then you get something like
this:

generated quantities {
vector[N_tilde] y_tilde;
if (N_tilde > 0) {
y_tilde[1] <- normal_rng(alpha + beta * y[N], sigma);
for (n in 2:N_tilde)
y_tilde[n] <- normal_rng(alpha + beta * y_tilde[n-1], sigma);
}
}

- Bob
Reply all
Reply to author
Forward
0 new messages