-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAWESOME_validation.Rmd
164 lines (129 loc) · 4.83 KB
/
AWESOME_validation.Rmd
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
title: "SoilErosionDB"
author: "Jinshi"
date: "8/12/2020"
output: html_document
---
# Load package
```{r load package, message=FALSE, include=FALSE, echo=FALSE}
# https://stackoverflow.com/questions/4090169/elegant-way-to-check-for-missing-packages-and-install-them/19873732
package_list <- c("cowplot","data.table","dplyr","ggplot2", "lubridate", "leaflet"
,"kableExtra","knitr","ggmap","maps","mapdata","tidyr","sp","ggpubr"
,"readxl")
package_new <- package_list[!(package_list %in% installed.packages()[,"Package"])]
if(length(package_new)) install.packages(package_new)
library(cowplot)
library(data.table)
library(dplyr)
library(ggplot2)
theme_set(theme_bw())
library(lubridate)
library(kableExtra)
library(knitr)
library("ggpubr")
library(tidyr)
library("leaflet")
library(sp)
library(readxl)
```
```{r functions}
country_lat_long_check <- function(sdata){
ggplot(data = counties) +
geom_polygon(aes(x = long, y = lat, group = group),
color = "gray", fill = 'white', alpha = 0.5, size = 0.25) +
guides(fill="none") +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
geom_rect(data = sdata,
mapping=aes(xmin=Lon_min, xmax=Lon_max, ymin=Lat_min, ymax=Lat_max),
color="red", alpha = 0.5) +
labs(title=sdata$Country,
x ="Latitude", y = "Longitude") ->
p_check
print(p_check)}
qc_background <- function (sdata) {
ggplot(sub_data, aes(x=Value)) +
geom_histogram(color="black", fill="gray", bins = 30) +
theme_bw() +
xlab (var_num_col[i]) ->
p_hist
print(p_hist)
}
```
# Setup
```{r preliminaries, message=FALSE, include=FALSE, echo=FALSE, cache=TRUE}
# Set chunks defaults, these options will be applied to all subsequent chunks
knitr::opts_chunk$set(message = TRUE, include = TRUE, echo = FALSE,
fig.height = 4, fig.width = 8)
```
# load data
```{r load data}
wos_summary <- drake::readd(wos_summary)
# SEDB_del <- drake::readd(SEDB_del)
# SEDB_del <- read.csv('data/SoilErosionDB_v2.csv')
counties <- drake::readd(counties)
GlobalMATMAP <- drake::readd(GlobalMATMAP)
IGBP_MODIS <- drake::readd(IGBP_MODIS)
## join and get climate, vegetation information
SEDB_del %>% mutate(Latitude2 = round(Latitude*2)/2,
Longitude2 = round(Longitude*2)/2,
Lat_dif = ifelse(Latitude2 - Latitude >=0, 0.25, -0.25),
Lon_dif = ifelse(Longitude2 - Longitude >=0, 0.25, -0.25),
Latitude2 = Latitude2 - Lat_dif,
Longitude2 = Longitude2 - Lon_dif) %>%
dplyr::select(-Lat_dif, -Lon_dif) ->
SEDB_del
# Get Ecosystem class, MAT and MAP for srdb data
left_join(SEDB_del, IGBP_MODIS, by=c("Latitude2"="Latitude", "Longitude2"="Longitude")) ->
SEDB_del
# Get MAT and MAP
left_join(SEDB_del, GlobalMATMAP, by=c("Latitude2"="Latitude", "Longitude2"="Longitude")) ->
SEDB_del
SEDB_del %>%
dplyr::select(Study_midyear, Latitude, Longitude, MAT, MAP, Study_temp, Study_precip)
# Change Latitude and Longitude to 0.5 resolution for IGBP
```
```{r check potential latitude and longitude input error}
varCountry <- sort(unique(SEDB_del$Country)) # data from XXX countries, all data has country information
sum_country <- data.frame()
for (i in seq(varCountry)) {
subdata <- SEDB_del %>% filter(Country == varCountry[i] & !is.na(Country))
lat_try <- subdata %>% dplyr::select(Latitude) %>% na.omit()
lon_try <- subdata %>% dplyr::select(Longitude) %>% na.omit()
min_lat <- ifelse(nrow(lat_try) == 0, NA, min(lat_try))
max_lat <- ifelse(nrow(lat_try) == 0, NA, max(lat_try))
min_lon <- ifelse(nrow(lon_try) == 0, NA, min(lon_try))
max_lon <- ifelse(nrow(lon_try) == 0, NA, max(lon_try))
Country_name <- varCountry[i]
n_obs <- nrow(subdata) # number of observations in this country
output <- bind_cols(i, Country_name, min_lat, max_lat, min_lon, max_lon, n_obs)
sum_country <- bind_rows(sum_country, output)
print(paste0("-----", i))
}
colnames(sum_country) <- c("ID", "Country", "Lat_min", "Lat_max", "Lon_min", "Lon_max", "obs")
sum_country # view and check potential latitude and longitude input error
for(i in seq(nrow(sum_country))) {
sdata = sum_country[i,]
country_lat_long_check(sdata)
print(paste0("*****", i))
}
```
```{r histgram of numeric records information}
colnames(SEDB_del)
var_num_col <- colnames(SEDB_del)
# create a function to
for(i in seq(var_num_col)){
sub_data = SEDB_del[, which(colnames(SEDB_del) == var_num_col[i])] %>% as.data.frame()
colnames(sub_data) = "Value"
sub_data %>% na.omit() -> sub_data
if(var_num_col[i] %in% c("Unique_ID", "barren_yn", "Study_number")) {next}
else if(is.numeric(sub_data$Value)) {qc_background (SEDB_del)}
else {next}
print(i)
}
```
```{r}
sdata
SEDB_del %>% count(Country)
SEDB_del %>% count(Latitude, Longitude)
```