简体   繁体   中英

R: how to calculate an EWMA correlation

I have to calculate an EWMA correlation and create a plot with plotly() of the data frame df.dataCorrelation , which is defined as:

date <- c("2018-01-02", "2018-01-03", "2018-01-04", "2018-01-05", "2018-01-06", "2018-01-07", "2018-01-08", 
          "2018-01-09", "2018-01-10", "2018-01-11", "2018-01-12", "2018-01-13", "2018-01-14", "2018-01-15", 
          "2018-01-16")
prod1 <- c(43.10, 42.56, 41.77, 41, NA, NA, 40.79, 41.03, 40.98, 41.13, 41.98, NA, NA, 40.81, 41.21)
prod2 <- c(19.442, 19.320, 19.204, 18.918, NA, NA, 19.041, 19.452, 19.516, 19.344, 19.840, NA, NA, 
           19.177, 18.974)
df.dataCorrelation <- data.frame(date, prod1, prod2)

数据帧 EWMA

How do I create an EWMA correlation plot between prod1 and prod2 ?

EDIT:

I've already tried the qcc -package and the ewma() function:

m.ewmaCorr <- qcc.groups(df.ewmaCorr$prod1, df.ewmaCorr$prod2)
l.ewma <- ewma(m.ewmaCorr, lambda = 0.85)

But this produces an empty plot: 空的

if a plotly alternative is acceptable, this works

EDIT (ugly quickfix)

library(qcc)
library(corrplot)

date <- c("2018-01-02", "2018-01-03", "2018-01-04", "2018-01-05", "2018-01-06", "2018-01-07", "2018-01-08", 
          "2018-01-09", "2018-01-10", "2018-01-11", "2018-01-12", "2018-01-13", "2018-01-14", "2018-01-15", 
          "2018-01-16")
prod1 <- c(43.10, 42.56, 41.77, 41, NA, NA, 40.79, 41.03, 40.98, 41.13, 41.98, NA, NA, 40.81, 41.21)
prod2 <- c(19.442, 19.320, 19.204, 18.918, NA, NA, 19.041, 19.452, 19.516, 19.344, 19.840, NA, NA, 
           19.177, 18.974)
df.dataCorrelation <- data.frame(date, prod1, prod2)
df.dataCorrelation <- na.omit(df.dataCorrelation)

df_prod1_ewma<-ewma(df.dataCorrelation$prod1, lambda = 0.85) # give now ewma plot    
df_prod2_ewma<-ewma(df.dataCorrelation$prod2, lambda = 0.85)

result_df<-cbind.data.frame(df_prod1_ewma$y,df_prod2_ewma$y) 

res<-cor(result_df, use="complete.obs")    

x11()
corrplot(res)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM