bobolafrite bobolafrite - 10 months ago 76
R Question

R - Can't find a way to plot the line which is closest to local maximums (méthode de la bande)

I'm working on an app to analyze time series with R and shiny, and I would like to plot a specific graphic to help choosing between additive or multiplicative model :1

I would like to plot my time series, but also plot two lines which are closest to each maximum and each minimum of each period respectively.

Here is a link to the graphic I would like to draw :

For the moment here is my code, I called my function


plot_band <- function(Xt, period){

# Create an index
index <- 1:lenght(Xt)

# Create the vector period which value is the period the point belong to
periods <- index%/%period + 1

# Create a dataframe
df <- data.frame(xt= Xt,periods = as.factor(periods))

# FInd the minimums and maximums
mins <- df[df$xt == ave(df$xt, df$period, FUN=min), ]
maxs <- df[df$xt == ave(df$xt, df$period, FUN=max), ]

# Regression with lm
mins_reg <- lm(mins$xt ~ mins$index)
maxs_reg <- lm(maxs$xt ~ maxs$index)

#And I don't know how to plot everything
my_graph <- ggplot(data=df,

Another issue is that
is in a ts format when it's given in parameter, and I don't know how to get the real index instead of indexing on

Answer Source

I finally found a solution for this problem, it might not be the better but here it is :

plot_bande <- function(xt,period){

begin <- start(xt)[1]
end <- end(xt)[1]
freq <- frequency(xt)

idx <- seq(begin,end,freq)
periods <- idx %/% period + 1 - idx[1] %/% period

df <- data.frame(data=xt,period=periods, idx=idx)

df$period <- as.factor(df$period)

min <- df[df$data == ave(df$data, df$period, FUN=min), ]
max <- df[df$data == ave(df$data, df$period, FUN=max), ]

reg_min <- lm(min$data ~ min$idx)
reg_max <- lm(max$data ~ max$idx)

a_min <- coef(reg_min)[1]
b_min <- coef(reg_min)[2]
a_max <- coef(reg_max)[1]
b_max <- coef(reg_max)[2]

legend('topleft', legend=c('Minimum values', 'Maximum values'),col=c('blue','red'), lty=1)


And you can see the graph I obtained with the sunspot.year dataset and a period of 12 years by clicking this link