【问题标题】:Autocorrelation and Mann-Kendall Trend test for multiple timeseries多个时间序列的自相关和 Mann-Kendall 趋势检验
【发布时间】:2016-02-21 07:17:21
【问题描述】:

我有一个很长的数据框,其中包含 200 个站号。这里给出了样本数据。 设样本数据为df .Now 我想检查每个站号在滞后 1 处的自动相关性。进行预白化并计算预白化后每个站点的 Mann-kendall 趋势。我可以使用下面的代码为一个单独的站点做。 您能帮我如何同时为所有车站执行此操作吗? 数据框df

dput(df)
structure(list(stn_num = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), .Label = c("08BB005", "08CE001", "08CF003"), class = "factor"), 
    year = c(1987L, 1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 
    1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 1980L, 1981L, 1982L, 
    1983L, 1984L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 1991L, 
    1992L, 1993L, 1984L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 
    1991L, 1992L, 1993L, 1994L), value = c(411.2146215, 346.9846995, 
    453.8616438, 435.3561644, 421.4019178, 444.7603825, 454.469589, 
    441.5884932, 339.76, 294.9562842, 371.8939726, 321.7016438, 
    337.7627397, 460.6622951, 513.1084932, 385.4580822, 386.6643836, 
    377.9076503, 440.7849315, 407.7731507, 454.4967123, 458.3259563, 
    421.4032877, 449.3890411, 456.3934247, 450.015847, 400.0569863, 
    1331.70765, 1415.484932, 1589.654795, 1606.709589, 1750.002732, 
    1803.646575, 1729.054795, 1802.509589, 1805.469945, 1711.854795, 
    1574.153425)), .Names = c("stn_num", "year", "value"), class = "data.frame", row.names = c(NA, 
-38L))

我用于单个站计算的代码

c<-acf(df$value,lag.max=1)
dim(c$acf)
c$acf[[2,1,1]]
df$prewhit1<-c$acf[[2,1,1]]*df$value
prewhitseries<-data.frame(with(df, (df$value[-1] - prewhit1[-length(prewhit1)])))
autocordata<-cbind(df,prewhitseries)
MannKendall(autocordata$prewhitseries)

那么我如何一次对同一数据帧上的所有站号执行预白化和 mankendall 测试。 谢谢。

【问题讨论】:

  • 您能否在最后运行上面的示例代码并确保它运行时没有错误,例如autocordata&lt;-cbind(df,prewhitseries)...干杯
  • 第二个上面的评论,prewhitseries&lt;-data.frame(with(df, (df$value[-1] - prewhit1[-length(prewhit1)]))) 行生成了一个只有 37 行的 data.frame,然后您尝试将其绑定到 38 行。此外,我对您的代码如何有点困惑只运行一个站,因为 df 包含两个站的数据,并且您一次计算整个系列的滞后 1 acf。即使 cbind 命令有效,它也不会创建名为 prewhiteseries 的列。最后,如果您使用的是非基本 R 函数 (MannKendall),请指定它们来自的包。

标签: r plyr apply stat


【解决方案1】:

除了我上面的 cmets,我认为这会让你得到你想要的:

stationList <- unique(df$stn_num)
resultsList <- vector("list", length(stationList))
for(i in stationList){
  tempDF <- df[df$stn_num == i, ]
  c<-acf(tempDF$value,lag.max=1)
  t <- dim(c$acf)
  tempDF$prewhit1<-c$acf[[t[1], t[2], t[3]]]*tempDF$value
  prewhitseries<-data.frame(with(tempDF, (tempDF$value[-1] - prewhit1[-length(prewhit1)])))
  autocordata<-cbind(tempDF[-1,],prewhitseries)
  resultsList[[grep(i, stationList)]] <- MannKendall(autocordata[,5])
}
names(resultsList) <- stationList

我从我在循环中创建的 tempDF 中任意删除了一行,因此 cbind 命令将实际工作我不确定您实际上想要在那里做什么。您可以从 apply 系列中获得相同的结果,如果您尝试并行化或需要更高的效率,这可能是您想要的方向。

【讨论】:

  • 谢谢亚当。这有帮助。
猜你喜欢
  • 2019-12-29
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 2021-07-15
  • 2022-08-19
  • 1970-01-01
相关资源
最近更新 更多