-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovariances.Rmd
90 lines (69 loc) · 3.16 KB
/
covariances.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
---
title: "covariances"
author: Caroline Owens
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(convoSPAT)
library(tidyverse)
library(geoR)
library(RandomFields)
setwd("G:/Shared drives/PSTAT-Geospatial_Model_Selection")
```
##Selecting a covariance structure for the model
Sophia's work
```{r Getting data and setting up the model}
#Read in cleaned data for SJ basin
sjdata <- data.frame(read.csv("G:/Shared drives/PSTAT-Geospatial_Model_Selection/Data/SJrb_cleaned.csv"))
## Conclusions - Go with 3 optimal Mixing clusters
xc <- data.frame(sjdata$utm.x1,sjdata$utm.x2)
final <- kmeans(xc, 3, nstart = 25)
print(final)
xymix <- final$centers
xy <- sjdata[,c(12,13)]
spdf <- SpatialPointsDataFrame(coords = xy, data = sjdata,
proj4string = CRS("+proj=utm +zone=10 +datum=WGS84"))
NS1 <-NSconvo_fit(sp.SPDF = spdf,
cov.model = "exponential",
mc.locations = xymix,
fit.radius = 13000,
mean.model = spdf$water.content ~ spdf$elev + spdf$aspect)
pred <- predict.NSconvo(NS1,
pred.coords = as.matrix(sjdata[,12:13]),
pred.covariates = as.matrix(sjdata[,c(8,17)])
)
```
```{r Trying out different covariance structures}
##########################################################################
### Choose the covariance structures to evaluate #########################
##########################################################################
structures <- c("exponential", "matern", "cauchy", "circular", "cubic", "gaussian", "spherical", "wave")
##########################################################################
### Initialize table for results #########################################
##########################################################################
results <- matrix(ncol = 3, nrow = length(structures))
colnames(results) <- c("CRPS", "MSPE", "pMSDR")
rownames(results) <- structures
##########################################################################
### Calculate evaluation criteria for each covariance structure ##########
##########################################################################
for (i in 1:length(structures)){
#build the model with the specified covariance structure
model <-NSconvo_fit(sp.SPDF = spdf, #this should be with simulated/'training' data?
cov.model = structures[i],
mc.locations = xymix,
fit.radius = 13000,
mean.model = spdf$water.content ~ spdf$elev + spdf$aspect)
#still using 'training' data, use the model to make predictions for yhats (water content)
pred <- predict.NSconvo(model,
pred.coords = as.matrix(sjdata[,12:13]), #coords of pred sites
pred.covariates = as.matrix(sjdata[,c(8,17)]) #elev, aspect
)
#using holdout/'testing' data, evaluate predictions and store results in table
evals <- evaluate_CV(sjdata[,10], pred$pred.means, pred$pred.SDs)
results[i,1] <- evals$CRPS
results[i,2] <- evals$MSPE
results[i,3] <- evals$pMSDR
}
```