r语言 - 计算月/周水平上的相关性



我在计算不同国家每月/每周电价之间的相关系数时遇到了麻烦。数据集(https://github.com/Argiro1983/prices_df.git)如下所示:

prices_df<-structure(list(DATETIME = structure(c(1609459200, 1609462800, 
1609466400, 1609470000, 1609473600, 1609477200, 1609480800, 1609484400, 
1609488000, 1609491600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
GR = c(50.87, 48.19, 44.68, 42.92, 40.39, 20.96, 39.63, 40.1, 
20, 40.74), IT = c(50.87, 48.19, 44.68, 42.92, 40.39, 40.2, 
39.63, 40.09, 41.27, 41.67), BG = c(49.95, 48.05, 49.62, 
46.73, 45.39, 44.25, 36.34, 19.97, 20, 20.43), HU = c(45.54, 
41.59, 40.05, 36.9, 34.47, 32.82, 27.7, 15, 8.43, 20.77), 
TR = c(26.31, 24.06, 24.21, 23.2, 23.2, 26.31, 24.98, 26.31, 
24.04, 26.31), SR = c(38.89, 34.86, 33.62, 28.25, 29.03, 
29.22, 29.71, 1.08, 1.1, 36.07)), row.names = c(NA, 10L), class = "data.frame")

我已经尝试将其转换为xts并使用apply。按如下方法按月(或按周)执行,但行不通。

library(xts)
SEE_prices <- xts(x = prices_df, order.by = DATETIME)
storage.mode(SEE_prices) <- "numeric"
SEE_prices <- na.locf(SEE_prices)

library(tidyverse)
library(tidyquant)
apply.monthly(SEE_prices, cor(SEE_prices$GR, SEE_prices$SR))

另一种方法我试图获得每周水平的相关性是使用dplyr包,但它也不起作用:

library(lubridate)
library(magrittr)
library(dplyr)
prices_df %<>% mutate( DATETIME = ymd_hms(DATETIME) )
table1<- prices_df %>% group_by( year( DATETIME ), isoweek( DATETIME )  ) %>%
summarise( DateCount = n_distinct(date(DATETIME)), correlation = cor(prices_df$GR, prices_df$SR))

有人知道如何计算数据集上的周/月相关性吗?提前谢谢你。

不要在dplyr管道中使用$。要计算相关性,请尝试-

library(dplyr)
library(lubridate)
prices_df %>%
mutate(DATETIME = ymd_hms(DATETIME),
year = year(DATETIME), week = isoweek(DATETIME)) %>%
group_by(year, week) %>%
summarise(DateCount = n_distinct(date(DATETIME)), 
correlation = cor(GR, SR), .groups = 'drop')

最新更新