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
。