-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecotheilsen_modified.R
65 lines (51 loc) · 1.8 KB
/
ecotheilsen_modified.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#' Theil-sen regression for a raster time series,
require("rkt")
require("modifiedmk")
eco.theilsen2 <- function(stacked, dates,
adjust = "none",
method = c("mk", "mk_corrected"),
my_modified = modifiedmk::mmkh3lag) {
adjust <- match.arg(adjust)
method <- match.arg(method)
if(method == "mk")
cat("Using uncorrected MK test\n")
else
cat("Using corrected MK test\n")
cat("Starting...", "\n")
# pre allocate memory
cellnumber <- raster::ncell(stacked)
estimates <- pvalues <- rep(0, cellnumber)
# compute slope and p value in series
for(i in seq_len(cellnumber)) {
temp <- stacked[i]
if(sum(!is.na(temp)) < 4) {
estimates[i] <- NA
pvalues[i] <- NA
} else {
if(method == "mk") {
this_result <- rkt::rkt(dates, temp)
estimates[i] <- this_result[[3]]
pvalues[i] <- this_result[[1]]
} else if(method == "mk_corrected") {
this_result <- my_modified(as.vector(temp))
estimates[i] <- this_result[[7]]
pvalues[i] <- this_result[[2]]
}
}
cat ("\r", ceiling(100 * i / cellnumber), "% ", "completed", sep = "")
}
cat("\n")
ts <- pval <- raster(nrow=nrow(stacked), ncol =ncol(stacked), crs=raster::crs(stacked))
raster::extent(ts) <- raster::extent(pval) <- raster::extent(stacked)
if(adjust != "none") {
cat(paste("Adjusting p values with ", adjust, " method"), "\n")
pvalues <- p.adjust(pvalues, method = adjust)
}
ts[] <- estimates
pval[] <- pvalues
# write output
cat("Writing slope image into workspace...", "\n")
raster::writeRaster(ts, "slope.tif", overwrite = T)
cat("Writing P-value image into workspace...", "\n")
raster::writeRaster(pval, "pvalue.tif", overwrite = T)
}