-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathmatern.R
More file actions
36 lines (32 loc) · 952 Bytes
/
matern.R
File metadata and controls
36 lines (32 loc) · 952 Bytes
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
library(TMB)
compile("matern.cpp")
dyn.load(dynlib("matern"))
## From package 'geoR':
matern <- function (u, phi, kappa)
{
if (is.vector(u))
names(u) <- NULL
if (is.matrix(u))
dimnames(u) <- list(NULL, NULL)
uphi <- u/phi
uphi <- ifelse(u > 0, (((2^(-(kappa - 1)))/ifelse(0, Inf,
gamma(kappa))) * (uphi^kappa) * besselK(x = uphi, nu = kappa)),
1)
uphi[u > 600 * phi] <- 0
return(uphi)
}
## Test:
n <- 100
set.seed(123)
x <- matrix(runif(2*n, 0, 10), n)
D <- as.matrix(dist(x))
C <- matern(D, phi=1.3, kappa=2.7)
x <- t(chol(C)) %*% rnorm(n)
data <- list(x=x, D=D)
parameters <- list(phi=.5, kappa=.5)
map <- NULL
################################################################################
obj <- MakeADFun(data, parameters, map=map, DLL="matern")
system.time( fit <- nlminb(obj$par, obj$fn, obj$gr, obj$he) )
system.time( rep <- sdreport(obj, fit$par, obj$he(fit$par)) )
print(rep)