forked from SPATIAL-Lab/isorig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQA_function_v3.R
98 lines (82 loc) · 3.76 KB
/
QA_function_v3.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
QA <- function(isoscape, known, valiStation, valiTime, setSeed = T){
#check that isoscape is valid and has defined CRS
if (class(isoscape) == "RasterStack" | class(isoscape) == "RasterBrick") {
if (is.na(proj4string(isoscape))) {
stop("isoscape must have valid coordinate reference system")
}
} else {
stop("isoscape should be a RasterStack or RastrBrick")
}
#check that known is valid and has defined, correct CRS
if (class(known) != "SpatialPointsDataFrame") {
stop("known should be a SpatialPointsDataFrame, see help page of calRaster function")
} else if (is.na(proj4string(known))) {
stop("known must have valid coordinate reference system")
} else if(proj4string(known) != proj4string(isoscape)){
stop("known must have same coordinate reference system as isoscape")
} else if(ncol(known@data) != 1){
stop("known must include a 1-column data frame containing only the isotope values")
}
if(!valiStation<nrow(known)){
stop("valiStation must be smaller than the number of known-origin stations in known")
}
if(setSeed == T){
set.seed(100)
}
rowLength <- nrow(known)
val_stations <- sort(sample(1:rowLength,valiStation,replace = F))
for (i in 1:(valiTime-1)){
val_stations <- rbind(val_stations,sort(sample(1:rowLength,valiStation,replace = F)))
}
stationNum4model <- rowLength - valiStation
prption_byProb <- matrix(0, valiTime, 99) # accuracy by checking top percentage by cumulative prob.
prption_byArea <- matrix(0, valiTime, 99) # accuracy by checking top percentage by area
pd_bird_val <- matrix(0, valiTime, valiStation) # pd value for each validation location
precision <- list() # precision
# create progress bar
pb <- txtProgressBar(min = 0, max = valiTime, style = 3)
for (i in 1:valiTime){
bird_val <- known[val_stations[i,],]
bird_model <- known[-val_stations[i,],]
rescale <- isOrigin::calRaster(bird_model, isoscape, sdMethod = 1, genplot = F, savePDF = F, verboseLM = F)
pd <- isOrigin::pdRaster(rescale, unknown = data.frame(row.names(bird_val@data), bird_val@data[,1]), genplot = F, saveFile = F)
# pd value for each validation location
for(m in 1:nlayers(pd)){
pd_bird_val[i, m] <- raster::extract(pd[[m]], bird_val[m,], method = "bilinear")
}
xx <- seq(1, 99, 1) ## 1 to 99
# total area
Tarea <- length(na.omit(pd[[1]][]))
# accuracy and precision by checking top percentage by cumulative prob.
precision[[i]] <- matrix(0, 99, valiStation) # precision
for(j in xx){
qtl <- isOrigin::qtlRaster(pd, threshold = j/100, pdf = F, thresholdType = 1,genplot = F)
prption_byProb[i, j] <- 0
for(k in 1:nlayers(qtl)){
prption_byProb[i, j] <- prption_byProb[i, j] +
raster::extract(qtl[[k]], bird_val[k,], method = "bilinear")
precision[[i]][j, k] <- sum(na.omit(qtl[[k]][]))/Tarea # precision
}
}
for(k in 1:nlayers(qtl)){
precision[[i]][j, k] <- sum(na.omit(qtl[[k]][]))/Tarea # precision
}
# accuracy by checking top percentage by cumulative area
for(n in xx){
qtl <- isOrigin::qtlRaster(pd, threshold = n/100, pdf = F, thresholdType = 2,genplot = F)
prption_byArea[i, n] <- 0
for(k in 1:nlayers(qtl)){
prption_byArea[i, n] <- prption_byArea[i, n] +
raster::extract(qtl[[k]], bird_val[k,], method = "bilinear")
}
}
#update progress bar
Sys.sleep(0.1)
setTxtProgressBar(pb, i)
}
random_prob_density=1/length(na.omit(getValues(isoscape[[1]])))
result <- list(val_stations, pd_bird_val, prption_byArea, prption_byProb, precision, random_prob_density)
names(result) <- c("val_stations", "pd_bird_val", "prption_byArea", "prption_byProb", "precision", "random_prob_density")
class(result) <- "QA"
return(result)
}