![](/img/trans.png)
[英]Speed-up a parallel process calculating a mann-kendall test over a huge dataset in R
[英]In R, how do I create individual time series (to loop trough and run a Mann-Kendall test) from a larger dataset?
我開始自動化我們用於水質數據時間序列數據的 Mann-Kendall 測試。 之前是用Excel做的,但是復制粘貼太多了。 我想創建一個 R 腳本來運行 MK 測試(包“趨勢”)並計算 Sens Slope。 我有以下示例表,其中包含我們的水質數據:
| Site | Program | Year | parameter1 | parameter2 |
|------|---------|------|-----|-----|
| A | ABC | 1990 | 5 | 100 |
| A | ABC | 1991 | 10 | 75 |
| A | ABC | 1992 | 15 | 50 |
| A | ABC | 1993 | 20 | 25 |
| A | ABC | 1994 | 25 | 5 |
| B | ABC | 1990 | 10 | 88 |
| B | ABC | 1991 | 20 | 44 |
| B | ABC | 1992 | 30 | 22 |
| B | ABC | 1993 | 40 | 11 |
| B | ABC | 1994 | 50 | 6 |
| C | XYZ | 1990 | 6 | 64 |
| C | XYZ | 1991 | 12 | 44 |
| C | XYZ | 1992 | 18 | 24 |
| C | XYZ | 1993 | 24 | 14 |
| C | XYZ | 1994 | 30 | 4 |
| D | XYZ | 1990 | 7 | 99 |
| D | XYZ | 1991 | 14 | 88 |
| D | XYZ | 1992 | 21 | 77 |
| D | XYZ | 1993 | 28 | 66 |
| D | XYZ | 1994 | 35 | 55 |
我需要為每個參數(ANC 和 SO4)取出數據中的每個時間序列(因此對於站點 A、B、C、D)並在 R 中運行 MannKendall 測試(下面的代碼)。 我需要一個如下所示的輸出表,但填充了 MK 統計數據和 sens 斜率(而不是如下所示的 1)。
| Site | Program | Parameter | MK Statistic | Sens Slope |
|------|---------|-----------|--------------|------------|
| A | ABC | ANC | 1 | 1 |
| A | ABC | SO4 | 1 | 1 |
| B | ABC | ANC | 1 | 1 |
| B | ABC | SO4 | 1 | 1 |
| C | XYZ | ANC | 1 | 1 |
| C | XYZ | SO4 | 1 | 1 |
| D | XYZ | ANC | 1 | 1 |
| D | XYZ | SO4 | 1 | 1 |
關於如何生成此輸出表的任何想法? 我知道它在某個時候需要一個循環,但不完全確定從哪里開始。 也許每個站點、程序都有一個,然后是 ANC 或 S04。 下面的 R 代碼來自一個單獨的站點和參數組合,但是對於我們擁有的 100 個站點和 6 個水質參數,這將是一個痛苦的復制。
install.packages("trend")
library("trend")
#put our data in a time series (but this only creates 1 site and its time series)
time_series <- ts(Trends$parameter1, start=c(1990, 1), end=c(1994, 1), frequency=1)
print(time_series)
#Run the MK Test and Sens Slope from package trend
mk.test(time_series, alternative = c("two.sided", "greater", "less"),
continuity = TRUE)
sens.slope(time_series, conf.level = 0.95)
輸出示例 - 這些是來自我的實際數據的結果,而不是示例數據集(因為我沒有在示例數據中的所有站點上成功運行 MKtest)。 下面帶有 ^^^^ 的數字是我需要用於最終輸出表的數字。
> mk.test(time_series , alternative = c("two.sided", "greater", "less"),
+ continuity = TRUE)
Mann-Kendall trend test
data: time_series
z = -5.7308, n = 26, p-value = 9.996e-09
alternative hypothesis: true S is not equal to 0
sample estimates:
S varS tau
-261.0000000 2058.3333333 -0.8030769
^^^^^^^^^^^^
> sens.slope(time_series , conf.level = 0.95)
Sens slope
data: time_series
z = -5.7308, n = 26, p-value = 9.996e-09
alternative hypothesis: true z is not equal to 0
95 percent confidence interval:
-1.3187075 -0.9495238
sample estimates:
Sens slope
-1.136842
^^^^^^^^^
我們可以通過“站點”將數據集split
為data.frames
list
並應用測試
library(trend)
lst1 <- split(Trends[c("parameter1", "parameter2")], Trends$Site)
out <- lapply(lst1, function(dat)
lapply(dat, function(para) {
time_series <- ts(para, start=c(1983, 1), end=c(2018, 1), frequency=1)
tsout <- mk.test(time_series, alternative = c("two.sided",
"greater", "less"), continuity = TRUE)
sensout <- sens.slope(time_series, conf.level = 0.95)
list(tsout = tsout, sensout = sensout)
}
))
-輸出
out$A$parameter1
#$tsout
# Mann-Kendall trend test
#data: time_series
#z = 0.57147, n = 36, p-value = 0.5677
#alternative hypothesis: true S is not equal to 0
#sample estimates:
# S varS tau
#4.200000e+01 5.147333e+03 7.352146e-02
#$sensout
# Sen's slope
#data: time_series
#z = 0.57147, n = 36, p-value = 0.5677
#alternative hypothesis: true z is not equal to 0
#95 percent confidence interval:
# 0.0000 0.3125
#sample estimates:
#Sen's slope
# 0
out$D$parameter2
#$tsout
# Mann-Kendall trend test
#data: time_series
#z = -0.57147, n = 36, p-value = 0.5677
#alternative hypothesis: true S is not equal to 0
#sample estimates:
# S varS tau
# -42.00000000 5147.33333333 -0.07352146
#$sensout
# Sen's slope
#data: time_series
#z = -0.57147, n = 36, p-value = 0.5677
#alternative hypothesis: true z is not equal to 0
#95 percent confidence interval:
# -0.6875 0.0000
#sample estimates:
#Sen's slope
# 0
Trends <- structure(list(Site = c("A", "A", "A", "A", "A", "B", "B", "B",
"B", "B", "C", "C", "C", "C", "C", "D", "D", "D", "D", "D"),
Program = c("ABC", "ABC", "ABC", "ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ", "XYZ",
"XYZ", "XYZ", "XYZ", "XYZ"), Year = c(1990L, 1991L, 1992L,
1993L, 1994L, 1990L, 1991L, 1992L, 1993L, 1994L, 1990L, 1991L,
1992L, 1993L, 1994L, 1990L, 1991L, 1992L, 1993L, 1994L),
parameter1 = c(5L, 10L, 15L, 20L, 25L, 10L, 20L, 30L, 40L,
50L, 6L, 12L, 18L, 24L, 30L, 7L, 14L, 21L, 28L, 35L), parameter2 = c(100L,
75L, 50L, 25L, 5L, 88L, 44L, 22L, 11L, 6L, 64L, 44L, 24L,
14L, 4L, 99L, 88L, 77L, 66L, 55L)), class = "data.frame", row.names = c(NA,
-20L))
我有一種使用 tidyverse 的不同方法。 到目前為止,它可能有點超出您的經驗,但我建議您檢查一下,因為當您來自 excel 時,我發現它比基礎 R 更易於使用
library(dplyr)
library(ggplot2)
library(tidyr)
library(purrr)
install.packages("trend")
library("trend")
out <- dat %>% gather(parameter, value, ANC, SO4) %>%
group_by(parameter, Site) %>% nest() %>%
mutate(ts_out = map(data, ~ts(.x$value, start=c(1990, 1), end=c(1994, 1), frequency=1))) %>%
mutate(mk_res = map(ts_out, ~mk.test(.x, alternative = c("two.sided", "greater", "less"),
continuity = TRUE)),
sens = map(ts_out, ~sens.slope(.x, conf.level = 0.95))) %>%
mutate(mk_stat = map_dbl(mk_res, ~.x$p.value),
sens_stat = map_dbl(sens, ~.x$p.value)) %>%
select(parameter, Site, mk_stat, sens_stat)
out
# A tibble: 8 x 4
parameter Site mk_stat sens_stat
<chr> <fct> <dbl> <dbl>
1 ANC " A " 0.0275 0.0275
2 ANC " B " 0.0275 0.0275
3 ANC " C " 0.0275 0.0275
4 ANC " D " 0.0275 0.0275
5 SO4 " A " 0.0275 0.0275
6 SO4 " B " 0.0275 0.0275
7 SO4 " C " 0.0275 0.0275
8 SO4 " D " 0.0275 0.0275
這給出了表中的輸出。 我不確定這是否是您想從測試中取出的部分,但這應該更容易更改
我建議查看每個步驟的輸出以了解結構。 這種分析風格的一個很好的資源是R for Data Science Many Models Chapter
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.