-
Notifications
You must be signed in to change notification settings - Fork 0
/
p-splines.r
147 lines (120 loc) · 3.48 KB
/
p-splines.r
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
library(nimble)
library(splines)
plot(mpg ~ hp, data = mtcars)
# https://bragqut.github.io/2016/05/24/samclifford-splines/
bspline <- function(x, K, bdeg = 3, cyclic = FALSE,
xl = min(x), xr = max(x)) {
x <- as.matrix(x, ncol = 1)
ndx <- K - bdeg
# as outlined in Eilers and Marx (1996)
dx <- (xr - xl) / ndx
t <- xl + dx * (-bdeg:(ndx + bdeg))
T <- (0 * x + 1) %*% t
X <- x %*% (0 * t + 1)
P <- (X - T) / dx
B <- (T <= X) & (X < (T + dx))
r <- c(2:length(t), 1)
for (k in 1:bdeg){
B <- (P * B + (k + 1 - P) * B[, r]) / k
}
B <- B[, 1:(ndx + bdeg)]
if (cyclic == 1) {
for (i in 1:bdeg){
B[, i] <- B[, i] + B[, K - bdeg + i]
}
B <- B[, 1:(K - bdeg)]
}
return(B)
}
x <- seq(0, 400, 0.1)
xb <- bspline(x, 10)
matplot(xb, type = "l")
b <- bspline(mtcars$hp, 10)
a <- solve(t(b) %*% b) %*% t(b) %*% mtcars$mpg
newmpg <- b %*% a
plot(x = mtcars$hp, y = mtcars$mpg)
lines(x = sort(mtcars$hp), y = newmpg[order(mtcars$hp)])
# Too wiggly! Let's penalise.
d1_model <- nimbleCode({
for (i in 1:N) {
mu[i] <- inprod(beta[1:K, i], alpha[1:K])
y[i] ~ dnorm(mean = mu[i],
sd = sigma_y)
}
alpha[1] ~ dnorm(0, sd = sigma_a)
for (k in 2:K) {
alpha[k] ~ dnorm(alpha[k - 1], sd = sigma_a)
}
sigma_y ~ dgamma(0.1, 0.1)
sigma_a ~ dgamma(0.1, 0.1)
})
d1_const <- list(beta = t(b), N = nrow(mtcars), K = ncol(b))
d1_data <- list(y = mtcars$mpg)
d1_samples <- nimbleMCMC(
code = d1_model, const = d1_const,
data = d1_data,
niter = 5000,
monitors = c("alpha")
)
alpha_est <- apply(d1_samples, 2, median)
d1_est <- b %*% alpha_est
lines(sort(mtcars$hp), y = d1_est[order(mtcars$hp)],
col = 2)
d2_model <- nimbleCode({
for (i in 1:N) {
mu[i] <- inprod(beta[1:K, i], alpha[1:K])
y[i] ~ dnorm(mean = mu[i],
sd = sigma_y)
}
alpha[1] ~ dnorm(0, sd = sigma_a)
alpha[2] ~ dnorm(alpha[1], sd = sigma_a)
for (k in 3:K) {
alpha[k] ~ dnorm(
mean = -2 * alpha[k - 1] + alpha[k - 2],
sd = sigma_a)
}
sigma_y ~ dgamma(0.1, 0.1)
sigma_a ~ dgamma(0.1, 0.1)
})
d2_const <- list(beta = t(b), N = nrow(mtcars), K = ncol(b))
d2_data <- list(y = mtcars$mpg)
d2_samples <- nimbleMCMC(
code = d2_model, const = d2_const,
data = d2_data,
niter = 5000,
monitors = c("alpha")
)
alpha_est <- apply(d2_samples, 2, median)
d2_est <- b %*% alpha_est
lines(sort(mtcars$hp), y = d2_est[order(mtcars$hp)],
col = 3)
qvar_model <- nimbleCode({
for (i in 1:N) {
mu[i] <- inprod(beta[1:K, i], alpha[1:K])
y[i] ~ dnorm(mean = mu[i], sd = sigma_y)
}
alpha[1:K] ~ dmnorm(zeros[1:K], prec[1:K, 1:K])
sigma_y ~ dgamma(0.1, 0.1)
lambda ~ dgamma(0.1, 0.1)
prec[1:K, 1:K] <- lambda * Q[1:K, 1:K]
})
make_q <- function(degree, k, epsilon = 1e-3) {
x <- diag(k)
e <- diff(x, differences = degree)
return(t(e) %*% e + x * epsilon)
}
q <- make_q(2, ncol(b))
qvar_const <- list(beta = t(b), N = nrow(mtcars), K = ncol(b),
Q = q, zeros = rep(0, ncol(b)))
qvar_data <- list(y = mtcars$mpg)
qvar_samples <- nimbleMCMC(
code = qvar_model, const = qvar_const,
data = qvar_data,
niter = 15000,
monitors = c("alpha")
)
alpha_est <- apply(qvar_samples, 2, median)
q_est <- b %*% alpha_est
lines(sort(mtcars$hp), y = q_est[order(mtcars$hp)],
col = 4)
legend("topright", legend = c("None", "1", "2", "Q"), col = 1:4, lty = 1)