# How to make predictions over time

I need to build a statistical model to predict the time of assembly of a machine. Those predictions should be updated during the assembly on a daily basis. I have some variables which for each machine built do not change in time such as the machine model and others, and some variables time-dependent such as the number of missing parts required to make the machine, the number of days spent on the machine so far and so on.

So in the dataset for each machine made in the last years (about 100 of them) I have a number of rows equal to the number of days spent to build that machine, in which the response variable is always the same (known at the end of the assembly), while some of the predictor variables changes.

How can I model this kind of data? I think is not a repeated measures analysis because for each machine the response is always the same, only some predictors changes, I just make predictions several times over time because I expect to improve my forecast approaching the end of the assembly. I can change the response variable to be the number of days remaining to finish assembly instead of the total number of days of assembly, perhaps in this case I can fit a mixed model for repeated measures? Or maybe Time-Varying survival regression?

With this structure of the data, I would build a discrete time hazard model. The literature on those is quite raw on the web, since I last searched about it. But it boils down to logistic regression where each row is a period (day, minute, etc...).

In response variable,for each subject(machine in your case), you have a zero, indicating that the subject did not experience the event (the machine wasn't ready at that period) and in the last observation you have a 1 indicating the machine was ready in that period, and then no more rows for that observation.

Here is a quick artificial dataset I built in R with this structure.

library(reprex)
library(tidyverse)

set.seed(11)
tibble(time=(1:6)) %>% crossing(machine = LETTERS[0:10]) %>%
arrange(machine) %>%
mutate(time_varying_covar = runif(n())) %>%
group_by(machine) %>%
mutate(finished_at = runif(1, 0, max(time))) %>%
mutate(non_time_varying_covar = rnorm(1, 10, 3)) %>%
mutate(not_finished = time < finished_at) %>%
mutate(response = cumsum(!not_finished)) %>%
filter(response <=1) %>%
select(time, machine, response, finished_at, time_varying_covar, non_time_varying_covar)
#> # A tibble: 29 x 6
#> # Groups:   machine [10]
#>     time machine response finished_at time_varying_covar non_time_varying_covar
#>    <int> <chr>      <int>       <dbl>              <dbl>                  <dbl>
#>  1     1 A              1       0.808             0.277                   11.5
#>  2     1 B              0       4.51              0.0865                   9.44
#>  3     2 B              0       4.51              0.290                    9.44
#>  4     3 B              0       4.51              0.881                    9.44
#>  5     4 B              0       4.51              0.123                    9.44
#>  6     5 B              1       4.51              0.175                    9.44
#>  7     1 C              0       1.04              0.907                   14.6
#>  8     2 C              1       1.04              0.851                   14.6
#>  9     1 D              0       3.48              0.158                    8.17
#> 10     2 D              0       3.48              0.480                    8.17
#> # … with 19 more rows


Created on 2020-10-28 by the reprex package (v0.3.0)

With this structure, you can model:

• the effect of time in how likely it is for a subject to experience the event at that period
• as well as the effect of time varying predictors and non time varying predictors
• model subject specific effects through mixed/hierarchical modeling

You can learn more about those models in Part II of Applied Longitudinal Data Analysis by Singer and Willett.

• Thank you for your answer. I have a question: setting the problem that way and using logistic regression, how can I forecast for a given machine and time t, the total amount of days needed to make the machine? Or do I just get the probability that the machine is completed that day? – Motmot Oct 28 '20 at 15:35
• You get a probability. But you can see what is the probability at an arbitrary time and see the difference in periods from now to when that probability reaches an acceptable threshold (90% probability means it will probably be done for example). But the applicability of this method is highly dependent on the data, and in how it is best appropriate for you to model time. – Guilherme Marthe Oct 28 '20 at 15:51
• OK thanks. I found this tutorial do you reccomend it to me? rensvandeschoot.com/tutorials/discrete-time-survival – Motmot Oct 29 '20 at 15:03
• awesome tutorial! great find! It seems to cover all basics. – Guilherme Marthe Oct 29 '20 at 16:35
• Just one last question: is it possible somehow using this approach to estimate a whole survival curve for a given machine that update daily according to the change in time dependent variables? – Motmot Oct 30 '20 at 14:21