-
Notifications
You must be signed in to change notification settings - Fork 0
/
brms_car.R
130 lines (103 loc) · 3.14 KB
/
brms_car.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
# icar example code
library(mgcv)
library(brms)
# generate spatial data
east <- north <- 1:10
Grid <- expand.grid(east, north)
K <- nrow(Grid)
# set up distance and neighbourhood matrices
distance <- as.matrix(dist(Grid))
W <- array(0, c(K, K))
W[distance == 1] <- 1
# generate the covariates and response data
x1 <- rnorm(K)
x2 <- rnorm(K)
theta <- rnorm(K, sd = 0.05)
phi <- rmulti_normal(
1, mu = rep(0, K), Sigma = 0.4 * exp(-0.1 * distance)
)
eta <- x1 + x2 + phi
prob <- exp(eta) / (1 + exp(eta))
size <- rep(50, K)
y <- rbinom(n = K, size = size, prob = prob)
dat <- data.frame(y, size, x1, x2)
# fit a CAR model to the data
fit <- brm(y | trials(size) ~ x1 + x2, data = dat,
family = binomial(), autocor = cor_car(W),
chains = 4, cores = 4, iter = 5000, warmup = 2000)
plot(marginal_effects(fit))
fitted_values <- fitted(fit)
data <- as.data.frame(cbind(Y = standata(fit)$Y, fitted_values))
library(ggplot2)
ggplot(data) +
geom_point(aes(x = Estimate, y = Y))
fit2 <- brm(y | trials(size) ~ x1 + x2, data = dat,
family = binomial(),
chains = 4, cores = 4, iter = 5000, warmup = 2000)
data2 <- as.data.frame(cbind(Y = standata(fit2)$Y, fitted(fit2)))
ggplot(data2) +
geom_point(aes(x = Estimate, y = Y))
######
make_stancode(rating ~ treat + period + carry + (1|subject),
data = inhaler, family = "cumulative")
library(bayesplot)
bayesplot::mcmc_plot(fit)
pairs(fit)
# trying gp models
dat <- mgcv::gamSim(1, n = 300, scale = 2)
fit1 <- brm(y ~ s(x1, x2, bs = "gp"), dat, chains = 4, cores = 4)
plot(fit1)
me <- marginal_effects(fit1)
plot(me)
ms <- marginal_smooths(fit1)
plot(ms)
fit2 <- gam(y ~ s(x1, x2, bs = "gp"), data = dat)
# the library schoenberg has been taken over by a package for 12 tone music
# the original one has been renamed to "gratia"
# library(schoenberg)
# install.packages('gratia')
remotes::install_github("gavinsimpson/gratia")
draw(fit2)
plot(fit2)
dat <- mgcv::gamSim(1, n = 200, scale = 2)
fit1 <- brm(y ~ s(x1, x2, bs = "gp"), dat, chains = 4, cores = 4)
fit2 <- brm(y ~ gp(x1, x2), dat, chains = 4, cores = 4)
fit3 <- gam(y ~ s(x1, x2, bs = "gp"), data = dat)
fit4 <- brm(y ~ t2(x1, x2), data = dat)
fits <- list(fit1, fit2, fit3, fit4)
saveRDS(fits, file = "testfits.RDS")
ms1 <- marginal_smooths(fit1)
ms2 <- marginal_smooths(fit2)
ms2 <- marginal_effects(fit2)
ms3 <- marginal_effects(fit3)
ms4 <- marginal_smooths(fit4)
plot(ms1)
plot(ms2)
plot(fit3)
plot(ms4)
plot(fit3)
plot(fit2)
# cor_sar
data(oldcol, package = "spdep")
fit0 <- brm(CRIME ~ INC + HOVAL, data = COL.OLD,
chains = 2, cores = 2)
fit1 <- brm(CRIME ~ INC + HOVAL, data = COL.OLD,
autocor = cor_lagsar(COL.nb),
chains = 2, cores = 2)
summary(fit1)
plot(fit1)
fit2 <- brm(CRIME ~ INC + HOVAL, data = COL.OLD,
autocor = cor_errorsar(COL.nb),
chains = 2, cores = 2)
summary(fit2)
plot(fit2)
plt_fitted <- function(fit) {
fitted_values <- fitted(fit)
data <- as.data.frame(cbind(Y = standata(fit)$Y, fitted_values))
plt <- ggplot(data) +
geom_point(aes(x = Estimate, y = Y))
return(plt)
}
plt_fitted(fit0)
plt_fitted(fit1)
plt_fitted(fit2)