Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用环路在多个站点上执行Mann-kendall测试?_R_Loops_Time Series_Lapply - Fatal编程技术网

R 如何使用环路在多个站点上执行Mann-kendall测试?

R 如何使用环路在多个站点上执行Mann-kendall测试?,r,loops,time-series,lapply,R,Loops,Time Series,Lapply,我刚刚开始使用R,并希望使用modifiedmk包对每月的地下水位数据进行测试。我的dataframe GL看起来像这样 GL well year month value 684 1994 Jan 8.53 684 1995 Jan 8.74 684 1996 Jan 8.88 684 1997 Jan 8.24 1001 2000 Jan 9.1 1001 200

我刚刚开始使用R,并希望使用modifiedmk包对每月的地下水位数据进行测试。我的dataframe GL看起来像这样

GL

well    year    month   value
684     1994    Jan     8.53
684     1995    Jan     8.74
684     1996    Jan     8.88
684     1997    Jan     8.24
1001    2000    Jan     9.1
1001    2001    Jan     9.2
1001    2002    Jan     9.54
1001    2003    Jan     9.68
2003    1981    Jan     55.2
2003    1982    Jan     55.8
2003    1983    Jan     56.4
2003    1984    Jan     53.2
首先,我创建了一个井列表和一个用于打印结果的结果列表文件

well_list <- unique(GL$well)
results_list <- vector("list", length(well_list))
然后我创建了一个我认为是循环的东西

for(i in well_list){
  results_list[[grep(i, well_list)]] <- MannKendall(GL[,4])
}
names(results_list) <- well_list
但我一直犯这个错误

Error in Kendall(1:length(x), x) : length(x)<3
我可以让这个代码工作,我从另一个帖子在这里,但我不想执行预白化方法

for(i in well_list){
tempDF <- GL1[GL$well == 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)
results_list[[grep(i, well_list)]] <- MannKendall(autocordata[,5])}
names(results_list) <- well_list
也许使用split/Lappy更容易阅读

首先,运行测试

sp <- split(GL, GL$well)
results_list <- lapply(sp, function(DF){
  tryCatch(MannKendall(DF[, 4]),
           error = function(e) e)
})

像这样的事情应该可以。“拆分”沿“井”列拆分,为每个井创建一个带有向量的列表。仅保留长度为3或更多的向量。然后使用lappy在剩余的每个向量上运行MannKendall


嗨,伙计们,太棒了!第一个解决方案有效,但不幸的是,我仍然有错误的问题。第二种解决方案有效,但没有错误。感谢堆-我一直在努力找出这个问题,看看它是多么简单!
bad <- sapply(results_list, inherits, "error")
results_list[bad]
#named list()

results_list[!bad]
#$`684`
#tau = 0, 2-sided pvalue =1
#
#$`1001`
#tau = 1, 2-sided pvalue =0.089429
#
#$`2003`
#tau = 0, 2-sided pvalue =1
library(Kendall)

tt <- read.table(text="
well    year    month   value
684     1994    Jan     8.53
684     1995    Jan     8.74
684     1996    Jan     8.88
684     1997    Jan     8.24
1001    2000    Jan     9.1
1001    2001    Jan     9.2
1001    2002    Jan     9.54
1001    2003    Jan     9.68
2003    1981    Jan     55.2
2003    1982    Jan     55.8
2003    1983    Jan     56.4
2003    1984    Jan     53.2
2004    1984    Jan     53.2", header=TRUE)

tt.wells <- split(tt$value, tt$well)
tt.wells <- tt.wells[lengths(tt.wells) >= 3]

lapply(tt.wells, MannKendall)

# $`684`
# tau = 0, 2-sided pvalue =1

# $`1001`
# tau = 1, 2-sided pvalue =0.089429

# $`2003`
# tau = 0, 2-sided pvalue =1