0
我正在研究一个应用程序,用R和闪亮分析时间序列,并且我想绘制一个以帮助选择添加剂或乘法模型:1R - 找不到绘制最接近局部最大值的线的方法
我想绘制我的时间序列,也绘制两条线,分别与每个最大值和每个最小值分别最接近。
这里是一个图形的链接我想提醒:https://i.imgsafe.org/fdb95a34f9.png
对于这里的那一刻是我的代码,我打电话给我的功能plot_band
:
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,
的另一个问题是,xt
是一个ts格式,当它在参数中给出时,我不知道如何获得真实索引而不是索引N
。