我有一个非常长的数据帧,有200个站号。这里给出了样本数据。
让样本数据为df
。现在
我想检查每个站号的滞后1的自动相关性。在预白化之后执行预白化并计算每个站的Mann-kendall趋势。我可以使用下面的代码为一个单独的电台做。
你能不能帮助我如何立即为所有车站执行此操作。
数据框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)
我用于个人电台计算的代码
INSERT INTO c_nodes(kb_nid,runState,startedTime)
SELECT n.id, n.nState>10 as runState, NULL as startedTime
FROM node n
LEFT JOIN c_nodes cn ON n.id =cn.kb_nid
GROUP BY n.id
ON DUPLICATE KEY UPDATE startedTime=IF(cn.runState<>VALUES(runState) ,NOW(),cn.startedTime)
那么我如何能够同时对同一数据帧上的所有站号执行prewhitening和mankendall测试。 谢谢。