[英]correlation time series in r
I currently have a data set that looks as such (3 variables and a time series): 我目前有一个看起来像这样的数据集(3个变量和一个时间序列):
date variable value
01/01/16 A 1000
01/01/16 B 800
01/01/16 C 10
01/02/16 A 2000
01/02/16 B 1800
01/02/16 C 100
01/03/16 A 100
01/03/16 B 80
01/03/16 C 10
What I'm trying to do is calculate the correlation between all three variables (AB, AC, BC) per day so that I can graph the result as a plot with the date being on the x axis and on the y axis the correlation coefficient (w/ the legend being AB, AC, BC). 我想做的是每天计算所有三个变量(AB,AC,BC)之间的相关性,以便我可以将结果绘制成曲线图,日期在x轴上,y轴上是相关系数(带有图例的AB,AC,BC)。
Any help would be greatly appreciated! 任何帮助将不胜感激!
I think this is what you want: 我认为这是您想要的:
df <- read.csv(sep=" ",stringsAsFactors=F,text="date variable value
01/01/16 A 1000
01/01/16 B 800
01/01/16 C 10
01/02/16 A 2000
01/02/16 B 1800
01/02/16 C 100
01/03/16 A 100
01/03/16 B 80
01/03/16 C 10 ")
df$date <- strptime(df$date, format = "%m/%d/%y")
df$value <- as.numeric(df$value)
vars <- unique(df$variable)
nv <- length(vars)
npairs <- nv*(nv-1)/2
labs <- rep("", npairs)
cors <- rep(0, npairs)
k <- 1
for (i in 1:(nv-1)) {
vi <- vars[i]
si <- df[df$variable == vi,]$value
for (j in (i+1):nv) {
vj <- vars[j]
sj <- df[df$variable == vj,]$value
labs[k] <- sprintf("%s-%s",vi,vj)
cors[k] <- cor(si, sj)
k <- k+1
}
}
pdf <- data.frame(labs=labs,cors=cors)
ggplot(pdf) + geom_bar(aes(x=labs,y=cors),stat="identity")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.