2017-10-15 60 views
0

我正在使用brms包在預測變量x上構建一個帶有高斯過程的多級模型。這個模型看起來像這樣:make_stancode(y〜gp(x,cov =「exp_quad」,by = groups)+(1 | groups),data = dat)這樣一個gp對x預測變量和一個多級組變量。在我的情況下,我有5個組。我一直在尋找代碼(下面),我試圖找出一些參數的含義和尺寸。brm迴歸參數的含義

我看到M_1是組數

我的問題是:

  1. 什麼是N_1的意思,是不是一樣的觀測值的數量,N?這裏使用它:vector [N_1] z_1 [M_1]; //未縮放的組級效果
  2. 對於Kgp_1和Mgp_1(int Kgp_1;和int Mgp_1;),如果我有5個組,Kgp_1和Mgp_1都等於5?如果是這樣,爲什麼使用兩個變量?與BRMS生成

    // 1.10.0 函數{

    /* compute a latent Gaussian process 
        * Args: 
        * x: array of continuous predictor values 
        * sdgp: marginal SD parameter 
        * lscale: length-scale parameter 
        * zgp: vector of independent standard normal variables 
        * Returns: 
        * a vector to be added to the linear predictor 
        */ 
        vector gp(vector[] x, real sdgp, real lscale, vector zgp) { 
        matrix[size(x), size(x)] cov; 
        cov = cov_exp_quad(x, sdgp, lscale); 
        for (n in 1:size(x)) { 
         // deal with numerical non-positive-definiteness 
         cov[n, n] = cov[n, n] + 1e-12; 
        } 
        return cholesky_decompose(cov) * zgp; 
        } 
    } 
    data { 
        int<lower=1> N; // total number of observations 
        vector[N] Y; // response variable 
        int<lower=1> Kgp_1; 
        int<lower=1> Mgp_1; 
        vector[Mgp_1] Xgp_1[N]; 
        int<lower=1> Igp_1[Kgp_1]; 
        int<lower=1> Jgp_1_1[Igp_1[1]]; 
        int<lower=1> Jgp_1_2[Igp_1[2]]; 
        int<lower=1> Jgp_1_3[Igp_1[3]]; 
        int<lower=1> Jgp_1_4[Igp_1[4]]; 
        int<lower=1> Jgp_1_5[Igp_1[5]]; 
        // data for group-level effects of ID 1 
        int<lower=1> J_1[N]; 
        int<lower=1> N_1; 
        int<lower=1> M_1; 
        vector[N] Z_1_1; 
        int prior_only; // should the likelihood be ignored? 
    } 
    transformed data { 
    } 
    parameters { 
        real temp_Intercept; // temporary intercept 
        // GP hyperparameters 
        vector<lower=0>[Kgp_1] sdgp_1; 
        vector<lower=0>[Kgp_1] lscale_1; 
        vector[N] zgp_1; 
        real<lower=0> sigma; // residual SD 
        vector<lower=0>[M_1] sd_1; // group-level standard deviations 
        vector[N_1] z_1[M_1]; // unscaled group-level effects 
    } 
    transformed parameters { 
        // group-level effects 
        vector[N_1] r_1_1 = sd_1[1] * (z_1[1]); 
    } 
    model { 
        vector[N] mu = rep_vector(0, N) + temp_Intercept; 
        mu[Jgp_1_1] = mu[Jgp_1_1] + gp(Xgp_1[Jgp_1_1], sdgp_1[1], lscale_1[1], zgp_1[Jgp_1_1]); 
        mu[Jgp_1_2] = mu[Jgp_1_2] + gp(Xgp_1[Jgp_1_2], sdgp_1[2], lscale_1[2], zgp_1[Jgp_1_2]); 
        mu[Jgp_1_3] = mu[Jgp_1_3] + gp(Xgp_1[Jgp_1_3], sdgp_1[3], lscale_1[3], zgp_1[Jgp_1_3]); 
        mu[Jgp_1_4] = mu[Jgp_1_4] + gp(Xgp_1[Jgp_1_4], sdgp_1[4], lscale_1[4], zgp_1[Jgp_1_4]); 
        mu[Jgp_1_5] = mu[Jgp_1_5] + gp(Xgp_1[Jgp_1_5], sdgp_1[5], lscale_1[5], zgp_1[Jgp_1_5]); 
        for (n in 1:N) { 
        mu[n] = mu[n] + (r_1_1[J_1[n]]) * Z_1_1[n]; 
        } 
        // priors including all constants 
        target += student_t_lpdf(sdgp_1 | 3, 0, 10) 
        - 1 * student_t_lccdf(0 | 3, 0, 10); 
        target += normal_lpdf(lscale_1 | 0, 0.5) 
        - 1 * normal_lccdf(0 | 0, 0.5); 
        target += normal_lpdf(zgp_1 | 0, 1); 
        target += student_t_lpdf(sigma | 3, 0, 10) 
        - 1 * student_t_lccdf(0 | 3, 0, 10); 
        target += student_t_lpdf(sd_1 | 3, 0, 10) 
        - 1 * student_t_lccdf(0 | 3, 0, 10); 
        target += normal_lpdf(z_1[1] | 0, 1); 
        // likelihood including all constants 
        if (!prior_only) { 
        target += normal_lpdf(Y | mu, sigma); 
        } 
    } 
    generated quantities { 
        // actual population-level intercept 
        real b_Intercept = temp_Intercept; 
    } 
    

回答

1

如果在相同的公式使用make_standata(...),你可以看到,將被傳遞到斯坦的數據。從這裏,你可以拼湊出一些變量的作用。如果我使用lme4::sleepstudy數據集作爲數據的代理,我得到:

library(brms) 
dat <- lme4::sleepstudy 
dat$groups <- dat$Subject 
dat$y <- dat$Reaction 
dat$x <- dat$Days 

s_data <- make_standata(
    y ~ gp(x, cov = "exp_quad", by= groups) + (1| groups), data = dat) 
s_data$N_1 
#> 18 

對於N_1,我拿到18是水平的groups在該數據集的數量。

對於Kgp_1和Mgp_1(int Kgp_1;和int Mgp_1;),如果我有5組,Kgp_1和Mgp_1都等於5?如果是這樣,爲什麼使用兩個變量?

s_data$Mgp_1 
#> 1 
s_data$Kgp_1 
#> 18 

看起來Kgp_1又是組數。我不確定除了設置矢量的長度外,Mgp_1還會做什麼vector[Mgp_1] Xgp_1[N];

+1

Mgp_1是高斯過程的維數,所以如果你有gp(x,z),那麼Mgp_1將是2 – user3022875