2014-05-09 39 views
0

我想绘制时间步长的双变量相关图,以便x轴是时间,y轴是双变量相关系数。 airquality数据就是一个很好的例子。在这种情况下,我想通过Day来绘制Ozone&TempOzone&Wind之间的相关性。谢谢!随时间变化的双变量相关图

data(airquality) 

相关矩阵如下:

  Ozone Solar.R Wind Temp Month Day 
Ozone 1.00 0.35 -0.60 0.70 0.16 -0.01 
Solar.R 0.35 1.00 -0.06 0.28 -0.08 -0.15 
Wind -0.60 -0.06 1.00 -0.46 -0.18 0.03 
Temp  0.70 0.28 -0.46 1.00 0.42 -0.13 
Month 0.16 -0.08 -0.18 0.42 1.00 -0.01 
Day  -0.01 -0.15 0.03 -0.13 -0.01 1.00 

回答

5

,每天只有一个观察,这是不可能计算的相关性。 但是,您可以计算移动窗口上的相关性,例如,使用rollapply

# Convert the data to time series 
library(zoo) 
d <- zoo( 
    airquality, 
    sprintf("%02i-%02i", airquality$Month, airquality$Day) 
) 

# Compute the correlations 
r <- rollapply( 
    d, 
    width = 7, 
    FUN = function(u) c( 
    cor(u[,"Ozone"], u[,"Temp"], use="pairwise"), 
    cor(u[,"Ozone"], u[,"Wind"], use="pairwise") 
), 
    by.column = FALSE, 
    align = "right" 
) 

# Plot 
matplot(1:nrow(r), r, type="l", lwd=3, lty=1, axes=FALSE) 
axis(2, las=1) 
axis(1, at=1:nrow(r), labels=index(r), las=2) 
box() 
+0

谢谢,对不起,我没有提到移动窗口。这正是我想要的。 –