-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path05-Spatial-GAM.Rmd
157 lines (138 loc) · 4.3 KB
/
05-Spatial-GAM.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
---
title: "Nonparametric Analysis of US Dairy Production and Consumption"
subtitle: "Spatial GAM"
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(ISLR2)
library(car)
library(sp)
library(mgcv)
library(rgl)
library(splines)
library(pbapply)
library(devtools)
library(visreg)
library(ggplot2)
library(mgcViz)
```
```{r}
source("05-Prepare-data-spatial.R", echo=F)
```
# Model 1: PREDICT ON INTERACTION BETWEEN COORDINATES
```{r}
mod2d <- mgcv::gam(log(Value) ~ s(y,x), data = all_sales_county_2007, method = "REML")
summary(mod2d)
par(mfrow=c(1,1))
plot(mod2d,scheme=2,pages=0,select = 1,
main = "Smooth coefficients of interaction",
xlab = "longitude",ylab = "latitude")
b <- getViz(mod2d)
pl <- plot(sm(b, 1)) + l_fitRaster() + l_fitContour() +
geom_polygon(data = map_data ("state"),
aes(x=long, y = lat,group=group),
fill=NA,color="black",lwd = 0.7,inherit.aes = F) +
ggtitle("Smooth coefficients of interaction") +
xlab("Longitude") + ylab("Latitude")
pl
x_grid <- map_data ("state")$long
y_grid <- map_data ("state")$lat
```
```{r,warning=FALSE}
# Make the perspective plot with error surfaces
vis.gam(mod2d, view = c("y", "x"),
plot.type = "persp", se = 2)
# Rotate the same plot
vis.gam(mod2d, view = c("y", "x"),
plot.type = "persp", se = 2, theta = 135)
# Make plot with 10% extrapolation
vis.gam(mod2d, view = c("y", "x"),
plot.type = "contour", too.far = 0.25)
dat1 <- all_sales_county_2007
coordinates(dat1) <- c("y","x")
points(dat1)
```
# Model 2: PREDICT ON INTERACTION BETWEEN COORDINATES AND POPULATION
```{r}
tensor_mod2 <- gam(log(Value) ~ s(y, x)+s(log(Population)),
data = all_sales_county_2007, method = "REML")
# Summarize and plot
summary(tensor_mod2)
plot(tensor_mod2,pages=1)
par(mfrow=c(1,2))
plot(tensor_mod2,scheme=2,pages=0,select = 1,
main = "Smooth coefficients of interaction",
xlab = "longitude",ylab = "latitude")
plot(tensor_mod2,scheme=2,pages=0,select = 2,
main = "Population effect", ylab = "s(log(Population))")
plot(tensor_mod2, scheme = 2, pages =0,select=1)
par(mfrow=c(1,1))
plot(tensor_mod2,scheme=2,pages=0,select = 1,
main = "Smooth coefficients of interaction",
xlab = "longitude",ylab = "latitude")
b <- getViz(tensor_mod2)
pl <- plot(sm(b, 1)) + l_fitRaster() + l_fitContour() +
geom_polygon(data = map_data ("state"),
aes(x=long, y = lat,group=group),
fill=NA,color="black",lwd = 0.7,inherit.aes = F) +
ggtitle("Smooth coefficients of interaction") +
xlab("Longitude") + ylab("Latitude")
pl
```
```{r,warning=FALSE}
vis.gam(tensor_mod2, view = c("y", "x"),
plot.type = "persp", se = 2)
# Rotate the same plot
vis.gam(tensor_mod2, view = c("y", "x"),
plot.type = "persp", se = 2, theta = 135)
# Make plot with 10% extrapolation
vis.gam(tensor_mod2, view = c("y", "x"),
plot.type = "contour", too.far = 0.25)
points(dat1)
visreg(tensor_mod2)
```
# Prediction
```{r,results='hide',message=FALSE,warning=FALSE}
x_grid <- seq(-118,-104, by = 0.01)
y_grid <- seq(36,43, by = 0.01)
grid <- expand.grid(x_grid,y_grid)
grid <- data.frame(x=grid$Var2, y=grid$Var1)
preds <- predict(mod2d,newdata=grid)
preds
df <- cbind(grid,"log(Sales)" =as.data.frame(preds))
ggplot(as.data.frame(df), aes(y,x, col=preds)) +
geom_point(alpha=0.5) +
scale_color_gradient(low="red", high="yellow") +
coord_cartesian(xlim=c(-115.5, -108), ylim = c(37, 42))+
geom_polygon( data=map_data("county"), aes(x=long, y=lat, group=group),
color="black", fill="lightblue", alpha=0.1 ) +
guides(color = guide_legend(title = "log(Sales)"))
```