# Main Question:

How do I apply e.divisive method from ecp package effectively on multivariate time-series, so that the resulting segments and changepoints are consistent with domain knowledge and the process is efficient?

# Background

I have data of cars moving in a lane. In the car-following literature, the movement of a subject car relative to its lead car in the same lane can be divided into at-least 3 distinct states:

### Free-driving:

Distance between the two cars is large, so the subject car (for which the data is collected) either accelerates or maintains speed. So, the position of the gas pedal is expected to be fully or partially pressed and maintained for some time.

### Approaching:

Speed difference between subject car and lead car is quite large because subject car was moving at higher speed. But due to decrease in distance, the subject car reduces speed. So, it is expected that driver in subject car completely steps off the gas pedal and in some cases follow by pressing brake pedal.

### Following:

Subject car reduces speed until the speed difference becomes zero and the distance reduces to the distance that subject car wants to keep while closely following lead car. So, it is expected that we see slight up and down movements in the position of gas pedal in subject car because driver tries to maintain the current speed equal to lead car's speed.

## Example:

Here is one example that illustrates 2 states:

I am showing plots for 5 variables for a subject car called "Cars_02". The x-axis contains the time frames (60 frames in 1 second), and y axis contains the given variable. On each plot I also manually put vertical lines where I think the Approaching and Following states start. For keeping this question medium sized, I'm skipping what happened before these 2 states.
You can see that Approaching starts when the driver steps off the gas pedal (see gas pedal position, plot # 2) which causes a decrease in speed (see plot # 1). Afterward, Following starts when speed difference becomes zero (plot # 4) and distance between the car gets shorter and remains consistent for some time (plot # 5). Also, the gas pedal position has up and down movements which shows that driver tried to maintain the speed.

# What I want to do?

I want to divide these time-series into segments where each segment represents a car-following state (basically automating my manual effort to identify pattern in multiple variables and detecting where the state changed). This is my first time using ecp so I have several questions:

1. Is using ecp going to be useful in this case?
2. Should I normalize all variables first?
3. Should I smooth all variables as they have tiny undulations?
4. How can I make e.divisive run faster? The above time series database is about 6000 data points. And I have 30 other time series.

# What I have tried?

I tried using e.divisive on a sliced gas pedal position variable only (first 2000 data points). I kept the permutations parameter R very small because otherwise it takes very, very long on my 12 GB RAM laptop.

library(ecp)
Xnorm <- as.matrix(df$acc_pedal_pos) output1 <- e.divisive(Xnorm, R = 20, alpha = 2) ts.plot(Xnorm,ylab='Value',main='Change points') abline(v=output1$estimates[c(-1,-21)],col='red',lty=2)


You can see that the small steps in the gas pedal positions cause detection of multiple changepoints, however, they should actually be part of the same behavior: accelerating to reach a desired speed in Free-driving'.

So, what is the most effective way to use ecp for my problem?

Due to the length of the data and the gradual change you are expecting (and have seen with the final plot) i'm not sure that ecp is the best approach to use. Instead you may want to fit a changepoint model which allows for slopes and large datasets. I suggest you give the EnvCpt package a try. The primary focus of the package is for model selection and so the main function fits 8 different models to allow the user to choose the most appropriate.
Having said this you can use the underlying functions to fit a specific model form, such as the trend you see in your plots. Use the EnvCpt:::cpt.reg function to do this. In contrast to ecp` this means making model assumptions but it looks like it might give you the segmentation you want.