R 多时间序列的自相关和Mann-Kendall趋势检验
我有一个很长的数据帧,有200个站点。这里给出了示例数据。 让示例数据为R 多时间序列的自相关和Mann-Kendall趋势检验,r,plyr,apply,stat,R,Plyr,Apply,Stat,我有一个很长的数据帧,有200个站点。这里给出了示例数据。 让示例数据为df 我想检查每个站号在滞后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,
df
我想检查每个站号在滞后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)
c抛开我的上述评论不谈,我想这会帮你找到你想要的:
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
stationList您是否可以运行上面的示例代码,并确保它运行时没有错误,例如autocord第二行prewhitseriesThank you Adam。这很有帮助。