R - 具有两个数据集之间相关性的世界等高线图

发布于 2024-12-29 12:31:51 字数 1169 浏览 3 评论 0原文

我有两个数据集 A 和 B,我想找到相关性并绘制等高线图。

A只是一个简单的向量,有230个流数据。

B是一系列日期下的复杂海面温度(SST)数据。在每个日期,SST 都有一个 360 行 * 180 列记录温度的矩阵。

向量A(230个数据)是:

Houlgrave_flow_1981_2000 = window(Houlgrave_flow_average, start = as.Date("1981-11-15"),end = as.Date("2000-12-15")) 
Houlgrave_SF_1981_2000 = coredata(Houlgrave_flow_1981_2000)

矩阵B的维度如下所示,我只使用1到230。

> dim(ssta_sst)
[1] 360 180 362

我寻找相关性的想法如下。

z_correlation = cor(Houlgrave_SF_SST_1981_2000,ssta_sst[c(181:360, 1:180),,i])

尝试一下,i=1。但是,它不起作用。错误消息显示:

"Error in cor(Houlgrave_SF_SST_1981_2000, ssta_sst[c(181:360, 1:180), ,  : 
  incompatible dimensions.".

另外,这是我的等高线图代码,

require(maps)
par(ask=TRUE)
for (i in 1:230) {
    maps::map(database="world", fill=TRUE, col="light blue")
    maps::map.axes()
    contour(x=lon_sst, y=lat_sst, z=cor(Houlgrave_SF_1981_2000,ssta_sst[c(181:360, 1:180),,i]), zlim=c(-3,3), add=TRUE)
    title(paste("Year: ", year_sst[i], ", Month: ", month_sst[i]))
}

我想我只需要修改等高线代码下的 z 即可。是否需要将每个A的数据重新定义为360*180的数据矩阵?

I have two data sets A and B and i wanna to find the correlation and plot the contour map.

A is just a simple vector with 230 stream flow data.

B is a complicated sea surface temperature(SST) data under a series date. On each date, the SST has a matrix of 360row *180columns of recorded temperatures.

The vector A (230 data) is :

Houlgrave_flow_1981_2000 = window(Houlgrave_flow_average, start = as.Date("1981-11-15"),end = as.Date("2000-12-15")) 
Houlgrave_SF_1981_2000 = coredata(Houlgrave_flow_1981_2000)

The dimension of matrix B is shown below and i only use from 1 to 230.

> dim(ssta_sst)
[1] 360 180 362

My idea for finding correlation is below.

z_correlation = cor(Houlgrave_SF_SST_1981_2000,ssta_sst[c(181:360, 1:180),,i])

Try, i=1. However, it doesn't work.The error message says:

"Error in cor(Houlgrave_SF_SST_1981_2000, ssta_sst[c(181:360, 1:180), ,  : 
  incompatible dimensions.".

Also, this is my contour map code,

require(maps)
par(ask=TRUE)
for (i in 1:230) {
    maps::map(database="world", fill=TRUE, col="light blue")
    maps::map.axes()
    contour(x=lon_sst, y=lat_sst, z=cor(Houlgrave_SF_1981_2000,ssta_sst[c(181:360, 1:180),,i]), zlim=c(-3,3), add=TRUE)
    title(paste("Year: ", year_sst[i], ", Month: ", month_sst[i]))
}

I think i just need to modify z under contour code. Is it necessary to redefine each A's data as a 360*180 data matrix?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2025-01-05 12:31:51

如果我正确理解这个问题,你有一个时间序列,即一个其索引可以解释为时间的向量,以及一个3维数组,其索引可以解释为时间和位置。

# Sample data
n <- 230
m <- 100
dates <- seq.Date( from=Sys.Date(), length=n, by="day" )
flow <- rnorm(n)
names(flow) <- as.character(dates)
temperatures <- array( rlnorm(n*m*m), dim=c(n,m,m) )
dimnames( temperatures ) <- list(
  time = as.character( dates ),
  longitude = NULL,
  latitude = NULL
)

对于每个位置,您可以使用 applyu,在下面的代码中)之间的相关性>。

correlations <- apply( 
  temperatures, 
  2:3, 
  function (u) cor(u, flow) 
)
image(correlations)

If I understand the problem correctly, you have a time series, i.e., a vector whose index can be interpreted as time, and a 3-dimensional array, whose indices can be interpreted as time and position.

# Sample data
n <- 230
m <- 100
dates <- seq.Date( from=Sys.Date(), length=n, by="day" )
flow <- rnorm(n)
names(flow) <- as.character(dates)
temperatures <- array( rlnorm(n*m*m), dim=c(n,m,m) )
dimnames( temperatures ) <- list(
  time = as.character( dates ),
  longitude = NULL,
  latitude = NULL
)

For each position, you can compute the correlation between your "flow" time series and the "temperature" time series (u, in the code below) for that position, using apply.

correlations <- apply( 
  temperatures, 
  2:3, 
  function (u) cor(u, flow) 
)
image(correlations)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文