lmer.c

Go to the documentation of this file.
00001 #include "lmer.h"
00002 #include <Rmath.h>              /* for dnorm5, etc. */
00003 #include <R_ext/Lapack.h>     /* for Lapack (dpotrf, etc.) and BLAS */
00004 #include <R_ext/stats_package.h> /* for S_nlminb_iterate */
00005 #include "Matrix.h"              /* for cholmod functions */
00006 
00007 extern
00008 #include "Syms.h"
00009 extern         
00010 cholmod_common c;
00011 
00012 #ifdef ENABLE_NLS               
00013 #include <libintl.h>
00014 #define _(String) dgettext ("lme4", String)
00015 #else
00016 #define _(String) (String)
00017 #endif
00018 
00019 /* When appropriate, alloca is cleaner than malloc/free.  The storage
00020  * is freed automatically on return from a function. When using gcc the
00021  * builtin version is much faster. */
00022 #ifdef __GNUC__
00023 # undef alloca
00024 # define alloca(x) __builtin_alloca((x))
00025 #else
00026 /* this is necessary (and sufficient) for Solaris 10: */
00027 # ifdef __sun
00028 #  include <alloca.h>
00029 # endif
00030 #endif
00031 
00033 #define Alloca(n, t)   (t *) alloca( (size_t) ( (n) * sizeof(t) ) )
00034 
00036 #define AZERO(x, n) {int _I_, _SZ_ = (n); for(_I_ = 0; _I_ < _SZ_; _I_++) (x)[_I_] = 0;}
00037 
00039 enum devP {
00040     ML_POS=0,                   
00041     REML_POS,                   
00042     ldL2_POS,                   
00043     ldRX2_POS,                  
00044     sigmaML_POS,                
00045     sigmaREML_POS,              
00046     pwrss_POS,                  
00047     disc_POS,                   
00048     usqr_POS,                   
00049     wrss_POS                    
00050 };
00052 enum dimP {
00053     nf_POS=0,                   
00054     n_POS,                      
00055     p_POS,                      
00056     q_POS,                      
00057     s_POS,                      
00058     np_POS,                     
00059     isREML_POS,                 
00060     fTyp_POS,                   
00061     lTyp_POS,                   
00062     vTyp_POS,                   
00063     nest_POS,                   
00064     useSc_POS,                  
00065     nAGQ_POS,                   
00066     cvg_POS                     
00067 };
00068 
00080 static R_INLINE double *SLOT_REAL_NULL(SEXP obj, SEXP nm)
00081 {
00082     SEXP pt = GET_SLOT(obj, nm);
00083     return LENGTH(pt) ? REAL(pt) : (double*) NULL;
00084 }
00085 
00088 #define A_SLOT(x) AS_CHM_SP(GET_SLOT(x, lme4_ASym))
00089 
00092 #define Cm_SLOT(x) AS_CHM_SP(GET_SLOT(x, lme4_CmSym))
00093 
00096 #define Cx_SLOT(x) SLOT_REAL_NULL(x, lme4_CxSym)
00097 
00099 #define DEV_SLOT(x) SLOT_REAL_NULL(x, lme4_devianceSym)
00100 
00102 #define DIMS_SLOT(x) INTEGER(GET_SLOT(x, lme4_dimsSym))
00103 
00105 #define ETA_SLOT(x) SLOT_REAL_NULL(x, lme4_etaSym)
00106 
00108 #define FIXEF_SLOT(x) SLOT_REAL_NULL(x, lme4_fixefSym)
00109 
00111 #define Gp_SLOT(x) INTEGER(GET_SLOT(x, lme4_GpSym))
00112 
00115 #define L_SLOT(x) AS_CHM_FR(GET_SLOT(x, lme4_LSym))
00116 
00118 #define MU_SLOT(x) SLOT_REAL_NULL(x, lme4_muSym)
00119 
00122 #define MUETA_SLOT(x) SLOT_REAL_NULL(x, lme4_muEtaSym)
00123 
00126 #define OFFSET_SLOT(x) SLOT_REAL_NULL(x, lme4_offsetSym)
00127 
00129 #define PERM_VEC(x) INTEGER(GET_SLOT(GET_SLOT(x, lme4_LSym), lme4_permSym))
00130 
00133 #define PWT_SLOT(x) SLOT_REAL_NULL(x, lme4_pWtSym)
00134 
00137 #define RANEF_SLOT(x) SLOT_REAL_NULL(x, lme4_ranefSym)
00138 
00140 #define RDF(dims) (dims[n_POS] - (dims[isREML_POS] ? dims[p_POS] : 0))
00141 
00143 #define RESID_SLOT(x) SLOT_REAL_NULL(x, lme4_residSym)
00144 
00146 #define RX_SLOT(x) SLOT_REAL_NULL(x, lme4_RXSym)
00147 
00149 #define RZX_SLOT(x) SLOT_REAL_NULL(x, lme4_RZXSym)
00150 
00153 #define SRWT_SLOT(x) SLOT_REAL_NULL(x, lme4_sqrtrWtSym)
00154 
00157 #define SXWT_SLOT(x) SLOT_REAL_NULL(x, lme4_sqrtXWtSym)
00158 
00160 #define U_SLOT(x) SLOT_REAL_NULL(x, lme4_uSym)
00161 
00164 #define V_SLOT(x) SLOT_REAL_NULL(x, lme4_VSym)
00165 
00168 #define VAR_SLOT(x) SLOT_REAL_NULL(x, lme4_varSym)
00169 
00171 #define X_SLOT(x) SLOT_REAL_NULL(x, lme4_XSym)
00172 
00174 #define Y_SLOT(x) SLOT_REAL_NULL(x, lme4_ySym)
00175 
00178 #define Zt_SLOT(x) AS_CHM_SP(GET_SLOT(x, lme4_ZtSym))
00179 
00180 /* Constants */
00181 
00182 #ifndef BUF_SIZE
00183 
00184 #define BUF_SIZE 127
00185 #endif  
00186 
00188 #define CM_MAXITER  300
00189 
00190 #define CM_TOL      1e-10
00191 
00192 #define CM_SMIN     1e-5
00193 
00194 #define LTHRESH     30.
00195 #define MLTHRESH   -30.
00196 
00197 static double MPTHRESH = 0;
00198 static double PTHRESH = 0;
00199 static const double INVEPS = 1/DOUBLE_EPS;
00200 
00201 
00202 /* In-line functions */
00203 
00216 static R_INLINE double*
00217 apply_perm(double *dest, const double *src, const int *perm, int n)
00218 {
00219     for (int i = 0; i < n; i++) dest[i] = src[perm ? perm[i] : i];
00220     return dest;
00221 }
00222 
00232 static R_INLINE int Gp_grp(int ind, int nf, const int *Gp)
00233 {
00234     for (int i = 0; i < nf; i++) if (ind < Gp[i + 1]) return i;
00235     error(_("invalid row index %d (max is %d)"), ind, Gp[nf]);
00236     return -1;                  /* -Wall */
00237 }
00238 
00247 static R_INLINE double sqr_length(const double *x, int n)
00248 {
00249     double ans = 0;
00250     for (int i = 0; i < n; i++) ans += x[i] * x[i];
00251     return ans;
00252 }
00253 
00254     
00263 static R_INLINE double y_log_y(double y, double mu)
00264 {
00265     return (y) ? (y * log(y/mu)) : 0;
00266 }
00267 
00268 /* Stand-alone utility functions (sorted by function name) */
00269 
00283 static int chkDims(char *buf, int nb, SEXP x, SEXP sym, int nr, int nc)
00284 {
00285     SEXP MP = GET_SLOT(x, sym);
00286     int *dm = isMatrix(MP) ? INTEGER(getAttrib(MP, R_DimSymbol)) : (int*) NULL;
00287     if (!dm || !isReal(MP) || dm[0] != nr || dm[1] != nc)
00288         return snprintf(buf, BUF_SIZE,
00289                         _("Slot %s must be a numeric matrix of size %d by %d"),
00290                         CHAR(PRINTNAME(sym)), nr, nc);
00291     return 0;
00292 }
00293 
00306 static int chkLen(char *buf, int nb, SEXP x, SEXP sym, int len, int zerok)
00307 {
00308     int ll;
00309 
00310     if (!(ll = LENGTH(GET_SLOT(x, sym))) == len && zerok && !ll)
00311         return snprintf(buf, BUF_SIZE, _("Slot %s must have length %d."),
00312                         CHAR(PRINTNAME(sym)), len);
00313     return 0;
00314 }
00315 
00329 static double
00330 lme4_devResid(const double* mu, const double* pWt, const double* y,
00331               int n, int vTyp)
00332 {
00333     double ans = 0.;
00334     for (int i = 0; i < n; i++) {
00335         double mui = mu[i], wi = pWt ? pWt[i] : 1, yi = y[i];
00336         double ri = yi - mui;
00337         switch(vTyp) {
00338         case 1:                 /* constant variance */
00339             ans += wi * ri * ri;
00340             break;
00341         case 2:                 /* mu(1-mu) variance */
00342             ans += 2 * wi *
00343                 (y_log_y(yi, mui) + y_log_y(1 - yi, 1 - mui));
00344             break;
00345         case 3:                 /* mu variance */
00346             ans += 2 * wi * (y_log_y(yi, mui) - (yi - mui));
00347             break;
00348         case 4:                 /* mu^2 variance */
00349             ans += 2 * wi * (y_log_y(yi, mui) - (yi - mui)/mui);
00350             break;
00351         case 5:                 /* mu^3 variance */
00352             ans += wi * (ri * ri)/(yi * mui * mui);
00353             break;
00354         default:
00355             error(_("Unknown vTyp value %d"), vTyp);
00356         }
00357     }
00358     return ans;
00359 }
00360 
00373 static void
00374 lme4_muEta(double* mu, double* muEta, const double* eta, int n, int lTyp)
00375 {
00376     for (int i = 0; i < n; i++) { /* apply the generalized linear part */
00377         double etai = eta[i], tmp;
00378         switch(lTyp) {
00379         case 1:         /* logit */
00380             tmp = (etai < MLTHRESH) ? DOUBLE_EPS :
00381             ((etai > LTHRESH) ? INVEPS : exp(etai));
00382             mu[i] = tmp/(1 + tmp);
00383             muEta[i] = mu[i] * (1 - mu[i]);
00384             break;
00385         case 2:         /* probit */
00386             if (!MPTHRESH) {
00387                 MPTHRESH = qnorm5(DOUBLE_EPS, 0, 1, 1, 0);
00388                 PTHRESH = -MPTHRESH;
00389             }
00390             mu[i] = (etai < MPTHRESH) ? DOUBLE_EPS :
00391                 ((etai > PTHRESH) ? 1 - DOUBLE_EPS :
00392                  pnorm5(etai, 0, 1, 1, 0));
00393             tmp = dnorm4(eta[i], 0, 1, 0);
00394             muEta[i] = (tmp < DOUBLE_EPS) ? DOUBLE_EPS : tmp;
00395             break;
00396         case 3:         /* cauchit */
00397             error(_("cauchit link not yet coded"));
00398             break;
00399         case 4:         /* cloglog */
00400             error(_("cloglog link not yet coded"));
00401             break;
00402         case 5:         /* identity */
00403             mu[i] = eta[i];
00404             muEta[i] = 1.;
00405             break;
00406         case 6:         /* log */
00407             tmp = exp(eta[i]);
00408             muEta[i] = mu[i] =
00409                 (tmp < DOUBLE_EPS) ? DOUBLE_EPS : tmp;
00410             break;
00411         case 7:         /* sqrt */
00412             mu[i] = etai * etai;
00413             muEta[i] = 2 * etai;
00414             break;
00415         default:
00416             error(_("General form of glmer_linkinv not yet written"));
00417         }
00418     }
00419 }
00420 
00431 static void
00432 lme4_varFunc(double* var, const double* mu, int n, int vTyp)
00433 {
00434     for (int i = 0; i < n; i++) {
00435         double mui = mu[i];
00436         switch(vTyp) {
00437         case 1:                 /* constant variance */
00438             var[i] = 1.;
00439             break;
00440         case 2:                 /* mu(1-mu) variance */
00441             if (mui <= 0 || mui >= 1)
00442                 error(_("mu[i] must be in the range (0,1): mu = %g, i = %d"),
00443                       mu, i);
00444             var[i] = mui * (1 - mui);
00445             break;
00446         case 3:                 /* mu variance */
00447             if (mui <= 0)
00448                 error(_("mu[i] must be positive: mu = %g, i = %d"), mu, i);
00449             var[i] = mui;
00450             break;
00451         case 4:                 /* mu^2 variance */
00452             if (mui <= 0)
00453                 error(_("mu[i] must be positive: mu = %g, i = %d"), mu, i);
00454             var[i] = mui * mui;
00455             break;
00456         case 5:                 /* mu^3 variance */
00457             if (mui <= 0)
00458                 error(_("mu[i] must be positive: mu = %g, i = %d"), mu, i);
00459             var[i] = mui * mui * mui;
00460             break;
00461         default:
00462             error(_("Unknown vTyp value %d"), vTyp);
00463         }
00464     }
00465 }
00466         
00477 static void
00478 P_sdmult(double *dest, const int *perm, const CHM_SP A,
00479          const double *X, int nc)
00480 {
00481     int *ai = (int*)(A->i), *ap = (int*)(A->p), m = A->nrow, n = A->ncol;
00482     double *ax = (double*)(A->x), *tmp = Alloca(m, double);
00483     R_CheckStack();
00484 
00485     for (int k = 0; k < nc; k++) {
00486         AZERO(tmp, m);
00487         for (int j = 0; j < n; j++) {
00488             for (int p = ap[j]; p < ap[j + 1]; p++)
00489                 tmp[ai[p]] += X[j + k * n] * ax[p];
00490         }
00491         apply_perm(dest + k * m, tmp, perm, m);
00492     }
00493 }
00494 
00503 static double *ST_getPars(SEXP x, double *pars)
00504 {
00505     SEXP ST = GET_SLOT(x, lme4_STSym);
00506     int nf = LENGTH(ST), pos = 0;
00507     for (int i = 0; i < nf; i++) {
00508         SEXP STi = VECTOR_ELT(ST, i);
00509         double *st = REAL(STi);
00510         int nci = INTEGER(getAttrib(STi, R_DimSymbol))[0];
00511         int ncp1 = nci + 1;
00512 
00513         for (int j = 0; j < nci; j++)
00514             pars[pos++] = st[j * ncp1];
00515         for (int j = 0; j < (nci - 1); j++)
00516             for (int k = j + 1; k < nci; k++)
00517                 pars[pos++] = st[k + j * nci];
00518     }
00519     return pars;
00520 }
00521 
00535 static int                      /* populate the st, nc and nlev arrays */
00536 ST_nc_nlev(const SEXP ST, const int *Gp, double **st, int *nc, int *nlev)
00537 {
00538     int ans = 0, nf = LENGTH(ST);
00539 
00540     for (int i = 0; i < nf; i++) {
00541         SEXP STi = VECTOR_ELT(ST, i);
00542         int nci = *INTEGER(getAttrib(STi, R_DimSymbol));
00543 
00544         if (nci > ans) ans = nci;
00545         if (st) st[i] = REAL(STi);
00546         nc[i] = nci;
00547         nlev[i] = (Gp[i + 1] - Gp[i])/nci;
00548     }
00549     return ans;
00550 }
00551 
00563 static R_INLINE int
00564 theta_S_ind(int i, int nf, int *Gp, int *nlev, int *spt)
00565 {
00566         int trm = Gp_grp(i, nf, Gp);
00567         return (spt[trm] + (i - Gp[trm]) / nlev[trm]);
00568 }
00569 
00581 static void Tt_Zt(CHM_SP A, int *Gp, int *nc, int *nlev, double **st, int nf)
00582 {
00583     int *ai = (int*)(A->i), *ap = (int *)(A->p);
00584     double *ax = (double*)(A->x), one[] = {1,0};
00585 
00586     for (int j = 0; j < A->ncol; j++) /* multiply column j by T' */
00587         for (int p = ap[j]; p < ap[j + 1];) {
00588             int i = Gp_grp(ai[p], nf, Gp);
00589             
00590             if (nc[i] <= 1) p++;
00591             else {
00592                 int nr = p;     /* number of rows in `B' in dtrmm call */
00593                 while ((ai[nr] - Gp[i]) < nlev[i]) nr++;
00594                 nr -= p;        /* nr == 1 except in models with carry-over */
00595                 F77_CALL(dtrmm)("R", "L", "N", "U", &nr, nc + i,
00596                                 one, st[i], nc + i, ax + p, &nr);
00597                 p += (nr * nc[i]);
00598             }
00599         }
00600 }
00601 
00602 /* Level-1 utilties that call at least one of the stand-alone utilities */
00603 
00613 static void lmm_update_projection(SEXP x, double *pb, double *pbeta)
00614 {
00615     int *dims = DIMS_SLOT(x), i1 = 1;
00616     int n = dims[n_POS], p = dims[p_POS], q = dims[q_POS];
00617     double *WX = (double*) NULL, *X = X_SLOT(x),
00618         *d = DEV_SLOT(x), *RZX = RZX_SLOT(x), *RX = RX_SLOT(x),
00619         *sXwt = SXWT_SLOT(x),
00620         *wy = (double*)NULL, *y = Y_SLOT(x),
00621         mone[] = {-1,0}, one[] = {1,0}, zero[] = {0,0};
00622     CHM_SP A = A_SLOT(x);
00623     CHM_FR L = L_SLOT(x);
00624     CHM_DN cpb = N_AS_CHM_DN(pb, q, 1), sol;
00625     R_CheckStack();
00626         
00627     if (sXwt) {              /* Replace X and y by weighted X and y */
00628         WX = Calloc(n * p, double);
00629         wy = Calloc(n, double);
00630         
00631         AZERO(WX, n * p);
00632         for (int i = 0; i < n; i++) {
00633             wy[i] = sXwt[i] * y[i];
00634             for (int j = 0; j < p; j++)
00635                 WX[i + j * n] += sXwt[i] * X[i + j * n];
00636         }
00637         X = WX;
00638         y = wy;
00639     }
00640                                 /* solve L del1 = PAy */
00641     P_sdmult(pb, (int*)L->Perm, A, y, 1);
00642     sol = M_cholmod_solve(CHOLMOD_L, L, cpb, &c);
00643     Memcpy(pb, (double*)sol->x, q);
00644     M_cholmod_free_dense(&sol, &c);
00645                                 /* solve RX' del2 = X'y - RZX'del1 */
00646     F77_CALL(dgemv)("T", &n, &p, one, X, &n,
00647                     y, &i1, zero, pbeta, &i1);
00648     F77_CALL(dgemv)("T", &q, &p, mone, RZX, &q,
00649                     pb, &i1, one, pbeta, &i1);
00650     F77_CALL(dtrsv)("U", "T", "N", &p, RX, &p, pbeta, &i1);
00651     d[pwrss_POS] = sqr_length(y, n)
00652         - (sqr_length(pbeta, p) + sqr_length(pb, q));
00653     if (d[pwrss_POS] < 0)
00654         error(_("Calculated PWRSS for a LMM is negative"));
00655     if (wy) Free(wy);
00656     if (WX) Free(WX);
00657 }
00658 
00664 static void update_A(SEXP x)
00665 {
00666     CHM_SP A = A_SLOT(x), Zt = Zt_SLOT(x);
00667     int *Gp = Gp_SLOT(x), *ai = (int*)(A->i), *ap = (int*)(A->p),
00668         *zi = (int*)(Zt->i), *zp = (int*)(Zt->p), ncmax,
00669         nf = DIMS_SLOT(x)[nf_POS];
00670     int annz = ap[A->ncol], znnz = zp[Zt->ncol];
00671     int *nc = Alloca(nf, int), *nlev = Alloca(nf, int);
00672     double **st = Alloca(nf, double*), *ax = (double*)(A->x),
00673         *zx = (double*)(Zt->x);
00674     R_CheckStack();
00675 
00676     ncmax = ST_nc_nlev(GET_SLOT(x, lme4_STSym), Gp, st, nc, nlev);
00677 
00678     if (annz == znnz) { /* Copy Z' to A unless A has new nonzeros */
00679         Memcpy(ax, zx, znnz);
00680     } else { /* Only for nonlinear models with correlated random effects */
00681         AZERO(ax, annz);        /* Initialize potential nonzeros to 0 */
00682         for (int j = 0; j < A->ncol; j++) { /* Iterate over columns */
00683             int pa = ap[j];
00684             for (int p = zp[j]; p < zp[j + 1]; p++) { /* nonzeros in Z' */
00685                 while (ai[pa] < zi[p]) pa++;          /* matching pos in A */
00686                 if (ai[pa] != zi[p])
00687                     error(_("nonconforming Zt and A structures, j = %d"), j);
00688                 ax[pa] = zx[p];
00689             }
00690         }
00691     }
00692                                 /* When T != I multiply A on the left by T' */
00693     if (ncmax > 1) Tt_Zt(A, Gp, nc, nlev, st, nf);
00694                                 /* Multiply A on the left by S */
00695     for (int p = 0; p < annz; p++) {
00696         int i = Gp_grp(ai[p], nf, Gp);
00697         ax[p] *= st[i][((ai[p] - Gp[i]) / nlev[i]) * (nc[i] + 1)];
00698     }
00699 }
00700 
00711 static double update_L(SEXP x)
00712 {
00713     int *dims = DIMS_SLOT(x);
00714     int n = dims[n_POS], p = dims[p_POS], s = dims[s_POS];
00715     double *V = V_SLOT(x), *cx = Cx_SLOT(x), *d = DEV_SLOT(x),
00716         *res = RESID_SLOT(x), *mu = MU_SLOT(x), *muEta = MUETA_SLOT(x),
00717         *pwt = PWT_SLOT(x), *sXwt = SXWT_SLOT(x), *srwt = SRWT_SLOT(x),
00718         *var =  VAR_SLOT(x), *y = Y_SLOT(x), one[] = {1,0};
00719     CHM_SP A = A_SLOT(x);
00720     CHM_FR L = L_SLOT(x);
00721     R_CheckStack();
00722     
00723     if (var || pwt) {      /* Update srwt and res. Reevaluate wrss. */
00724         d[wrss_POS] = 0;
00725         for (int j = 0; j < n; j++) {
00726             srwt[j] = sqrt((pwt ? pwt[j] : 1.0) / (var ? var[j] : 1.0));
00727             res[j] = srwt[j] * (y[j] - mu[j]);
00728             d[wrss_POS] += res[j] * res[j];
00729         }
00730     }
00731     if (sXwt) {                 /* Update sXwt and C */
00732         int *ai = (int*)A->i, *ap = (int*)A->p;
00733         double *ax = (double*)(A->x);
00734         CHM_SP C = A;
00735 
00736         for (int j = 0; j < s; j++) {
00737             for (int i = 0; i < n; i++) {
00738                 int ja = i + j * n;
00739                 sXwt[ja] = (srwt ? srwt[i] : 1) *
00740                     (muEta ? muEta[i] : 1) * (V ? V[ja] : 1);
00741             }
00742         }
00743         if (cx) {               /* C is a scaled version of A */
00744             for (int j = 0; j < n; j++)
00745                 for (int p = ap[j]; p < ap[j + 1]; p++)
00746                     cx[p] = ax[p] * sXwt[j];
00747             A->x = (void*)cx;
00748         } else {
00749             int *ci, *cp;
00750             C = Cm_SLOT(x);
00751             R_CheckStack();
00752 
00753             ci = (int*)C->i; cp = (int*)C->p; cx = (double*)C->x;
00754             AZERO(cx, cp[n]);
00755             for (int j = 0; j < s; j++)
00756                 for (int i = 0; i < n; i++) {
00757                     int ja = i + j * n, pc;
00758                     for (int pa = ap[ja]; pa < ap[ja + 1]; pa++) {
00759                         for (pc = cp[i]; pc < cp[i + 1]; pc++)
00760                             if (ci[pc] == ai[pa]) break;
00761                         if (pc >= cp[i + 1])
00762                             error(_("Structure of Cm and A are not consistent"));
00763                         cx[pc] += ax[pa] * sXwt[ja];
00764                     }
00765                 }
00766             A = C;
00767         }
00768     }
00769     if (!M_cholmod_factorize_p(A, one, (int*)NULL, 0 /*fsize*/, L, &c))
00770         error(_("cholmod_factorize_p failed: status %d, minor %d from ncol %d"),
00771               c.status, L->minor, L->n);
00772 
00773     d[ldL2_POS] = M_chm_factor_ldetL2(L);
00774     d[pwrss_POS] = d[usqr_POS] + d[wrss_POS];
00775     d[sigmaML_POS] = sqrt(d[pwrss_POS]/
00776                           (srwt ? sqr_length(srwt, n) : (double) n));
00777     d[sigmaREML_POS] = (V || muEta) ? NA_REAL :
00778         d[sigmaML_POS] * sqrt((((double) n)/((double)(n - p))));
00779     return d[pwrss_POS];
00780 }
00781 
00791 static double update_mu(SEXP x)
00792 {
00793     int *dims = DIMS_SLOT(x);
00794     int i1 = 1, n = dims[n_POS], p = dims[p_POS], s = dims[s_POS];
00795     int ns = n * s;
00796     double *V = V_SLOT(x), *d = DEV_SLOT(x), *eta = ETA_SLOT(x),
00797         *etaold = (double*) NULL, *mu = MU_SLOT(x),
00798         *muEta = MUETA_SLOT(x), *offset = OFFSET_SLOT(x),
00799         *srwt = SRWT_SLOT(x), *res = RESID_SLOT(x),
00800         *var = VAR_SLOT(x), *y = Y_SLOT(x), one[] = {1,0};
00801     CHM_FR L = L_SLOT(x);
00802     CHM_SP A = A_SLOT(x);
00803     CHM_DN Ptu, ceta, cu = AS_CHM_DN(GET_SLOT(x, lme4_uSym));
00804     R_CheckStack();
00805 
00806     if (V) {
00807         etaold = eta;
00808         eta = Calloc(ns, double);
00809     }
00810                                 /* eta := offset or eta := 0 */
00811     for (int i = 0; i < ns; i++) eta[i] = offset ? offset[i] : 0;
00812                                 /* eta := eta + X \beta */
00813     F77_CALL(dgemv)("N", &ns, &p, one, X_SLOT(x), &ns,
00814                     FIXEF_SLOT(x), &i1, one, eta, &i1);
00815                                 /* eta := eta + C' P' u */
00816     Ptu = M_cholmod_solve(CHOLMOD_Pt, L, cu, &c);
00817     ceta = N_AS_CHM_DN(eta, ns, 1);
00818     R_CheckStack();
00819     if (!M_cholmod_sdmult(A, 1 /* trans */, one, one, Ptu, ceta, &c))
00820         error(_("cholmod_sdmult error returned"));
00821     M_cholmod_free_dense(&Ptu, &c);
00822 
00823     if (V) {            /* evaluate the nonlinear model */
00824         SEXP pnames = VECTOR_ELT(GET_DIMNAMES(GET_SLOT(x, lme4_VSym)), 1),
00825             gg, rho = GET_SLOT(x, lme4_envSym), vv;
00826         int *gdims;
00827         
00828         if (!isString(pnames) || LENGTH(pnames) != s)
00829             error(_("Slot V must be a matrix with %d named columns"), s);
00830         for (int i = 0; i < s; i++) { /* par. vals. into env. */
00831             vv = findVarInFrame(rho,
00832                                 install(CHAR(STRING_ELT(pnames, i))));
00833             if (!isReal(vv) || LENGTH(vv) != n)
00834                 error(_("Parameter %s in the environment must be a length %d numeric vector"),
00835                       CHAR(STRING_ELT(pnames, i)), n);
00836             Memcpy(REAL(vv), eta + i * n, n);
00837         }
00838         vv = PROTECT(eval(GET_SLOT(x, lme4_nlmodelSym), rho));
00839         if (!isReal(vv) || LENGTH(vv) != n)
00840             error(_("evaluated model is not a numeric vector of length %d"), n);
00841         gg = getAttrib(vv, lme4_gradientSym);
00842         if (!isReal(gg) || !isMatrix(gg))
00843             error(_("gradient attribute of evaluated model must be a numeric matrix"));
00844         gdims = INTEGER(getAttrib(gg, R_DimSymbol));
00845         if (gdims[0] != n ||gdims[1] != s)
00846             error(_("gradient matrix must be of size %d by %d"), n, s);
00847                                 /* colnames of the gradient
00848                                  * corresponding to the order of the
00849                                  * pnames has been checked */
00850         Free(eta);
00851         eta = etaold;
00852         Memcpy(eta, REAL(vv), n);
00853         Memcpy(V, REAL(gg), ns);
00854         UNPROTECT(1);
00855     }
00856 
00857     if (muEta) {
00858         lme4_muEta(mu, muEta, eta, n, dims[lTyp_POS]);
00859         lme4_varFunc(var, mu, n, dims[vTyp_POS]);
00860     } else {
00861         Memcpy(mu, eta, n);
00862     }
00863 
00864     d[wrss_POS] = 0;            /* update resid slot and d[wrss_POS] */
00865     for (int i = 0; i < n; i++) {
00866         res[i] = (y[i] - mu[i]) * (srwt ? srwt[i] : 1);
00867         d[wrss_POS] += res[i] * res[i];
00868     }
00869                                 /* store u'u */
00870     d[usqr_POS] = sqr_length((double*)(cu->x), dims[q_POS]);
00871     d[pwrss_POS] = d[usqr_POS] + d[wrss_POS];
00872     d[sigmaML_POS] = sqrt(d[pwrss_POS]/
00873                           (srwt ? sqr_length(srwt, n) : (double) n));
00874     d[sigmaREML_POS] = (V || muEta) ? NA_REAL :
00875         d[sigmaML_POS] * sqrt((((double) n)/((double)(n - p))));
00876     return d[pwrss_POS];
00877 }
00878 
00887 static void update_ranef(SEXP x)
00888 {
00889     int *Gp = Gp_SLOT(x), *dims = DIMS_SLOT(x), *perm = PERM_VEC(x);
00890     int nf = dims[nf_POS], q = dims[q_POS];
00891     double *b = RANEF_SLOT(x), *u = U_SLOT(x), one[] = {1,0};
00892     int *nc = Alloca(nf, int), *nlev = Alloca(nf, int);
00893     double **st = Alloca(nf, double*);
00894     R_CheckStack(); 
00895 
00896     ST_nc_nlev(GET_SLOT(x, lme4_STSym), Gp, st, nc, nlev);
00897                                 /* inverse permutation */
00898     for (int i = 0; i < q; i++) b[perm[i]] = u[i];
00899     for (int i = 0; i < nf; i++) {
00900         for (int k = 0; k < nc[i]; k++) { /* multiply by \tilde{S}_i */
00901             double dd = st[i][k * (nc[i] + 1)];
00902             int base = Gp[i] + k * nlev[i];
00903             for (int kk = 0; kk < nlev[i]; kk++) b[base + kk] *= dd;
00904         }
00905         if (nc[i] > 1) {        /* multiply by \tilde{T}_i */
00906             F77_CALL(dtrmm)("R", "L", "T", "U", nlev + i, nc + i, one,
00907                             st[i], nc + i, b + Gp[i], nlev + i);
00908         }
00909     }
00910 }
00911 
00920 static double update_RX(SEXP x)
00921 {
00922     int *dims = DIMS_SLOT(x), info;
00923     int n = dims[n_POS], p = dims[p_POS], q = dims[q_POS], s = dims[s_POS];
00924     double *cx = Cx_SLOT(x), *d = DEV_SLOT(x),
00925         *RZX = RZX_SLOT(x), *RX = RX_SLOT(x), *sXwt = SXWT_SLOT(x),
00926         *WX = (double*) NULL, *X = X_SLOT(x),
00927         mone[] = {-1,0}, one[] = {1,0}, zero[] = {0,0};
00928     CHM_SP A = A_SLOT(x);
00929     CHM_FR L = L_SLOT(x);
00930     CHM_DN cRZX = N_AS_CHM_DN(RZX, q, p), ans;
00931     R_CheckStack();
00932 
00933     if (sXwt) {                 /* Create W^{1/2}GHX in WX */
00934         WX = Calloc(n * p, double);
00935 
00936         AZERO(WX, n * p);
00937         for (int j = 0; j < p; j++)
00938             for (int k = 0; k < s; k++)
00939                 for (int i = 0; i < n; i++)
00940                     WX[i + j * n] +=
00941                         sXwt[i + k * n] * X[i + n * (k + j * s)];
00942         X = WX;
00943         /* Replace A by C, either just the x component or the entire matrix */
00944         if (cx) A->x = (void*)cx;
00945         else {
00946             A = Cm_SLOT(x);
00947             R_CheckStack();
00948         }
00949     }
00950                                 /* solve L %*% RZX = PAW^{1/2}GHX */
00951     P_sdmult(RZX, (int*)L->Perm, A, X, p); /* right-hand side */
00952     ans = M_cholmod_solve(CHOLMOD_L, L, cRZX, &c); /* solution */
00953     Memcpy(RZX, (double*)(ans->x), q * p);
00954     M_cholmod_free_dense(&ans, &c);
00955                                 /* downdate X'X and factor  */
00956     F77_CALL(dsyrk)("U", "T", &p, &n, one, X, &n, zero, RX, &p); /* X'X */
00957     F77_CALL(dsyrk)("U", "T", &p, &q, mone, RZX, &q, one, RX, &p);
00958     F77_CALL(dpotrf)("U", &p, RX, &p, &info);
00959     if (info)
00960         error(_("Downdated X'X is not positive definite, %d."), info);
00961                                 /* accumulate log(det(RX)^2)  */
00962     d[ldRX2_POS] = 0;
00963     for (int j = 0; j < p; j++) d[ldRX2_POS] += 2 * log(RX[j * (p + 1)]);
00964 
00965     if (WX) Free(WX);
00966     return d[ML_POS];
00967 }
00968 
00969 /* Level-2 utilities that call at least one of the level-1 utilities */
00970 
00977 static double lmm_update_fixef_u(SEXP x)
00978 {
00979     double ans = NA_REAL;
00980     if (!V_SLOT(x) && !MUETA_SLOT(x)) { /* linear mixed model */
00981         int *dims = DIMS_SLOT(x), i1 = 1;
00982         int n = dims[n_POS], p = dims[p_POS], q = dims[q_POS];
00983         double *d = DEV_SLOT(x), *fixef = FIXEF_SLOT(x),
00984             *srwt = SRWT_SLOT(x), *u = U_SLOT(x),
00985             dn = (double)n, dnmp = (double)(n-p),
00986             mone[] = {-1,0}, one[] = {1,0};
00987         CHM_FR L = L_SLOT(x);
00988         CHM_DN cu = N_AS_CHM_DN(u, q, 1), sol;
00989         R_CheckStack();
00990 
00991         lmm_update_projection(x, u, fixef);
00992                                 /* solve RX beta-hat = del2 */
00993         F77_CALL(dtrsv)("U", "N", "N", &p, RX_SLOT(x), &p, fixef, &i1);
00994         F77_CALL(dgemv)("N", &q, &p, mone, RZX_SLOT(x), &q, fixef,
00995                         &i1, one, u, &i1);
00996         if (!(sol = M_cholmod_solve(CHOLMOD_Lt, L, cu, &c)))
00997             error(_("cholmod_solve (CHOLMOD_Lt) failed:"));
00998         Memcpy(u, (double*)(sol->x), q);
00999         M_cholmod_free_dense(&sol, &c);
01000 
01001         d[usqr_POS] = sqr_length(u, q);
01002         d[wrss_POS] = d[disc_POS] = d[pwrss_POS] - d[usqr_POS];
01003         d[ML_POS] = d[ldL2_POS] +
01004             dn * (1 + log(d[pwrss_POS]) + log(2 * PI / dn));
01005         d[REML_POS] = d[ldL2_POS] + d[ldRX2_POS] +
01006             dnmp * (1. + log(d[pwrss_POS]) + log(2. * PI / dnmp));
01007         d[sigmaML_POS] = sqrt(d[pwrss_POS]/
01008                               (srwt ? sqr_length(srwt, n) : dn));
01009         d[sigmaREML_POS] = d[sigmaML_POS] * sqrt(dn/dnmp);
01010 
01011         ans = d[dims[isREML_POS] ? REML_POS : ML_POS];
01012     }
01013     return ans;
01014 }
01015 
01023 static void
01024 ST_setPars(SEXP x, const double *pars)
01025 {
01026     int *Gp = Gp_SLOT(x), nf = DIMS_SLOT(x)[nf_POS], pos = 0;
01027     int *nc = Alloca(nf, int), *nlev = Alloca(nf, int);
01028     double **st = Alloca(nf, double*);
01029     R_CheckStack();
01030 
01031     ST_nc_nlev(GET_SLOT(x, lme4_STSym), Gp, st, nc, nlev);
01032                                 /* install the parameters in the ST slot */
01033     for (int i = 0; i < nf; i++) {
01034         int nci = nc[i], ncp1 = nc[i] + 1;
01035         double *sti = st[i];
01036 
01037         for (int j = 0; j < nci; j++)
01038             sti[j * ncp1] = pars[pos++];
01039         for (int j = 0; j < (nci - 1); j++)
01040             for (int k = j + 1; k < nci; k++)
01041                 sti[k + j * nci] = pars[pos++];
01042     }
01043     update_A(x);
01044 }
01045 
01055 static int update_u(SEXP x, int verb)
01056 {
01057     int *dims = DIMS_SLOT(x);
01058     int i, n = dims[n_POS], q = dims[q_POS];
01059     double *Cx = Cx_SLOT(x), *sXwt = SXWT_SLOT(x),
01060         *res = RESID_SLOT(x), *u = U_SLOT(x),
01061         cfac = ((double)n) / ((double)q), 
01062         crit, pwrss, pwrss_old, step;
01063     double *tmp = Alloca(q, double), *tmp1 = Alloca(q, double),
01064         *uold = Alloca(q, double), one[] = {1,0}, zero[] = {0,0};
01065     CHM_FR L = L_SLOT(x);
01066     CHM_DN cres = N_AS_CHM_DN(res, n, 1),
01067         ctmp = N_AS_CHM_DN(tmp, q, 1), sol;
01068     CHM_SP C = Cm_SLOT(x);
01069     R_CheckStack();
01070     
01071     if (!sXwt) return(0);       /* nothing to do for LMMs */
01072     if (!(L->is_ll)) error(_("L must be LL', not LDL'"));
01073     if (q > n) error(_("q = %d > n = %d"), q, n);
01074     if (Cx) {               /* A and C have the same structure */
01075         C = A_SLOT(x);
01076         R_CheckStack();
01077         C->x = (void*)Cx;
01078     }
01079     
01080     /* resetting u to zero at the beginning of each evaluation
01081      * requires more iterations but is necessary to obtain a
01082      * repeatable evaluation.  If this is not done the optimization
01083      * algorithm can take wild steps. */
01084     AZERO(u, q);
01085     update_mu(x);
01086     for (i = 0; ; i++) {
01087         
01088         Memcpy(uold, u, q);
01089         pwrss_old = update_L(x);
01090                                 /* tmp := PC %*% wtdResid */
01091         M_cholmod_sdmult(C, 0 /* notrans */, one, zero, cres, ctmp, &c);
01092         Memcpy(tmp1, tmp, q);
01093         apply_perm(tmp, tmp1, (int*)L->Perm, q);
01094                                 /* tmp := tmp - u */
01095         for (int j = 0; j < q; j++) tmp[j] -= u[j];
01096                                 /* solve L %*% sol = tmp */
01097         if (!(sol = M_cholmod_solve(CHOLMOD_L, L, ctmp, &c)))
01098             error(_("cholmod_solve (CHOLMOD_L) failed"));
01099         Memcpy(tmp, (double*)(sol->x), q);
01100         M_cholmod_free_dense(&sol, &c);
01101                                 /* evaluate convergence criterion */
01102         crit = cfac * sqr_length(tmp, q) / pwrss_old;
01103         if (crit < CM_TOL) break; /* don't do needless evaluations */
01104                                 /* solve t(L) %*% sol = tmp */
01105         if (!(sol = M_cholmod_solve(CHOLMOD_Lt, L, ctmp, &c)))
01106             error(_("cholmod_solve (CHOLMOD_Lt) failed"));
01107         Memcpy(tmp, (double*)(sol->x), q);
01108         M_cholmod_free_dense(&sol, &c);
01109 
01110         for (step = 1; step > CM_SMIN; step /= 2) { /* step halving */
01111             for (int j = 0; j < q; j++) u[j] = uold[j] + step * tmp[j];
01112             pwrss = update_mu(x);
01113             if (verb < 0)
01114                 Rprintf("%2d,%8.6f,%12.4g: %15.6g %15.6g %15.6g %15.6g\n",
01115                         i, step, crit, pwrss, pwrss_old, u[1], u[2]);
01116             if (pwrss < pwrss_old) {
01117                 pwrss_old = pwrss;
01118                 break;
01119             }
01120         }
01121         if (step <= CM_SMIN || i > CM_MAXITER) return 0;
01122     }
01123     return i;
01124 }
01125 
01126 /* Level-3 utilities that call at least one level-2 utilities */
01127 
01139 static void MCMC_beta_u(SEXP x, double sigma, double *fvals, double *rvals)
01140 {
01141     int *dims = DIMS_SLOT(x);
01142     int i1 = 1, p = dims[p_POS], q = dims[q_POS];
01143     double *V = V_SLOT(x), *fixef = FIXEF_SLOT(x), *muEta = MUETA_SLOT(x),
01144         *u = U_SLOT(x), mone[] = {-1,0}, one[] = {1,0};
01145     CHM_FR L = L_SLOT(x);
01146     double *del1 = Alloca(q, double), *del2 = Alloca(p, double);
01147     CHM_DN sol, rhs = N_AS_CHM_DN(del1, q, 1);
01148     R_CheckStack();
01149 
01150     if (V || muEta) {
01151         error(_("Update not yet written"));
01152     } else {                    /* Linear mixed model */
01153         update_L(x);
01154         update_RX(x);
01155         lmm_update_fixef_u(x);
01156                                 /* Update beta */
01157         for (int j = 0; j < p; j++) del2[j] = sigma * norm_rand();
01158         F77_CALL(dtrsv)("U", "N", "N", &p, RX_SLOT(x), &p, del2, &i1);
01159         for (int j = 0; j < p; j++) fixef[j] += del2[j];
01160                                 /* Update u */
01161         for (int j = 0; j < q; j++) del1[j] = sigma * norm_rand();
01162         F77_CALL(dgemv)("N", &q, &p, mone, RZX_SLOT(x), &q,
01163                         del2, &i1, one, del1, &i1);
01164         sol = M_cholmod_solve(CHOLMOD_Lt, L, rhs, &c);
01165         for (int j = 0; j < q; j++) u[j] += ((double*)(sol->x))[j];
01166         M_cholmod_free_dense(&sol, &c);
01167         update_mu(x);        /* and parts of the deviance slot */
01168     }
01169     Memcpy(fvals, fixef, p);
01170     if (rvals) {
01171         update_ranef(x);
01172         Memcpy(rvals, RANEF_SLOT(x), q);
01173     }
01174 }
01175 
01183 /* FIXME: Probably should fold this function into MCMC_S */
01184 static void MCMC_T(SEXP x, double sigma)
01185 {
01186     int *Gp = Gp_SLOT(x), nf = (DIMS_SLOT(x))[nf_POS];
01187     double **st = Alloca(nf, double*);
01188     int *nc = Alloca(nf, int), *nlev = Alloca(nf, int);
01189     R_CheckStack();
01190 
01191     if (ST_nc_nlev(GET_SLOT(x, lme4_STSym), Gp, st, nc, nlev) < 2) return;
01192     error("Code for non-trivial theta_T not yet written");
01193 }
01194 
01202 static void MCMC_S(SEXP x, double sigma)
01203 {
01204     CHM_SP A = A_SLOT(x), Zt = Zt_SLOT(x);
01205     int *Gp = Gp_SLOT(x), *ai = (int*)(A->i),
01206         *ap = (int*)(A->p), *dims = DIMS_SLOT(x), *perm = PERM_VEC(x);
01207     int annz = ap[A->ncol], info, i1 = 1, n = dims[n_POS],
01208         nf = dims[nf_POS], ns, p = dims[p_POS], pos,
01209         q = dims[q_POS], znnz = ((int*)(Zt->p))[Zt->ncol];
01210     double *R, *ax = (double*)(A->x), *b = RANEF_SLOT(x),
01211         *eta = ETA_SLOT(x), *offset = OFFSET_SLOT(x),  
01212         *rr, *ss, one = 1, *u = U_SLOT(x), *y = Y_SLOT(x);
01213     int *nc = Alloca(nf, int), *nlev = Alloca(nf, int),
01214         *spt = Alloca(nf + 1, int);
01215     double **st = Alloca(nf, double*);
01216     R_CheckStack();
01217 
01218     ST_nc_nlev(GET_SLOT(x, lme4_STSym), Gp, st, nc, nlev);
01219     ns = 0;                     /* ns is length(theta_S) */
01220     spt[0] = 0;                 /* pointers into ss for terms */
01221     for (int i = 0; i < nf; i++) {
01222         ns += nc[i];
01223         spt[i + 1] = spt[i] + nc[i];
01224     }
01225 
01226     if (annz == znnz) { /* Copy Z' to A unless A has new nonzeros */
01227         Memcpy(ax, (double*)(Zt->x), znnz);
01228     } else error("Code not yet written for MCMC_S with NLMMs");
01229                                 /* Create T'Zt in A */
01230     Tt_Zt(A, Gp, nc, nlev, st, nf); 
01231                                 /* Create P'u in ranef slot */
01232     for (int i = 0; i < q; i++) b[perm[i]] = u[i];
01233                                 /* Create X\beta + offset in eta slot */
01234     for (int i = 0; i < n; i++) eta[i] = offset ? offset[i] : 0;
01235     F77_CALL(dgemv)("N", &n, &p, &one, X_SLOT(x), &n,
01236                     FIXEF_SLOT(x), &i1, &one, eta, &i1);
01237                                 /* Allocate R, rr and ss */
01238     R = Alloca(ns * ns, double); /* crossproduct matrix then factor */
01239     rr = Alloca(ns, double);     /* row of model matrix for theta_S */
01240     ss = Alloca(ns, double);     /* right hand side, then theta_S */
01241     R_CheckStack();
01242     AZERO(R, ns * ns);
01243     AZERO(ss, ns);
01244     /* Accumulate crossproduct from pseudo-data part of model matrix */
01245     for (int i = 0; i < q; i++) {
01246         int sj = theta_S_ind(i, nf, Gp, nlev, spt);
01247         AZERO(rr, ns);
01248         rr[sj] = b[i];
01249         F77_CALL(dsyr)("U", &ns, &one, rr, &i1, R, &ns);
01250     }   
01251     /* Accumulate crossproduct and residual product of the model matrix. */
01252     /* This is done one row at a time.  Rows of the model matrix
01253      * correspond to columns of T'Zt */
01254     for (int j = 0; j < n; j++) { /* jth column of T'Zt */
01255         AZERO(rr, ns);
01256         for (int p = ap[j]; p < ap[j + 1]; p++) {
01257             int i = ai[p];      /* row in T'Zt */
01258             int sj = theta_S_ind(i, nf, Gp, nlev, spt);
01259 
01260             rr[sj] += ax[p] * b[i];
01261             ss[sj] += rr[sj] * (y[j] - eta[j]);
01262         }
01263         F77_CALL(dsyr)("U", &ns, &one, rr, &i1, R, &ns);
01264     }
01265     F77_CALL(dposv)("U", &ns, &i1, R, &ns, ss, &ns, &info);
01266     if (info)
01267         error(_("Model matrix for theta_S is not positive definite, %d."), info);
01268     for (int j = 0; j < ns; j++) rr[j] = sigma * norm_rand();
01269     /* Sample from the conditional Gaussian distribution */
01270     F77_CALL(dtrsv)("U", "N", "N", &ns, R, &ns, rr, &i1);
01271     for (int j = 0; j < ns; j++) ss[j] += rr[j];
01272     /* Copy positive part of solution onto diagonals of ST */
01273     pos = 0;
01274     for (int i = 0; i < nf; i++) {
01275         for (int j = 0; j < nc[i]; j++) {
01276             st[i][j * (nc[i] + 1)] = (ss[pos] > 0) ? ss[pos] : 0;
01277             pos++;
01278         }
01279     }
01280     update_A(x);
01281 }
01282 
01283 /* Externally callable functions */
01284 
01293 SEXP mer_create_L(SEXP CmP)
01294 {
01295     double one[] = {1, 0};
01296     CHM_SP Cm = AS_CHM_SP(CmP);
01297     CHM_FR L;
01298     R_CheckStack();
01299 
01300     L = M_cholmod_analyze(Cm, &c);
01301     if (!M_cholmod_factorize_p(Cm, one, (int*)NULL, 0 /*fsize*/, L, &c))
01302         error(_("cholmod_factorize_p failed: status %d, minor %d from ncol %d"),
01303               c.status, L->minor, L->n);
01304 
01305     return M_chm_factor_to_SEXP(L, 1);
01306 }
01307 
01316 SEXP mer_MCMCsamp(SEXP x, SEXP fm)
01317 {
01318     SEXP devsamp = GET_SLOT(x, lme4_devianceSym);
01319     int *dims = DIMS_SLOT(x), nsamp = LENGTH(devsamp);
01320     int n = dims[n_POS], np = dims[np_POS],
01321         p = dims[p_POS], q = dims[q_POS];
01322     double
01323         *STsamp = REAL(GET_SLOT(x, lme4_STSym)),
01324         *d = DEV_SLOT(fm), *dev = REAL(devsamp),
01325         *sig = SLOT_REAL_NULL(x, lme4_sigmaSym),
01326         *fixsamp = FIXEF_SLOT(x), *resamp = RANEF_SLOT(x);
01327 
01328     GetRNGstate();
01329     /* The first column of storage slots contains the fitted values */
01330     for (int i = 1; i < nsamp; i++) {
01331 /* FIXME: This is probably wrong for a model with weights. */
01332         if (sig)                /* update and store sigma */
01333             sig[i] = sqrt(d[pwrss_POS]/rchisq((double)(n + q)));
01334                         /* update L, RX, beta, u, eta, mu, res and d */
01335         MCMC_beta_u(fm, sig ? sig[i] : 1, fixsamp + i * p,
01336                     resamp + (resamp ? i : 0) * q); 
01337         dev[i] = d[ML_POS];
01338                                 /* update theta_T, theta_S and A */
01339         MCMC_T(fm, sig ? sig[i] : 1);
01340         MCMC_S(fm, sig ? sig[i] : 1);
01341         ST_getPars(fm, STsamp + i * np); /* record theta */
01342     }
01343     PutRNGstate();
01344                 /* Restore pars from the first columns of the samples */
01345     ST_setPars(fm, STsamp);
01346     Memcpy(FIXEF_SLOT(fm), fixsamp, p);
01347     update_u(fm, 0);
01348     update_L(fm);
01349     update_RX(fm);
01350     lmm_update_fixef_u(fm);
01351     update_ranef(fm);
01352 
01353     return x;
01354 }
01355 
01365 SEXP 
01366 mer_optimize(SEXP x, SEXP verbp)
01367 {
01368     SEXP ST = GET_SLOT(x, lme4_STSym);
01369     int *dims = DIMS_SLOT(x), verb = asInteger(verbp);
01370     int lmm = !(MUETA_SLOT(x) || V_SLOT(x)), nf = dims[nf_POS];
01371     int nv = dims[np_POS] + (lmm ? 0 : dims[p_POS]);
01372     int liv = S_iv_length(OPT, nv), lv = S_v_length(OPT, nv);
01373     double *g = (double*)NULL, *h = (double*)NULL, fx = R_PosInf;
01374     double *fixef = FIXEF_SLOT(x);
01375     int *iv = Alloca(liv, int);
01376     double *b = Alloca(2 * nv, double), *d = Alloca(nv, double),
01377         *v = Alloca(lv, double), *xv = Alloca(nv, double);
01378     R_CheckStack();
01379 
01380     ST_getPars(x, xv);
01381     if (!lmm) {
01382         double eta = 1.e-5; /* estimated rel. error on computed lpdisc */
01383 
01384         Memcpy(xv + dims[np_POS], fixef, dims[p_POS]);
01385         v[31] = eta;            /* RFCTOL */
01386         v[36] = eta;            /* SCTOL */
01387         v[41] = eta;            /* ETA0 */
01388     }
01389                                 /* initialize the state vectors v and iv */
01390     S_Rf_divset(OPT, iv, liv, lv, v);
01391     if (verb) iv[OUTLEV] = 1;
01392                                 /* set the bounds to plus/minus Infty  */
01393     for (int i = 0; i < nv; i++) {
01394         b[2*i] = R_NegInf; b[2*i+1] = R_PosInf; d[i] = 1;
01395     }
01396                                 /* reset lower bounds on elements of theta_S */
01397     for (int i = 0, pos = 0; i < nf; i++) {
01398         int nc = *INTEGER(getAttrib(VECTOR_ELT(ST, i), R_DimSymbol));
01399         for (int j = 0; j < nc; j++) b[pos + 2*j] = 0;
01400         pos += nc * (nc + 1);
01401     }
01402     do {
01403         ST_setPars(x, xv);              /* update ST and A */
01404 /* FIXME: Change this so that update_dev is always called and that
01405  * function does the selection of methods based on lmm. The Memcpy
01406  * call should always be used but the number of values to copy can be
01407  * zero. */
01408         if (lmm) {
01409             update_L(x);
01410             update_RX(x);
01411             fx = lmm_update_fixef_u(x);
01412         } else {
01413             Memcpy(fixef, xv + dims[np_POS], dims[p_POS]);
01414             update_u(x, verb);
01415             mer_update_dev(x);
01416             fx = DEV_SLOT(x)[ML_POS];
01417         }
01418         S_nlminb_iterate(b, d, fx, g, h, iv, liv, lv, nv, v, xv);
01419     } while (iv[0] == 1 || iv[0] == 2);
01420     update_RX(x);
01421     lmm_update_fixef_u(x);
01422     dims[cvg_POS] = iv[0];
01423     return R_NilValue;
01424 }
01425 
01434 SEXP mer_postVar(SEXP x)
01435 {
01436     int *Gp = Gp_SLOT(x), *dims = DIMS_SLOT(x);
01437     int nf = dims[nf_POS], q = dims[q_POS];
01438     SEXP ans = PROTECT(allocVector(VECSXP, nf));
01439     double *vv, one[] = {1,0}, sc;
01440     CHM_SP sm1, sm2;
01441     CHM_DN dm1;
01442     CHM_FR L = L_SLOT(x);
01443     int *Perm = (int*)(L->Perm), *iperm = Alloca(q, int),
01444         *nc = Alloca(nf, int), *nlev = Alloca(nf, int);
01445     double **st = Alloca(nf, double*);
01446     R_CheckStack();
01447 
01448     ST_nc_nlev(GET_SLOT(x, lme4_STSym), Gp, st, nc, nlev);
01449     for (int j = 0; j < q; j++) iperm[Perm[j]] = j; /* inverse permutation */
01450     sc = dims[useSc_POS] ?
01451         (DEV_SLOT(x)[dims[isREML_POS] ? sigmaREML_POS : sigmaML_POS]) : 1;
01452     for (int i = 0; i < nf; i++) {
01453         int ncisqr = nc[i] * nc[i];
01454         CHM_SP rhs = M_cholmod_allocate_sparse(q, nc[i], nc[i],
01455                                                1/*sorted*/, 1/*packed*/,
01456                                                0/*stype*/, CHOLMOD_REAL, &c);
01457 
01458         SET_VECTOR_ELT(ans, i, alloc3DArray(REALSXP, nc[i], nc[i], nlev[i]));
01459         vv = REAL(VECTOR_ELT(ans, i));
01460         for (int j = 0; j <= nc[i]; j++) ((int *)(rhs->p))[j] = j;
01461         for (int j = 0; j < nc[i]; j++)
01462             ((double *)(rhs->x))[j] = st[i][j * (nc[i] + 1)] * sc;
01463         for (int k = 0; k < nlev[i]; k++) {
01464             double *vvk = vv + k * ncisqr;
01465             for (int j = 0; j < nc[i]; j++)
01466                 ((int*)(rhs->i))[j] = iperm[Gp[i] + k + j * nlev[i]];
01467             sm1 = M_cholmod_spsolve(CHOLMOD_L, L, rhs, &c);
01468             sm2 = M_cholmod_transpose(sm1, 1 /*values*/, &c);
01469             M_cholmod_free_sparse(&sm1, &c);
01470             sm1 = M_cholmod_aat(sm2, (int*)NULL, (size_t)0, 1 /*mode*/, &c);
01471             dm1 = M_cholmod_sparse_to_dense(sm1, &c);
01472             M_cholmod_free_sparse(&sm1, &c); M_cholmod_free_sparse(&sm2, &c);
01473             Memcpy(vvk, (double*)(dm1->x), ncisqr);
01474             M_cholmod_free_dense(&dm1, &c);
01475             if (nc[i] > 1) {
01476                 F77_CALL(dtrmm)("L", "L", "N", "U", nc + i, nc + i,
01477                                 one, st[i], nc + i, vvk, nc + i);
01478                 F77_CALL(dtrmm)("R", "L", "T", "U", nc + i, nc + i,
01479                                 one, st[i], nc + i, vvk, nc + i);
01480             }
01481         }
01482         M_cholmod_free_sparse(&rhs, &c);
01483     }
01484     UNPROTECT(1);
01485     return ans;
01486 }
01487 
01495 SEXP mer_ST_chol(SEXP x)
01496 {
01497     SEXP ans = PROTECT(duplicate(GET_SLOT(x, lme4_STSym)));
01498     int ncmax, nf = DIMS_SLOT(x)[nf_POS];
01499     int *nc = Alloca(nf, int), *nlev = Alloca(nf, int);
01500     double **st = Alloca(nf, double*);
01501     R_CheckStack();
01502 
01503     ncmax = ST_nc_nlev(ans, Gp_SLOT(x), st, nc, nlev);
01504     for (int k = 0; k < nf; k++) {
01505         if (nc[k] > 1) {        /* nothing to do for nc[k] == 1 */
01506             int nck = nc[k], nckp1 = nc[k] + 1;
01507             double *stk = st[k];
01508 
01509             for (int j = 0; j < nck; j++) {
01510                 double dd = stk[j * nckp1]; /* diagonal el */
01511                 for (int i = j + 1; i < nck; i++) {
01512                     stk[j + i * nck] = dd * stk[i + j * nck];
01513                     stk[i + j * nck] = 0;
01514                 }
01515             }
01516         }
01517     }
01518 
01519     UNPROTECT(1);
01520     return ans;
01521 }
01522 
01530 SEXP mer_ST_getPars(SEXP x)
01531 {
01532     SEXP ans = PROTECT(allocVector(REALSXP, DIMS_SLOT(x)[np_POS]));
01533     ST_getPars(x, REAL(ans));
01534 
01535     UNPROTECT(1); 
01536     return ans;
01537 }
01538 
01548 SEXP mer_ST_initialize(SEXP ST, SEXP Gpp, SEXP Zt)
01549 {
01550     int *Gp = INTEGER(Gpp),
01551         *Zdims = INTEGER(GET_SLOT(Zt, lme4_DimSym)),
01552         *zi = INTEGER(GET_SLOT(Zt, lme4_iSym)), nf = LENGTH(ST);
01553     int *nc = Alloca(nf, int), *nlev = Alloca(nf, int),
01554         nnz = INTEGER(GET_SLOT(Zt, lme4_pSym))[Zdims[1]];
01555     double *rowsqr = Alloca(Zdims[0], double),
01556         **st = Alloca(nf, double*),
01557         *zx = REAL(GET_SLOT(Zt, lme4_xSym));
01558     R_CheckStack();
01559     
01560     ST_nc_nlev(ST, Gp, st, nc, nlev);
01561     AZERO(rowsqr, Zdims[0]);
01562     for (int i = 0; i < nnz; i++) rowsqr[zi[i]] += zx[i] * zx[i];
01563     for (int i = 0; i < nf; i++) {
01564         AZERO(st[i], nc[i] * nc[i]);
01565         for (int j = 0; j < nc[i]; j++) {
01566             double *stij = st[i] + j * (nc[i] + 1);
01567             for (int k = 0; k < nlev[i]; k++)
01568                 *stij += rowsqr[Gp[i] + j * nlev[i] + k];
01569             *stij = sqrt(nlev[i]/(0.375 * *stij));
01570         }
01571     }
01572     return R_NilValue;
01573 }
01574 
01584 SEXP mer_ST_setPars(SEXP x, SEXP pars)
01585 {
01586     int npar = DIMS_SLOT(x)[np_POS];
01587 
01588     if (!isReal(pars) || LENGTH(pars) != npar)
01589         error(_("pars must be a real vector of length %d"), npar);
01590     ST_setPars(x, REAL(pars));
01591     return R_NilValue;
01592 }
01593 
01601 SEXP mer_update_dev(SEXP x)
01602 {
01603     double *d = DEV_SLOT(x);
01604     int *dims = DIMS_SLOT(x);
01605 
01606     if (MUETA_SLOT(x)) {
01607         if (dims[nAGQ_POS] > 1) {
01608             error("Code not yet written");
01609         }
01610         d[disc_POS] = lme4_devResid(MU_SLOT(x), PWT_SLOT(x), Y_SLOT(x),
01611                                     dims[n_POS], dims[vTyp_POS]);
01612         d[ML_POS] = d[disc_POS] + d[ldL2_POS] + d[usqr_POS];
01613     } else {
01614         double dn = (double) dims[n_POS];
01615 
01616         d[disc_POS] = d[wrss_POS];
01617         if (dims[nAGQ_POS] > 1) {
01618             error("Code not yet written");
01619         }
01620         d[ML_POS] = dn * (1 + log(2 * PI/dn)) +
01621             d[wrss_POS] + d[ldL2_POS] + d[usqr_POS];
01622     }
01623     return R_NilValue;
01624 }
01625 
01637 SEXP mer_update_L(SEXP x)
01638 {
01639     return ScalarReal(update_L(x));
01640 }
01641 
01652 SEXP mer_update_mu(SEXP x)
01653 {
01654     return ScalarReal(update_mu(x));
01655 }
01656 
01668 SEXP mer_update_u(SEXP x, SEXP verbP)
01669 {
01670     return ScalarInteger(update_u(x, asInteger(verbP)));
01671 }
01672 
01682 SEXP mer_update_projection(SEXP x)
01683 {
01684     SEXP ans = PROTECT(allocVector(VECSXP, 2));
01685     int *dims = DIMS_SLOT(x);
01686 
01687     SET_VECTOR_ELT(ans, 0, allocVector(REALSXP, dims[q_POS]));
01688     SET_VECTOR_ELT(ans, 1, allocVector(REALSXP, dims[p_POS]));
01689     lmm_update_projection(x, REAL(VECTOR_ELT(ans, 0)),
01690                           REAL(VECTOR_ELT(ans, 1)));
01691 
01692     UNPROTECT(1);
01693     return ans;
01694 }
01695 
01706 SEXP mer_update_ranef(SEXP x)
01707 {
01708     update_ranef(x);
01709     return R_NilValue;
01710 }
01711 
01719 SEXP
01720 mer_update_RX(SEXP x)
01721 {
01722     return ScalarReal(update_RX(x));
01723 }
01724 
01725 SEXP merMCMC_VarCorr(SEXP x, SEXP typP)
01726 {
01727     SEXP ST = GET_SLOT(x, lme4_STSym),
01728         ncs = GET_SLOT(x, install("nc"));
01729     int *Gp = Gp_SLOT(x), *Sd = INTEGER(GET_DIM(ST)), *nc = INTEGER(ncs);
01730     int maxnc = 0, nf = LENGTH(ncs), np = Sd[0], nsamp = Sd[1], pos;
01731     double *sig = SLOT_REAL_NULL(x, lme4_sigmaSym);
01732     SEXP ans = PROTECT(allocMatrix(REALSXP, nsamp, np + (sig ? 1 : 0)));
01733     double *av = REAL(ans), *STx = REAL(ST);
01734     double *as = av + nsamp * np, *t1, *t2, var;
01735     int *nlev = Alloca(nf, int);
01736     R_CheckStack();
01737 
01738     for (int j = 0; j < nf; j++) {
01739         nlev[j] = (Gp[j + 1] - Gp[j])/nc[j];
01740         if (maxnc < nc[j]) maxnc = nc[j];
01741     }
01742     if (maxnc > 1) {
01743         t1 = Alloca(maxnc * maxnc, double);
01744         t2 = Alloca(maxnc * maxnc, double);
01745         R_CheckStack();
01746     }
01747     
01748     for (int i = 0; i < nsamp; i++) {
01749         var = 1; pos = 0;
01750         if (sig) var = as[i] = sig[i] * sig[i];
01751         for (int k = 0; k < nf; k++) {
01752             if (nc[k] < 2) {
01753                 double sd = STx[pos + i * np] * sig[i];
01754                 av[i + nsamp * pos++] = sd * sd;
01755             }
01756             else error(_("Code not yet written"));
01757         }
01758     }
01759         
01760     UNPROTECT(1);
01761     return ans;
01762 }
01771 SEXP mer_validate(SEXP x)
01772 {
01773     SEXP GpP = GET_SLOT(x, lme4_GpSym),
01774         ST = GET_SLOT(x, lme4_STSym),
01775         devianceP = GET_SLOT(x, lme4_devianceSym),
01776         dimsP = GET_SLOT(x, lme4_dimsSym),
01777         flistP = GET_SLOT(x, lme4_flistSym), asgnP;
01778     int *Gp = INTEGER(GpP), *dd = INTEGER(dimsP), *asgn;
01779     int n = dd[n_POS], nf = dd[nf_POS], nq, nfl,
01780         p = dd[p_POS], q = dd[q_POS], s = dd[s_POS];
01781     int nv = n * s;
01782     CHM_SP Zt = Zt_SLOT(x), A =  A_SLOT(x);
01783     CHM_FR L = L_SLOT(x);
01784     char *buf = Alloca(BUF_SIZE + 1, char);
01785     R_CheckStack();
01786                                 /* check lengths */
01787     if (nf < 1 || LENGTH(ST) != nf)
01788         return mkString(_("Slot ST must have length dims['nf']"));
01789     asgnP = getAttrib(flistP, install("assign"));
01790     if (!isInteger(asgnP) || LENGTH(asgnP) != nf)
01791         return mkString(_("Slot flist must have integer attribute 'assign' of length dims['nf']"));
01792     asgn = INTEGER(asgnP);
01793     nfl = LENGTH(flistP);
01794     for (int i = 0; i < nf; i++)
01795         if (asgn[i] <= 0 || asgn[i] > nfl)
01796             return mkString(_("All elements of the assign attribute must be in [1,length(ST)]"));
01797     if (LENGTH(GpP) != nf + 1)
01798         return mkString(_("Slot Gp must have length dims['nf'] + 1"));
01799     if (Gp[0] != 0 || Gp[nf] != q)
01800         return mkString(_("Gp[1] != 0 or Gp[dims['nf'] + 1] != dims['q']"));
01801     if (LENGTH(devianceP) != (wrss_POS + 1) ||
01802         LENGTH(getAttrib(devianceP, R_NamesSymbol)) != (wrss_POS + 1))
01803         return mkString(_("deviance slot not named or incorrect length"));
01804     if (LENGTH(dimsP) != (cvg_POS + 1) ||
01805         LENGTH(getAttrib(dimsP, R_NamesSymbol)) != (cvg_POS + 1))
01806         return mkString(_("dims slot not named or incorrect length"));
01807     if (L->n != q || !L->is_ll || !L->is_monotonic)
01808         return mkString(_("Slot L must be a monotonic LL' factorization of size dims['q']"));
01809     if (Zt->nrow != q || Zt->ncol != nv)
01810         return mkString(_("Slot Zt must by dims['q']  by dims['n']*dims['s']"));
01811     if (A->nrow != q || A->ncol != nv)
01812         return mkString(_("Slot A must be dims['q']  by dims['n']*dims['s']"));
01813     if (chkLen(buf, BUF_SIZE, x, lme4_etaSym, n, 0)) return(mkString(buf));
01814     if (chkLen(buf, BUF_SIZE, x, lme4_fixefSym, p, 0)) return(mkString(buf));
01815     if (chkLen(buf, BUF_SIZE, x, lme4_muEtaSym, n, 1)) return(mkString(buf));
01816     if (chkLen(buf, BUF_SIZE, x, lme4_muSym, n, 0)) return(mkString(buf));
01817     if (chkLen(buf, BUF_SIZE, x, lme4_offsetSym, n, 1)) return(mkString(buf));
01818     if (chkLen(buf, BUF_SIZE, x, lme4_pWtSym, n, 1)) return(mkString(buf));
01819     if (chkLen(buf, BUF_SIZE, x, lme4_ranefSym, q, 0)) return(mkString(buf));
01820     if (chkLen(buf, BUF_SIZE, x, lme4_residSym, n, 0)) return(mkString(buf));
01821     if (chkLen(buf, BUF_SIZE, x, lme4_sqrtrWtSym, n, 1)) return(mkString(buf));
01822     if (chkLen(buf, BUF_SIZE, x, lme4_uSym, q, 0)) return(mkString(buf));
01823     if (chkLen(buf, BUF_SIZE, x, lme4_VSym, nv, 1)) return(mkString(buf));
01824     if (chkLen(buf, BUF_SIZE, x, lme4_varSym, n, 1)) return(mkString(buf));
01825     if (chkLen(buf, BUF_SIZE, x, lme4_ySym, n, 0)) return(mkString(buf));
01826     if (chkDims(buf, BUF_SIZE, x, lme4_XSym, nv, p)) return(mkString(buf));
01827     if (chkDims(buf, BUF_SIZE, x, lme4_RZXSym, q, p)) return(mkString(buf));
01828     if (chkDims(buf, BUF_SIZE, x, lme4_RXSym, p, p)) return(mkString(buf));
01829 
01830     nq = 0;
01831     for (int i = 0; i < LENGTH(flistP); i++) {
01832         SEXP fli = VECTOR_ELT(flistP, i);
01833         if (!isFactor(fli))
01834             return mkString(_("flist must be a list of factors"));
01835 /*      nq += dm[0] * LENGTH(getAttrib(fli, R_LevelsSymbol)); */
01836     }
01837     for (int i = 0; i < nf; i++) {
01838         SEXP STi = VECTOR_ELT(ST, i);
01839         int *dm = INTEGER(getAttrib(STi, R_DimSymbol));
01840         if (!isMatrix(STi) || !isReal(STi) || dm[0] != dm[1])
01841             return
01842                 mkString(_("Slot ST must be a list of square numeric matrices"));
01843         if (Gp[i] > Gp[i + 1])
01844             return mkString(_("Gp must be non-decreasing"));
01845     }
01846 #if 0
01847 /* FIXME: Need to incorporate the assign attribute in the calculation of nq */
01848     if (q != nq)
01849         return mkString(_("q is not sum of columns by levels"));
01850 #endif
01851     return ScalarLogical(1);
01852 }
01853 
01862 SEXP merMCMC_validate(SEXP x)
01863 {
01864     SEXP GpP = GET_SLOT(x, lme4_GpSym),
01865         devianceP = GET_SLOT(x, lme4_devianceSym),
01866         dimsP = GET_SLOT(x, lme4_dimsSym);
01867     int *Gp = INTEGER(GpP), *dd = INTEGER(dimsP);
01868     int nf = dd[nf_POS], np = dd[np_POS], nsamp = LENGTH(devianceP),
01869         p = dd[p_POS], q = dd[q_POS];
01870     char *buf = Alloca(BUF_SIZE + 1, char);
01871     R_CheckStack();
01872                                 /* check lengths */
01873     if (nsamp <= 0)
01874         return mkString(_("number of samples must be positive"));
01875     if (LENGTH(dimsP) != (cvg_POS + 1) ||
01876         LENGTH(getAttrib(dimsP, R_NamesSymbol)) != (cvg_POS + 1))
01877         return mkString(_("dims slot not named or incorrect length"));
01878     if (LENGTH(GpP) != nf + 1)
01879         return mkString(_("Slot Gp must have length dims['nf'] + 1"));
01880     if (Gp[0] != 0 || Gp[nf] != q)
01881         return mkString(_("Gp[1] != 0 or Gp[dims['nf'] + 1] != dims['q']"));
01882 
01883     if (chkLen(buf, BUF_SIZE, x, lme4_ncSym, nf, 0))
01884         return(mkString(buf));
01885     if (chkLen(buf, BUF_SIZE, x, lme4_sigmaSym, nsamp, !dd[useSc_POS]))
01886         return(mkString(buf));
01887     if (chkDims(buf, BUF_SIZE, x, lme4_STSym, np, nsamp))
01888         return(mkString(buf));
01889     if (chkDims(buf, BUF_SIZE, x, lme4_fixefSym, p, nsamp))
01890         return(mkString(buf));
01891     if (LENGTH(GET_SLOT(x, lme4_ranefSym)))
01892         if (chkDims(buf, BUF_SIZE, x, lme4_ranefSym, q, nsamp))
01893             return(mkString(buf));
01894     return ScalarLogical(1);
01895 }
01896 
01905 SEXP spR_update_mu(SEXP x)
01906 {
01907     int *dims = DIMS_SLOT(x);
01908     int n = dims[n_POS];
01909     double *d = DEV_SLOT(x), *eta = Calloc(n, double), *mu = MU_SLOT(x),
01910         *offset = OFFSET_SLOT(x), *srwt = SRWT_SLOT(x),
01911         *res = RESID_SLOT(x), *y = Y_SLOT(x), one[] = {1,0};
01912     CHM_SP Zt = Zt_SLOT(x);
01913     CHM_DN cbeta = AS_CHM_DN(GET_SLOT(x, lme4_fixefSym)),
01914         ceta = N_AS_CHM_DN(eta, n, 1);
01915     R_CheckStack();
01916     
01917     for (int i = 0; i < n; i++) eta[i] = offset ? offset[i] : 0;
01918     if (!M_cholmod_sdmult(Zt, 1 /* trans */, one, one, cbeta, ceta, &c))
01919         error(_("cholmod_sdmult error returned"));
01920     lme4_muEta(mu, MUETA_SLOT(x), eta, n, dims[lTyp_POS]);
01921     lme4_varFunc(VAR_SLOT(x), mu, n, dims[vTyp_POS]);
01922 
01923     d[wrss_POS] = 0;            /* update resid slot and d[wrss_POS] */
01924     for (int i = 0; i < n; i++) {
01925         res[i] = (y[i] - mu[i]) * srwt[i];
01926         d[wrss_POS] += res[i] * res[i];
01927     }
01928 
01929     Free(eta);
01930     return R_NilValue;
01931 }
01932 
01933 
01944 SEXP spR_optimize(SEXP x, SEXP verbP)
01945 {
01946     int *zp, m, n, nnz, verb = asInteger(verbP);
01947     double *Zcp, *Ztx, *d = DEV_SLOT(x), *fixef = FIXEF_SLOT(x),
01948         *mu = MU_SLOT(x), *muEta = MUETA_SLOT(x),
01949         *pWt = PWT_SLOT(x), *res = RESID_SLOT(x),
01950         *srwt = SRWT_SLOT(x), *tmp, *tmp2,
01951         *var = VAR_SLOT(x), *y = Y_SLOT(x), cfac, crit,
01952         one[] = {1, 0}, step, wrss_old, zero[] = {0, 0};
01953     CHM_SP Zt = Zt_SLOT(x);
01954     CHM_FR L = L_SLOT(x);
01955     CHM_DN cres = N_AS_CHM_DN(res, Zt->ncol, 1), ctmp, sol;
01956     R_CheckStack();
01957     
01958     zp = (int*)Zt->p;
01959     m = Zt->nrow;
01960     n = Zt->ncol;
01961     nnz = zp[n];
01962     Zcp = Calloc(nnz, double);
01963     Ztx = (double*)Zt->x;
01964     tmp = Calloc(m, double);
01965     tmp2 = Calloc(m, double);
01966     ctmp = N_AS_CHM_DN(tmp, m, 1);
01967     cfac = ((double)n) / ((double)m);
01968     R_CheckStack();
01969 
01970     spR_update_mu(x);
01971     for (int i = 0; ; i++) {
01972         d[wrss_POS] = 0;
01973         for (int j = 0; j < n; j++) {
01974             srwt[j] = sqrt((pWt ? pWt[j] : 1) / var[j]);
01975             res[j] = srwt[j] * (y[j] - mu[j]);
01976             d[wrss_POS] += res[j] * res[j];
01977             for (int p = zp[j]; p < zp[j + 1]; p++)
01978                 Zcp[p] = srwt[j] * muEta[j] * Ztx[p];
01979         }
01980         Zt->x = (void*)Zcp;
01981         if (!M_cholmod_factorize_p(Zt, zero, (int*)NULL, 0 /*fsize*/, L, &c))
01982             error(_("cholmod_factorize_p failed: status %d, minor %d from ncol %d"),
01983                   c.status, L->minor, L->n);
01984         wrss_old = d[wrss_POS];
01985                                 /* tmp := Zt %*% muEta %*% var %*% wtdResid */
01986         M_cholmod_sdmult(Zt, 0 /* notrans */, one, zero, cres, ctmp, &c);
01987         Memcpy(tmp2, tmp, m);
01988         apply_perm(tmp, tmp2, (int*)L->Perm, m);
01989                                 /* solve L %*% sol = tmp */
01990         if (!(sol = M_cholmod_solve(CHOLMOD_L, L, ctmp, &c)))
01991             error(_("cholmod_solve (CHOLMOD_L) failed"));
01992         Memcpy(tmp, (double*)(sol->x), m);
01993         M_cholmod_free_dense(&sol, &c);
01994                                 /* evaluate convergence criterion */
01995         crit = cfac * sqr_length(tmp, m) / wrss_old;
01996         if (crit < CM_TOL) break; /* don't do needless evaluations */
01997                                 /* solve t(L) %*% sol = tmp */
01998         if (!(sol = M_cholmod_solve(CHOLMOD_Lt, L, ctmp, &c)))
01999             error(_("cholmod_solve (CHOLMOD_Lt) failed"));
02000         Memcpy(tmp, (double*)(sol->x), m);
02001         M_cholmod_free_dense(&sol, &c);
02002 
02003         Memcpy(tmp2, fixef, m);
02004         for (step = 1; step > CM_SMIN; step /= 2) { /* step halving */
02005             for (int j = 0; j < m; j++) fixef[j] = tmp2[j] + step * tmp[j];
02006             spR_update_mu(x);
02007             if (verb < 0)
02008                 Rprintf("%2d,%8.6f,%12.4g: %15.6g %15.6g %15.6g %15.6g\n",
02009                         i, step, crit, d[wrss_POS], wrss_old, fixef[1], fixef[2]);
02010             if (d[wrss_POS] < wrss_old) {
02011                 wrss_old = d[wrss_POS];
02012                 break;
02013             }
02014         }
02015         if (step <= CM_SMIN || i > CM_MAXITER) return 0;
02016     }
02017     Free(tmp2);
02018     Free(Zcp);
02019     Free(tmp);
02020     return R_NilValue;
02021 }
02022 
02023 #if 0
02024 
02025 /* Gauss-Hermite Quadrature x positions and weights */
02026 static const double
02027     GHQ_x1[1] = {0},
02028     GHQ_w1[1] = {1},
02029     GHQ_x2[1] = {1},
02030     GHQ_w2[1] = {0.5},
02031     GHQ_x3[2] = {1.7320507779261, 0},
02032     GHQ_w3[2] = {0.166666666666667, 0.666666666666667},
02033     GHQ_x4[2] = {2.3344141783872, 0.74196377160456},
02034     GHQ_w4[2] = {0.0458758533899086, 0.454124131589555},
02035     GHQ_x5[3] = {2.85696996497785, 1.35562615677371, 0},
02036     GHQ_w5[3] = {0.0112574109895360, 0.222075915334214,
02037                  0.533333317311434},
02038     GHQ_x6[3] = {3.32425737665988, 1.88917584542184,
02039                  0.61670657963811},
02040     GHQ_w6[3] = {0.00255578432527774, 0.0886157433798025,
02041                  0.408828457274383},
02042     GHQ_x7[4] = {3.7504396535397, 2.36675937022918,
02043                  1.15440537498316, 0},
02044     GHQ_w7[4] = {0.000548268839501628, 0.0307571230436095,
02045                  0.240123171391455, 0.457142843409801},
02046     GHQ_x8[4] = {4.14454711519499, 2.80248581332504,
02047                  1.63651901442728, 0.539079802125417},
02048     GHQ_w8[4] = {0.000112614534992306, 0.0096352198313359,
02049                  0.117239904139746, 0.373012246473389},
02050     GHQ_x9[5] = {4.51274578616743, 3.20542894799789,
02051                  2.07684794313409, 1.02325564627686, 0},
02052     GHQ_w9[5] = {2.23458433364535e-05, 0.00278914123744297,
02053                  0.0499164052656755, 0.244097495561989,
02054                  0.406349194142045},
02055     GHQ_x10[5] = {4.85946274516615, 3.58182342225163,
02056                   2.48432579912153, 1.46598906930182,
02057                   0.484935699216176},
02058     GHQ_w10[5] = {4.31065250122166e-06, 0.000758070911538954,
02059                   0.0191115799266379, 0.135483698910192,
02060                   0.344642324578594},
02061     GHQ_x11[6] = {5.18800113558601, 3.93616653976536,
02062                   2.86512311160915, 1.87603498804787,
02063                   0.928868981484148, 0},
02064     GHQ_w11[6] = {8.12184954622583e-07, 0.000195671924393029,
02065                   0.0067202850336527, 0.066138744084179,
02066                   0.242240292596812, 0.36940835831095};
02067 
02068 static const double
02069     *GHQ_x[12] = {(double *) NULL, GHQ_x1, GHQ_x2, GHQ_x3, GHQ_x4,
02070                   GHQ_x5, GHQ_x6, GHQ_x7, GHQ_x8, GHQ_x9, GHQ_x10,
02071                   GHQ_x11},
02072     *GHQ_w[12] = {(double *) NULL, GHQ_w1, GHQ_w2, GHQ_w3, GHQ_w4,
02073                   GHQ_w5, GHQ_w6, GHQ_w7, GHQ_w8, GHQ_w9, GHQ_w10,
02074                   GHQ_w11};
02075 
02076 static void
02077 safe_pd_matrix(double x[], const char uplo[], int n, double thresh)
02078 {
02079     int info, lwork = 3 * n, nm1 = n - 1;
02080     double *work = Alloca(3 * n, double),
02081         *w = Alloca(n, double),
02082         *xcp = Memcpy(Alloca(n * n, double), x, n * n);
02083 
02084     F77_CALL(dsyev)("N", uplo, &n, xcp, &n, w, work, &lwork, &info);
02085     if (info) error(_("dsyev returned %d"), info);
02086     if (w[nm1] <= 0) error(_("no positive eigenvalues!"));
02087     if ((w[0]/w[nm1]) < thresh) {
02088         int i, np1 = n + 1;
02089         double incr = w[nm1] * thresh;
02090         for (int i = 0; i < n; i++) x[i * np1] += incr;
02091     }
02092 }
02093 
02103 static void MCMC_ST(SEXP x, double sigma, double *vals)
02104 {
02105     int *Gp = Gp_SLOT(x), *dims = DIMS_SLOT(x), *perm = PERM_VEC(x);
02106     int nf = dims[nf_POS], q = dims[q_POS], pos = 0;
02107     double *Ptu = Calloc(q, double), *u = U_SLOT(x);
02108     double **st = Alloca(nf, double*);
02109     int *nc = Alloca(nf, int), *nlev = Alloca(nf, int);
02110     R_CheckStack();
02111 
02112     ST_nc_nlev(GET_SLOT(x, lme4_STSym), Gp, st, nc, nlev);
02113                                 /* inverse permutation of u */
02114     for (int j = 0; j < q; j++) Ptu[perm[j]] = u[j];
02115 
02116     for (int i = 0; i < nf; i++) {
02117         int qi = nc[i], nl = nlev[i];
02118         double *sti = st[i], *ui = Ptu + Gp[i];
02119 
02120         if (qi == 1) {
02121             *sti *= sqrt(sqr_length(ui, nl)/rchisq((double)nl))/sigma;
02122             vals[pos++] = *sti;
02123         } else {
02124             int info, qisq = qi * qi, qip1 = qi + 1;
02125             double invsigsq = 1/(sigma * sigma), one[] = {1,0}, zer[] = {0,0};
02126 
02127             double *scal = Alloca(qisq, double), *tmp = Alloca(qisq, double);
02128             R_CheckStack();
02129                                 /* scal := crossprod(U_i)/sigma^2 */
02130             F77_CALL(dsyrk)("L", "T", &qi, &nl, &invsigsq, ui, &nl,
02131                             zer, scal, &qi); 
02132             F77_CALL(dpotrf)("L", &qi, scal, &qi, &info);
02133             if (info)
02134                 error(_("scal matrix %d is not positive definite"), i + 1);
02135             /* solve W_i' B_i = R_i for W_i a random std Wishart factor */
02136             F77_CALL(dtrsm)("L", "L", "T", "N", &qi, &qi, one,
02137                             std_rWishart_factor((double)(nl - qi + 1), qi, 0, tmp),
02138                             &qi, scal, &qi);
02139                                 /* B_i := T(theta) %*% S(theta) %*% B_i */
02140             for (int k = 0; k < qi; k++)
02141                 for (int j = 0; j < qi; j++)
02142                     scal[j * qi + k] *= st[i][k * qip1];
02143             F77_CALL(dtrmm)("L", "L", "N", "U", &qi, &qi, one, st[i], &qi, scal, &qi);
02144                                 /* Create the lower Cholesky factor */
02145             F77_CALL(dsyrk)("L", "T", &qi, &qi, one, scal, &qi, zer, tmp, &qi); 
02146             F77_CALL(dpotrf)("L", &qi, tmp, &qi, &info);
02147             if (info)
02148                 error(_("crossproduct matrix %d is not positive definite"), i + 1);
02149                                 /* Update S_i and T_i */
02150             for (int j = 0; j < qi; j++) vals[pos++] = sti[j * qip1] = tmp[j * qip1];
02151             for (int j = 0; j < qi; j++)
02152                 for (int k = j + 1; k < qi; k++)
02153                     vals[pos++] = sti[j * qi + k] = tmp[j * qi + k]/sti[j * qip1];
02154         }
02155     }
02156     update_A(x);
02157     Free(Ptu);
02158 }
02159 
02160 /* Functions for sampling from a Wishart distribution */
02161 /* FIXME: Move these to the R sources */
02162 
02175 static double
02176 *std_rWishart_factor(double nu, int p, int upper, double ans[])
02177 {
02178     int pp1 = p + 1;
02179 
02180     if (nu < (double) p || p <= 0)
02181         error("inconsistent degrees of freedom and dimension");
02182 
02183     AZERO(ans, p * p);
02184     for (int j = 0; j < p; j++) {       /* jth column */
02185         ans[j * pp1] = sqrt(rchisq(nu - (double) j));
02186         for (int i = 0; i < j; i++) {
02187             int uind = i + j * p, /* upper triangle index */
02188                 lind = j + i * p; /* lower triangle index */
02189             ans[(upper ? uind : lind)] = norm_rand();
02190             ans[(upper ? lind : uind)] = 0;
02191         }
02192     }
02193     return ans;
02194 }
02195 
02205 SEXP
02206 lme4_rWishart(SEXP ns, SEXP nuP, SEXP scal)
02207 {
02208     SEXP ans;
02209     int *dims = INTEGER(getAttrib(scal, R_DimSymbol)), info,
02210         n = asInteger(ns), psqr;
02211     double *scCp, *ansp, *tmp, nu = asReal(nuP), one = 1, zero = 0;
02212 
02213     if (!isMatrix(scal) || !isReal(scal) || dims[0] != dims[1])
02214         error("scal must be a square, real matrix");
02215     if (n <= 0) n = 1;
02216     psqr = dims[0] * dims[0];
02217     tmp = Alloca(psqr, double);
02218     scCp = Alloca(psqr, double);
02219     R_CheckStack();
02220 
02221     Memcpy(scCp, REAL(scal), psqr);
02222     AZERO(tmp, psqr);
02223     F77_CALL(dpotrf)("U", &(dims[0]), scCp, &(dims[0]), &info);
02224     if (info)
02225         error("scal matrix is not positive-definite");
02226     PROTECT(ans = alloc3DArray(REALSXP, dims[0], dims[0], n));
02227     ansp = REAL(ans);
02228     GetRNGstate();
02229     for (int j = 0; j < n; j++) {
02230         double *ansj = ansp + j * psqr;
02231         std_rWishart_factor(nu, dims[0], 1, tmp);
02232         F77_CALL(dtrmm)("R", "U", "N", "N", dims, dims,
02233                         &one, scCp, dims, tmp, dims);
02234         F77_CALL(dsyrk)("U", "T", &(dims[1]), &(dims[1]),
02235                         &one, tmp, &(dims[1]),
02236                         &zero, ansj, &(dims[1]));
02237 
02238         for (int i = 1; i < dims[0]; i++)
02239             for (int k = 0; k < i; k++)
02240                 ansj[i + k * dims[0]] = ansj[k + i * dims[0]];
02241     }
02242 
02243     PutRNGstate();
02244     UNPROTECT(1);
02245     return ans;
02246 }
02247 
02248 
02261 static R_INLINE double*
02262 apply_iperm(double *dest, const double *src, const int *perm, int n)
02263 {
02264     for (int i = 0; i < n; i++) dest[perm ? perm[i] : i] = src[i];
02265     return dest;
02266 }
02267 
02268 #endif

Generated on Wed Jul 9 15:07:48 2008 for lme4 by  doxygen 1.4.7