static double prior_uk(double x, SEXP da){ int *dm = DIMS_SLOT(da), *Gp = Gp_SLOT(da), k = K_SLOT(da)[0]; int gn = Gp_grp(k, dm[nT_POS], Gp); /* group number of u_k */ double *u = U_SLOT(da), tmp = U_SLOT(da)[k], ans; u[k] = x ; ans = prior_u_Gp(gn, da); /* compute log prior for group gn */ u[k] = tmp ; return ans; }
/* FIXME: Probably should fold this function into MCMC_S */ static void MCMC_T(SEXP x, double sigma) { int *Gp = Gp_SLOT(x), nt = (DIMS_SLOT(x))[nt_POS]; double **st = Alloca(nt, double*); int *nc = Alloca(nt, int), *nlev = Alloca(nt, int); R_CheckStack(); if (ST_nc_nlev(GET_SLOT(x, lme4_STSym), Gp, st, nc, nlev) < 2) return; error("Code for non-trivial theta_T not yet written"); }
static double prior_u_Gp(int gn, SEXP da){ SEXP V = GET_SLOT(da, install("Sigma")); int *Gp = Gp_SLOT(da), *nc = NCOL_SLOT(da), *nlev = NLEV_SLOT(da) ; double *v = REAL(VECTOR_ELT(V, gn)), *u = U_SLOT(da), ans = 0.0; if (nc[gn] == 1) { /* univariate normal */ for (int j = 0; j < nlev[gn]; j++) ans += -0.5 * u[Gp[gn] + j] * u[Gp[gn] + j] / v[0]; return ans ; } else { /* multivariate normal */ double *xv = Alloca(nc[gn], double), *iv = Alloca(nc[gn] * nc[gn], double); R_CheckStack() ; solve_po(nc[gn], v, iv) ; for (int j = 0; j < nlev[gn]; j++){ for (int i = 0; i < nc[gn]; i++) xv[i] = u[Gp[gn] + i * nlev[gn] + j] ; ans += dmvnorm(nc[gn], xv, (double*) NULL, iv) ; } return ans ; } }
static void sim_Sigma(SEXP da){ SEXP V = GET_SLOT(da, install("Sigma")) ; int *dm = DIMS_SLOT(da), *Gp = Gp_SLOT(da), *nc = NCOL_SLOT(da), *nlev = NLEV_SLOT(da); int nT = dm[nT_POS], mc = imax(nc, nT); double *v, su, *u = U_SLOT(da), *scl = Alloca(mc * mc, double); R_CheckStack(); for (int i = 0; i < nT; i++){ v = REAL(VECTOR_ELT(V, i)); if (nc[i] == 1){ /* simulate from the inverse-Gamma */ su = sqr_length(u + Gp[i], nlev[i]); v[0] = 1/rgamma(0.5 * nlev[i] + IG_SHAPE, 1.0/(su * 0.5 + IG_SCALE)); } else { /* simulate from the inverse-Wishart */ mult_xtx(nlev[i], nc[i], u + Gp[i], scl); /* t(x) * (x) */ for (int j = 0; j < nc[i]; j++) scl[j * j] += 1.0; /* add prior (identity) scale matrix */ solve_po(nc[i], scl, v); rwishart(nc[i], (double) (nlev[i] + nc[i]), v, scl); solve_po(nc[i], scl, v); } } }
/** * Update the theta_S parameters from the ST arrays in place. * * @param x an mer object * @param sigma current standard deviation of the per-observation * noise terms. */ static void MCMC_S(SEXP x, double sigma) { CHM_SP A = A_SLOT(x), Zt = Zt_SLOT(x); int *Gp = Gp_SLOT(x), *ai = (int*)(A->i), *ap = (int*)(A->p), *dims = DIMS_SLOT(x), *perm = PERM_VEC(x); int annz = ap[A->ncol], info, i1 = 1, n = dims[n_POS], nt = dims[nt_POS], ns, p = dims[p_POS], pos, q = dims[q_POS], znnz = ((int*)(Zt->p))[Zt->ncol]; double *R, *ax = (double*)(A->x), *b = RANEF_SLOT(x), *eta = ETA_SLOT(x), *offset = OFFSET_SLOT(x), *rr, *ss, one = 1, *u = U_SLOT(x), *y = Y_SLOT(x); int *nc = Alloca(nt, int), *nlev = Alloca(nt, int), *spt = Alloca(nt + 1, int); double **st = Alloca(nt, double*); R_CheckStack(); ST_nc_nlev(GET_SLOT(x, lme4_STSym), Gp, st, nc, nlev); ns = 0; /* ns is length(theta_S) */ spt[0] = 0; /* pointers into ss for terms */ for (int i = 0; i < nt; i++) { ns += nc[i]; spt[i + 1] = spt[i] + nc[i]; } if (annz == znnz) { /* Copy Z' to A unless A has new nonzeros */ Memcpy(ax, (double*)(Zt->x), znnz); } else error("Code not yet written for MCMC_S with NLMMs"); /* Create T'Zt in A */ Tt_Zt(A, Gp, nc, nlev, st, nt); /* Create P'u in ranef slot */ for (int i = 0; i < q; i++) b[perm[i]] = u[i]; /* Create X\beta + offset in eta slot */ for (int i = 0; i < n; i++) eta[i] = offset ? offset[i] : 0; F77_CALL(dgemv)("N", &n, &p, &one, X_SLOT(x), &n, FIXEF_SLOT(x), &i1, &one, eta, &i1); /* Allocate R, rr and ss */ R = Alloca(ns * ns, double); /* crossproduct matrix then factor */ rr = Alloca(ns, double); /* row of model matrix for theta_S */ ss = Alloca(ns, double); /* right hand side, then theta_S */ R_CheckStack(); AZERO(R, ns * ns); AZERO(ss, ns); /* Accumulate crossproduct from pseudo-data part of model matrix */ for (int i = 0; i < q; i++) { int sj = theta_S_ind(i, nt, Gp, nlev, spt); AZERO(rr, ns); rr[sj] = b[i]; F77_CALL(dsyr)("U", &ns, &one, rr, &i1, R, &ns); } /* Accumulate crossproduct and residual product of the model matrix. */ /* This is done one row at a time. Rows of the model matrix * correspond to columns of T'Zt */ for (int j = 0; j < n; j++) { /* jth column of T'Zt */ AZERO(rr, ns); for (int p = ap[j]; p < ap[j + 1]; p++) { int i = ai[p]; /* row in T'Zt */ int sj = theta_S_ind(i, nt, Gp, nlev, spt); rr[sj] += ax[p] * b[i]; ss[sj] += rr[sj] * (y[j] - eta[j]); } F77_CALL(dsyr)("U", &ns, &one, rr, &i1, R, &ns); } F77_CALL(dposv)("U", &ns, &i1, R, &ns, ss, &ns, &info); if (info) error(_("Model matrix for theta_S is not positive definite, %d."), info); for (int j = 0; j < ns; j++) rr[j] = sigma * norm_rand(); /* Sample from the conditional Gaussian distribution */ F77_CALL(dtrsv)("U", "N", "N", &ns, R, &ns, rr, &i1); for (int j = 0; j < ns; j++) ss[j] += rr[j]; /* Copy positive part of solution onto diagonals of ST */ pos = 0; for (int i = 0; i < nt; i++) { for (int j = 0; j < nc[i]; j++) { st[i][j * (nc[i] + 1)] = (ss[pos] > 0) ? ss[pos] : 0; pos++; } } update_A(x); }