0

如何brms为具有随机变化点的分段线性模型指定模型公式?

以下是数据:

library("tidyverse")
library("brms")

dat <-
  structure(list(log_q = c(8.28, 6.51, 6.64, 3.82, 5.86, 7.21, 
  3.58, 3.3, 4.19, 9.67, 2.95, 7.08, 8.82, 4.31, 4.33, 7.97, 7.57, 
  3, 3.69, 7.93, 6.46, 3, 4.02, 5.68, 8.29, 4.31, 7.31, 6.43, 7.74, 
  4.08, 4.01, 5.94, 7.56, 9.16, 10.29, 9.47, 5.6, 4.86, 3.58, 7.73, 
  7.01, 10.43, 6.15, 10.73, 5.34, 6.45, 7.15, 5.09, 6.14, 5.97), 
      log_c = c(1.24, 0.26, -0.03, -0.33, 0.3, -0.11, 0.26, -0.39, 
      0.63, 1.52, 0, 0.56, 1.41, -0.22, 0.09, 0, 0.34, 0.3, -0.06, 
      1.66, 0.17, 0.06, -0.15, -0.11, 1.99, 0.16, 1.54, 0.1, -0.04, 
      0.1, -0.07, -0.04, -0.11, 1.92, 1.45, 1.41, 0.1, -0.53, -0.22, 
      0.74, 0.35, 1.56, -0.08, 1.2, -0.51, 0.03, 0.23, 0.23, -0.31, 
      -0.3)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", 
  "data.frame"))

# Plot
dat %>% 
  ggplot(aes(x = log_q, y = log_c)) +
  geom_point()

在此处输入图像描述

我可以这样指定一个简单的线性模型:

# Function to standardize data
standardize <- function(x) {
  (x - mean(x)) / sd(x)
}

# Simple linear model
fit_lm <-
  brm(
    data = dat %>%
      # Standardize log-transformed variables
      mutate(x_z = standardize(log_q),
             y_z = standardize(log_c)),
    family = student,
    y_z ~ 1 + x_z,
    prior = c(
      prior(normal(0, 10), class = Intercept),
      prior(normal(0, 10), class = b),
      prior(normal(0, 1), class = sigma),
      prior(exponential(one_over_twentynine), class = nu)
    ),
    stanvars = stanvar(1 / 29, name = "one_over_twentynine")
  )

但我不知道如何为具有随机变化点的分段线性模型指定公式。brms一般来说,贝叶斯分析非常新。

4

0 回答 0