-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.R
62 lines (60 loc) · 2.29 KB
/
functions.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
#' @title Simulate data from the model.
#' @description Use a continuous covariate x.
#' @return A data frame with the following columns.
#' * `y`: Simulated normal responses.
#' * `x`: A simulated covariate of zeroes and ones.
#' * `beta_true`: The value of the regression coefficient `beta`
#' used to simulate the data.
#' @examples
#' library(tidyverse)
#' simulate_data_continuous()
simulate_data_continuous <- function() {
alpha <- rnorm(1, 0, 1)
beta <- rnorm(1, 0, 1)
sigma <- rhcauchy(1, 1)
x <- rnorm(100, 1, 1) # continuous covariate
y <- rnorm(100, alpha + x * beta, sigma)
sim <- basename(tempfile(pattern = "sim"))
tibble(x = x, y = y, beta_true = beta, sim = sim)
}
#' @title Simulate data from the model.
#' @description Use a discrete covariate x.
#' @return A data frame with the following columns.
#' * `y`: Simulated normal responses.
#' * `x`: A simulated covariate of zeroes and ones.
#' * `beta_true`: The value of the regression coefficient `beta`
#' used to simulate the data.
#' @examples
#' library(tidyverse)
#' simulate_data_discrete()
simulate_data_discrete <- function() {
alpha <- rnorm(1, 0, 1)
beta <- rnorm(1, 0, 1)
sigma <- rhcauchy(1, 1)
x <- rbinom(100, 1, 0.5) # discrete covariate
y <- rnorm(100, alpha + x * beta, sigma)
sim <- basename(tempfile(pattern = "sim"))
tibble(x = x, y = y, beta_true = beta, sim = sim)
}
#' @title Fit the Stan model to some data.
#' @description Fit the Stan model to some data. Where possible,
#' it is best to return small summaries instead of entire
#' chains worth of MCMC samples so data storage stays reasonably light.
#' @return A data frame with one row and columns with information
#' about the model fit.
#' @param model_file Path to the Stan model source file.
#' @param data Data frame, a single simulated dataset.
#' @examples
#' library(cmdstanr)
#' library(tidyverse)
#' compile_model("stan/model.stan")
#' fit_model(simulate_data_discrete(), "stan/model.stan")
fit_model <- function(data, model_file) {
stan_data <- list(x = data$x, y = data$y, n = nrow(data))
truth <- data$beta_true[1]
model <- cmdstan_model(model_file)
fit <- model$sample(data = stan_data, refresh = 0)
fit$summary() %>%
filter(variable == "beta") %>%
mutate(beta_true = truth, cover_beta = q5 < truth & truth < q95)
}