-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path05-Spatial-Nonparametric.Rmd
162 lines (133 loc) · 4.58 KB
/
05-Spatial-Nonparametric.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
---
title: "Nonparametric Analysis of US Dairy Production and Consumption"
subtitle: "Spatial Nonparametric"
author:
- "Teo Bucci^[[email protected]]"
- "Filippo Cipriani^[[email protected]]"
- "Gabriele Corbo^[[email protected]]"
- "Andrea Puricelli^[[email protected]]"
output:
pdf_document:
toc: true
toc_depth: 3
number_section: true
#keep_md: TRUE
html_document:
toc: true
toc_float: true
number_sections: true
date: "2023-02-17"
editor_options:
chunk_output_type: inline
---
```{r setup, echo = FALSE}
knitr::opts_chunk$set(
echo = TRUE,
dev = c('pdf'),
fig.align = 'center',
fig.path = 'output/',
fig.height = 6,
fig.width = 12
)
```
# Load libraries and data
```{r, message=FALSE}
library("readxl")
library(sf)
library(maps)
library(ggspatial)
library(tidyr)
library(raster)
library(sp)
library(npsp)
library(ggplot2)
```
```{r}
source("05-Prepare-data-spatial.R",echo=FALSE)
```
# Study of the Dairy prodction in 2007
Using log values of the population and the dairy sales to avoid scalability problems and setting up the coordinates for spatial analysis
```{r}
dat1 <- all_sales_county_2007
dat1 <- dat1[-c(1,2,3)]
log_pop <- log(dat1$Population)
log_value <- log(dat1$Value)
log_dat <- as.data.frame(cbind(log_pop,log_value,"x" = dat1$x, "y" = dat1$y))
coordinates(log_dat) <- c("y","x")
scattersplot(log_dat, main = 'Milk and cheese sales', xlab = 'Longitude',
ylab = 'Latitude', zlab = 'Sales')
```
# Trend estimates for log_value
Plotting the estimates of the model using a local linear estimator
```{r,warning=FALSE}
attr_prec <- attributes(precipitation)
border <- attr_prec$border
interior <- attr_prec$interior
x <- coordinates(log_dat)
y <- log_dat$log_value
lp <- locpol(x, y, nbin = c(120, 120), h = diag(c(5, 5)))
lp <- mask(lp, window = border)
attr <- attributes(log_dat)
slim <- range(y)
col <- jet.colors(256)
simage(lp, slim = slim, main = "Trend estimates",
col = col, xlab = "Longitude", ylab = "Latitude", asp = 1)
plot(border, border = "darkgray", lwd = 2, add = TRUE)
plot(interior, border = "lightgray", lwd = 1, add = TRUE)
```
# Bandwidth selection
Tuning of the bandwidth using CV
```{r,warning=FALSE}
bin <- binning(x, y, window = border)
lp0.h <- h.cv(bin)$h
lp0 <- locpol(bin, h = lp0.h, hat.bin = TRUE)
lp0 <- mask(lp0, window = border)
simage(lp0, slim = slim, main = "Trend estimates",
col = col, xlab = "Longitude", ylab = "Latitude", asp = 1)
plot(border, border = "darkgray", lwd = 2, add = TRUE)
plot(interior, border = "lightgray", lwd = 1, add = TRUE)
```
# Variogram estimation
Plotting the estimates of the bias-corrected semivariogram
```{r,warning=FALSE}
lp0 <- locpol(bin, h = lp0.h, hat.bin = TRUE)
svar.bin <- svariso(x, residuals(lp0), nlags = 40, maxlag = 45)
svar.h <- h.cv(svar.bin)$h
svar.np <- np.svar(svar.bin, h = svar.h)
svar.np2 <- np.svariso.corr(lp0, nlags = 40, maxlag = 45,
h = svar.h, plot = FALSE)
svm0 <- fitsvar.sb.iso(svar.np, dk = 0)
svm1 <- fitsvar.sb.iso(svar.np2, dk = 0)
plot(svm1, main = "Nonparametric bias-corrected semivariogram\nand fitted models",
legend = FALSE, xlim = c(0,max(coords(svar.np2))),
ylim = c(0,max(svar.np2$biny, na.rm = TRUE)))
plot(svm0, lwd = c(1, 1), add = TRUE)
plot(svar.np, type = "p", pch = 2, add = TRUE)
# abline(h = c(svm1$nugget, svm1$sill), lty = 3)
# abline(v = 0, lty = 3)
legend("bottomright", legend = c("corrected", 'biased'),
lty = c(1, 1), pch = c(1, 2), lwd = c(1, 1))
```
# Automatic modelling
\texttt{np.fitgeo()} repeats the same procedure all in one step
```{r,warning=FALSE}
geomod <- np.fitgeo(x, y, nbin = c(30, 30), maxlag = 45,
svm.resid = TRUE, window = border)
plot(geomod)
```
# Kriging
Using the model estimates and the semivariogram we can use a kriging prediction method
```{r}
krig.grid <- np.kriging(geomod, ngrid = c(120, 120))
# Plot kriging predictions and kriging standard deviations
old.par <- par(mfrow = c(1,2), omd = c(0.0, 0.98, 0.0, 1))
krig.grid2 <- krig.grid
simage(krig.grid2, 'kpred', main = 'Kriging predictions', slim = slim,
xlab = "Longitude", ylab = "Latitude" , col = col, asp = 1)
plot(border, border = "darkgray", lwd = 2, add = TRUE)
plot(interior, border = "lightgray", lwd = 1, add = TRUE)
simage(krig.grid2, 'ksd', main = 'Kriging sd',
xlab = "Longitude", ylab = "Latitude" , col = hot.colors(256), asp = 1)
plot(border, border = "darkgray", lwd = 2, add = TRUE)
plot(interior, border = "lightgray", lwd = 1, add = TRUE)
```