7  Modeling Longitudinal Responses using Generalized Least Squares

Some good general references on longitudinal data analysis are Davis (2002), Pinheiro & Bates (2000), Diggle et al. (2002), Venables & Ripley (2003), Hand & Crowder (1996), Verbeke & Molenberghs (2000), Lindsey (1997)

7.1 Notation

  • \(N\) subjects
  • Subject \(i\) (\(i=1,2,\ldots,N\)) has \(n_{i}\) responses measured at times \(t_{i1}, t_{i2}, \ldots, t_{in_{i}}\)
  • Response at time \(t\) for subject \(i\): \(Y_{it}\)
  • Subject \(i\) has baseline covariates \(X_{i}\)
  • Generally the response measured at time \(t_{i1}=0\) is a covariate in \(X_{i}\) instead of being the first measured response \(Y_{i0}\)
  • Time trend in response is modeled with \(k\) parameters so that the time “main effect” has \(k\) d.f.
  • Let the basis functions modeling the time effect be \(g_{1}(t), g_{2}(t), \ldots, g_{k}(t)\)
A

7.2 Model Specification for Effects on \(E(Y)\)

7.2.1 Common Basis Functions

  • \(k\) dummy variables for \(k+1\) unique times (assumes no functional form for time but may spend many d.f.)
  • \(k=1\) for linear time trend, \(g_{1}(t)=t\)
  • \(k\)–order polynomial in \(t\)
  • \(k+1\)–knot restricted cubic spline (one linear term, \(k-1\) nonlinear terms)
B

7.2.2 Model for Mean Profile

  • A model for mean time-response profile without interactions between time and any \(X\):
    \(E[Y_{it} | X_{i}] = X_{i}\beta + \gamma_{1}g_{1}(t) + \gamma_{2}g_{2}(t) + \ldots + \gamma_{k}g_{k}(t)\)
  • Model with interactions between time and some \(X\)’s: add product terms for desired interaction effects
  • Example: To allow the mean time trend for subjects in group 1 (reference group) to be arbitrarily different from time trend for subjects in group 2, have a dummy variable for group 2, a time “main effect” curve with \(k\) d.f. and all \(k\) products of these time components with the dummy variable for group 2
C

7.2.3 Model Specification for Treatment Comparisons

  • In studies comparing two or more treatments, a response is often measured at baseline (pre-randomization)
  • Analyst has the option to use this measurement as \(Y_{i0}\) or as part of \(X_{i}\)
D

For RCTs, I draw a sharp line at the point when the intervention begins. The LHS [left hand side of the model equation] is reserved for something that is a response to treatment. Anything before this point can potentially be included as a covariate in the regression model. This includes the “baseline” value of the outcome variable. Indeed, the best predictor of the outcome at the end of the study is typically where the patient began at the beginning. It drinks up a lot of variability in the outcome; and, the effect of other covariates is typically mediated through this variable.

I treat anything after the intervention begins as an outcome. In the western scientific method, an “effect” must follow the “cause” even if by a split second.

Note that an RCT is different than a cohort study. In a cohort study, “Time 0” is not terribly meaningful. If we want to model, say, the trend over time, it would be legitimate, in my view, to include the “baseline” value on the LHS of that regression model.

Now, even if the intervention, e.g., surgery, has an immediate effect, I would include still reserve the LHS for anything that might legitimately be considered as the response to the intervention. So, if we cleared a blocked artery and then measured the MABP, then that would still be included on the LHS.

Now, it could well be that most of the therapeutic effect occurred by the time that the first repeated measure was taken, and then levels off. Then, a plot of the means would essentially be two parallel lines and the treatment effect is the distance between the lines, i.e., the difference in the intercepts.

If the linear trend from baseline to Time 1 continues beyond Time 1, then the lines will have a common intercept but the slopes will diverge. Then, the treatment effect will the difference in slopes.

One point to remember is that the estimated intercept is the value at time 0 that we predict from the set of repeated measures post randomization. In the first case above, the model will predict different intercepts even though randomization would suggest that they would start from the same place. This is because we were asleep at the switch and didn’t record the “action” from baseline to time 1. In the second case, the model will predict the same intercept values because the linear trend from baseline to time 1 was continued thereafter.

More importantly, there are considerable benefits to including it as a covariate on the RHS. The baseline value tends to be the best predictor of the outcome post-randomization, and this maneuver increases the precision of the estimated treatment effect. Additionally, any other prognostic factors correlated with the outcome variable will also be correlated with the baseline value of that outcome, and this has two important consequences. First, this greatly reduces the need to enter a large number of prognostic factors as covariates in the linear models. Their effect is already mediated through the baseline value of the outcome variable. Secondly, any imbalances across the treatment arms in important prognostic factors will induce an imbalance across the treatment arms in the baseline value of the outcome. Including the baseline value thereby reduces the need to enter these variables as covariates in the linear models.

Senn (2006) states that temporally and logically, a “baseline cannot be a response to treatment”, so baseline and response cannot be modeled in an integrated framework.

… one should focus clearly on ‘outcomes’ as being the only values that can be influenced by treatment and examine critically any schemes that assume that these are linked in some rigid and deterministic view to ‘baseline’ values. An alternative tradition sees a baseline as being merely one of a number of measurements capable of improving predictions of outcomes and models it in this way.

The final reason that baseline cannot be modeled as the response at time zero is that many studies have inclusion/exclusion criteria that include cutoffs on the baseline variable. In other words, the baseline measurement comes from a truncated distribution. In general it is not appropriate to model the baseline with the same distributional shape as the follow-up measurements. Thus the approach recommended by Liang & Zeger (2000) and Liu et al. (2009) are problematic1.

E
  • 1 In addition to this, one of the paper’s conclusions that analysis of covariance is not appropriate if the population means of the baseline variable are not identical in the treatment groups is not correct (Senn, 2006). See Kenward et al. (2010) for a rebuke of Liu et al. (2009).

  • 7.3 Modeling Within-Subject Dependence

    • Random effects and mixed effects models have become very popular
    • Disadvantages:
      • Induced correlation structure for \(Y\) may be unrealistic
      • Numerically demanding
      • Require complex approximations for distributions of test statistics
    • Conditional random effects vs. (subject-) marginal models:
      • Random effects are subject-conditional
      • Random effects models are needed to estimate responses for individual subjects
      • Models without random effects are marginalized with respect to subject-specific effects
      • They are natural when the interest is on group-level parameters (e.g., overall treatment effect)
      • Random effects are natural when there is clustering at more than the subject level (multi-level models)
    • Extended linear model (marginal; with no random effects) is a logical extension of the univariate model (e.g., few statisticians use subject random effects for univariate \(Y\))
    • This was known as growth curve models and generalized least squares (Goldstein, 1989; Potthoff & Roy, 1964) and was developed long before mixed effect models became popular
    • Pinheiro and Bates (Section~5.1.2) state that “in some applications, one may wish to avoid incorporating random effects in the model to account for dependence among observations, choosing to use the within-group component \(\Lambda_{i}\) to directly model variance-covariance structure of the response.”
    • We will assume that \(Y_{it} | X_{i}\) has a multivariate normal distribution with mean given above and with variance-covariance matrix \(V_{i}\), an \(n_{i}\times n_{i}\) matrix that is a function of \(t_{i1}, \ldots, t_{in_{i}}\)
    • We further assume that the diagonals of \(V_{i}\) are all equal
    • Procedure can be generalized to allow for heteroscedasticity over time or with respect to \(X\) (e.g., males may be allowed to have a different variance than females)
    • This extended linear model has the following assumptions:
      • all the assumptions of OLS at a single time point including correct modeling of predictor effects and univariate normality of responses conditional on \(X\)
      • the distribution of two responses at two different times for the same subject, conditional on \(X\), is bivariate normal with a specified correlation coefficient
      • the joint distribution of all \(n_{i}\) responses for the \(i^{th}\) subject is multivariate normal with the given correlation pattern (which implies the previous two distributional assumptions)
      • responses from any times for any two different subjects are uncorrelated
    FGH
    What Methods To Use for Repeated Measurements / Serial Data? 2 3
    Repeated Measures ANOVA GEE Mixed Effects Models GLS Markov LOCF Summary Statistic4
    Assumes normality × × ×
    Assumes independence of measurements within subject ×5 ×6
    Assumes a correlation structure7 × ×8 × × ×
    Requires same measurement times for all subjects × ?
    Does not allow smooth modeling of time to save d.f. ×
    Does not allow adjustment for baseline covariates ×
    Does not easily extend to non-continuous \(Y\) × ×
    Loses information by not using intermediate measurements ×9 ×
    Does not allow widely varying # observations per subject × ×10 × ×11
    Does not allow for subjects to have distinct trajectories12 × × × × ×
    Assumes subject-specific effects are Gaussian ×
    Badly biased if non-random dropouts ? × ×
    Biased in general ×
    Harder to get tests & CLs ×13 ×14
    Requires large # subjects/clusters ×
    SEs are wrong ×15 ×
    Assumptions are not verifiable in small samples × N/A × × ×
    Does not extend to complex settings such as time-dependent covariates and dynamic 16 models × × × × ?
  • 2 Thanks to Charles Berry, Brian Cade, Peter Flom, Bert Gunter, and Leena Choi for valuable input.

  • 3 GEE: generalized estimating equations; GLS: generalized least squares; LOCF: last observation carried forward.

  • 4 E.g., compute within-subject slope, mean, or area under the curve over time. Assumes that the summary measure is an adequate summary of the time profile and assesses the relevant treatment effect.

  • 5 Unless one uses the Huynh-Feldt or Greenhouse-Geisser correction

  • 6 For full efficiency, if using the working independence model

  • 7 Or requires the user to specify one

  • 8 For full efficiency of regression coefficient estimates

  • 9 Unless the last observation is missing

  • 10 The cluster sandwich variance estimator used to estimate SEs in GEE does not perform well in this situation, and neither does the working independence model because it does not weight subjects properly.

  • 11 Unless one knows how to properly do a weighted analysis

  • 12 Or users population averages

  • 13 Unlike GLS, does not use standard maximum likelihood methods yielding simple likelihood ratio \(\chi^2\) statistics. Requires high-dimensional integration to marginalize random effects, using complex approximations, and if using SAS, unintuitive d.f. for the various tests.

  • 14 Because there is no correct formula for SE of effects; ordinary SEs are not penalized for imputation and are too small

  • 15 If correction not applied

  • 16 E.g., a model with a predictor that is a lagged value of the response variable

    • Markov models use ordinary univariate software and are very flexible
    • They apply the same way to binary, ordinal, nominal, and continuous Y
    • They require post-fitting calculations to get probabilities, means, and quantiles that are not conditional on the previous Y value
    I

    Gardiner et al. (2009) compared several longitudinal data models, especially with regard to assumptions and how regression coefficients are estimated. Peters et al. (2012) have an empirical study confirming that the “use all available data” approach of likelihood–based longitudinal models makes imputation of follow-up measurements unnecessary.

    J

    7.4 Parameter Estimation Procedure

    • Generalized least squares
    • Like weighted least squares but uses a covariance matrix that is not diagonal
    • Each subject can have her own shape of \(V_{i}\) due to each subject being measured at a different set of times
    • Maximum likelihood
    • Newton-Raphson or other trial-and-error methods used for estimating parameters
    • For small number of subjects, advantages in using REML (restricted maximum likelihood) instead of ordinary MLE (Diggle et al., 2002, p. Section~5.3), (Pinheiro & Bates, 2000, p. Chapter~5), Goldstein (1989) (esp. to get more unbiased estimate of the covariance matrix)
    • When imbalances are not severe, OLS fitted ignoring subject identifiers may be efficient
      • But OLS standard errors will be too small as they don’t take intra-cluster correlation into account
      • May be rectified by substituting covariance matrix estimated from Huber-White cluster sandwich estimator or from cluster bootstrap
    • When imbalances are severe and intra-subject correlations are strong, OLS is not expected to be efficient because it gives equal weight to each observation
      • a subject contributing two distant observations receives \(\frac{1}{5}\) the weight of a subject having 10 tightly-spaced observations
    KLM

    7.5 Common Correlation Structures

    • Usually restrict ourselves to isotropic correlation structures — correlation between responses within subject at two times depends only on a measure of distance between the two times, not the individual times
    • We simplify further and assume depends on \(|t_{1} - t_{2}|\)
    • Can speak interchangeably of correlations of residuals within subjects or correlations between responses measured at different times on the same subject, conditional on covariates \(X\)
    • Assume that the correlation coefficient for \(Y_{it_{1}}\) vs. \(Y_{it_{2}}\) conditional on baseline covariates \(X_{i}\) for subject \(i\) is \(h(|t_{1} - t_{2}|, \rho)\), where \(\rho\) is a vector (usually a scalar) set of fundamental correlation parameters
    • Some commonly used structures when times are continuous and are not equally spaced (Pinheiro & Bates, 2000, sec. 5.3.3) (nlme correlation function names are at the right if the structure is implemented in nlme):
    NO
    Table 7.1: Some longitudinal data correlation structures
    Structure nlme Function
    Compound symmetry: \(h = \rho\) if \(t_{1} \neq t_{2}\), 1 if \(t_{1}=t_{2}\) 17 corCompSymm
    Autoregressive-moving average lag 1: \(h = \rho^{|t_{1} - t_{2}|} = \rho^s\) where \(s = |t_{1}-t_{2}|\) corCAR1
    Exponential: \(h = \exp(-s/\rho)\) corExp
    Gaussian: \(h = \exp[-(s/\rho)^2]\) corGaus
    Linear: \(h = (1 - s/\rho)[s < \rho]\) corLin
    Rational quadratic: \(h = 1 - (s/\rho)^{2}/[1+(s/\rho)^{2}]\) corRatio
    Spherical: \(h = [1-1.5(s/\rho)+0.5(s/\rho)^{3}][s < \rho]\) corSpher
    Linear exponent AR(1): \(h = \rho^{d_{min} + \delta\frac{s - d_{min}}{d_{max} - d_{min}}}\), 1 if \(t_{1}=t_{2}\) Simpson et al. (2010)
  • 17 Essentially what two-way ANOVA assumes

  • The structures 3-7 use \(\rho\) as a scaling parameter, not as something restricted to be in \([0,1]\)

    7.6 Checking Model Fit

    • Constant variance assumption: usual residual plots
    • Normality assumption: usual qq residual plots
    • Correlation pattern: Variogram
      • Estimate correlations of all possible pairs of residuals at different time points
      • Pool all estimates at same absolute difference in time \(s\)
      • Variogram is a plot with \(y = 1 - \hat{h}(s, \rho)\) vs. \(s\) on the \(x\)-axis
      • Superimpose the theoretical variogram assumed by the model
    P

    7.7 R Software

    • Nonlinear mixed effects model package of Pinheiro & Bates
    • For linear models, fitting functions are
      • lme for mixed effects models
      • gls for generalized least squares without random effects
    • For this version the rms package has Gls so that many features of rms can be used:
      • anova: all partial Wald tests, test of linearity, pooled tests
      • summary: effect estimates (differences in \(\hat{Y}\)) and confidence limits, can be plotted
      • plot, ggplot, plotp: continuous effect plots
      • nomogram: nomogram
      • Function: generate R function code for fitted model
      • latex:  representation of fitted model
    Q

    In addition, Gls has a bootstrap option (hence you do not use rms’s bootcov for Gls fits).
    To get regular gls functions named anova (for likelihood ratio tests, AIC, etc.) or summary use anova.gls or summary.gls * nlme package has many graphics and fit-checking functions * Several functions will be demonstrated in the case study

    7.8 Case Study

    Consider the dataset in Table~6.9 of Davis[davis-repmeas, pp. 161-163] from a multi-center, randomized controlled trial of botulinum toxin type B (BotB) in patients with cervical dystonia from nine U.S. sites.

    • Randomized to placebo (\(N=36\)), 5000 units of BotB (\(N=36\)), 10,000 units of BotB (\(N=37\))
    • Response variable: total score on Toronto Western Spasmodic Torticollis Rating Scale (TWSTRS), measuring severity, pain, and disability of cervical dystonia (high scores mean more impairment)
    • TWSTRS measured at baseline (week 0) and weeks 2, 4, 8, 12, 16 after treatment began
    • Dataset cdystonia from web site
    R

    7.8.1 Graphical Exploration of Data

    Code
    require(rms)
    require(data.table)
    options(prType='html')    # for model print, summary, anova
    getHdata(cdystonia)
    setDT(cdystonia)          # convert to data.table
    cdystonia[, uid := paste(site, id)]   # unique subject ID
    
    # Tabulate patterns of subjects' time points
    g <- function(w) paste(sort(unique(w)), collapse=' ')
    cdystonia[, table(tapply(week, uid, g))]
    
                0         0 2 4   0 2 4 12 16       0 2 4 8    0 2 4 8 12 
                1             1             3             1             1 
    0 2 4 8 12 16    0 2 4 8 16   0 2 8 12 16   0 4 8 12 16      0 4 8 16 
               94             1             2             4             1 
    Code
    # Plot raw data, superposing subjects
    xl <- xlab('Week'); yl <- ylab('TWSTRS-total score')
    ggplot(cdystonia, aes(x=week, y=twstrs, color=factor(id))) +
           geom_line() + xl + yl + facet_grid(treat ~ site) +
           guides(color=FALSE)

    Figure 7.1: Time profiles for individual subjects, stratified by study site and dose

    Code
    # Show quartiles
    g <- function(x) {
      k <- as.list(quantile(x, (1 : 3) / 4, na.rm=TRUE))
      names(k) <- .q(Q1, Q2, Q3)
      k
    }
    cdys <- cdystonia[, g(twstrs), by=.(treat, week)]
    ggplot(cdys, aes(x=week, y=Q2)) + xl + yl + ylim(0, 70) +
      geom_line() + facet_wrap(~ treat, nrow=2) +
      geom_ribbon(aes(ymin=Q1, ymax=Q3), alpha=0.2)

    Figure 7.2: Quartiles of TWSTRS stratified by dose

    Code
    # Show means with bootstrap nonparametric CLs
    cdys <-  cdystonia[, as.list(smean.cl.boot(twstrs)),
                       by = list(treat, week)]
    ggplot(cdys, aes(x=week, y=Mean)) + xl + yl + ylim(0, 70) +
      geom_line() + facet_wrap(~ treat, nrow=2) +
      geom_ribbon(aes(x=week, ymin=Lower, ymax=Upper), alpha=0.2)

    Figure 7.3: Mean responses and nonparametric bootstrap 0.95 confidence limits for population means, stratified by dose

    Model with \(Y_{i0}\) as Baseline Covariate

    Code
    baseline <- cdystonia[week == 0]
    baseline[, week := NULL]
    setnames(baseline, 'twstrs', 'twstrs0')
    followup <- cdystonia[week > 0, .(uid, week, twstrs)]
    setkey(baseline, uid)
    setkey(followup, uid, week)
    both     <- Merge(baseline, followup, id = ~ uid)
             Vars Obs Unique IDs IDs in #1 IDs not in #1
    baseline    7 109        109        NA            NA
    followup    3 522        108       108             0
    Merged      9 523        109       109             0
    
    Number of unique IDs in any data frame : 109 
    Number of unique IDs in all data frames: 108 
    Code
    # Remove person with no follow-up record
    both     <- both[! is.na(week)]
    dd       <- datadist(both)
    options(datadist='dd')

    7.8.2 Using Generalized Least Squares

    We stay with baseline adjustment and use a variety of correlation structures, with constant variance. Time is modeled as a restricted cubic spline with 3 knots, because there are only 3 unique interior values of week.

    S
    Code
    require(nlme)
    cp <- list(corCAR1,corExp,corCompSymm,corLin,corGaus,corSpher)
    z  <- vector('list',length(cp))
    for(k in 1:length(cp)) {
      z[[k]] <- gls(twstrs ~ treat * rcs(week, 3) +
                    rcs(twstrs0, 3) + rcs(age, 4) * sex, data=both,
                    correlation=cp[[k]](form = ~week | uid))
    }
    anova(z[[1]],z[[2]],z[[3]],z[[4]],z[[5]],z[[6]])
           Model df      AIC      BIC    logLik
    z[[1]]     1 20 3553.906 3638.357 -1756.953
    z[[2]]     2 20 3553.906 3638.357 -1756.953
    z[[3]]     3 20 3587.974 3672.426 -1773.987
    z[[4]]     4 20 3575.079 3659.531 -1767.540
    z[[5]]     5 20 3621.081 3705.532 -1790.540
    z[[6]]     6 20 3570.958 3655.409 -1765.479

    AIC computed above is set up so that smaller values are best. From this the continuous-time AR1 and exponential structures are tied for the best. For the remainder of the analysis use corCAR1, using Gls.

    Keselman et al. (1998) did a simulation study to study the reliability of AIC for selecting the correct covariance structure in repeated measurement models. In choosing from among 11 structures, AIC selected the correct structure 47% of the time. Gurka et al. (2011) demonstrated that fixed effects in a mixed effects model can be biased, independent of sample size, when the specified covariate matrix is more restricted than the true one.
    Code
    a <- Gls(twstrs ~ treat * rcs(week, 3) + rcs(twstrs0, 3) +
             rcs(age, 4) * sex, data=both,
             correlation=corCAR1(form=~week | uid))
    a

    Generalized Least Squares Fit by REML

     Gls(model = twstrs ~ treat * rcs(week, 3) + rcs(twstrs0, 3) + 
         rcs(age, 4) * sex, data = both, correlation = corCAR1(form = ~week | 
         uid))
     
    Obs 522 Log-restricted-likelihood -1756.95
    Clusters 108 Model d.f. 17
    g 11.334 σ 8.5917
    d.f. 504
    β S.E. t Pr(>|t|)
    Intercept  -0.3093  11.8804 -0.03 0.9792
    treat=5000U   0.4344   2.5962 0.17 0.8672
    treat=Placebo   7.1433   2.6133 2.73 0.0065
    week   0.2879   0.2973 0.97 0.3334
    week’   0.7313   0.3078 2.38 0.0179
    twstrs0   0.8071   0.1449 5.57 <0.0001
    twstrs0’   0.2129   0.1795 1.19 0.2360
    age  -0.1178   0.2346 -0.50 0.6158
    age’   0.6968   0.6484 1.07 0.2830
    age’’  -3.4018   2.5599 -1.33 0.1845
    sex=M  24.2802  18.6208 1.30 0.1929
    treat=5000U × week   0.0745   0.4221 0.18 0.8599
    treat=Placebo × week  -0.1256   0.4243 -0.30 0.7674
    treat=5000U × week’  -0.4389   0.4363 -1.01 0.3149
    treat=Placebo × week’  -0.6459   0.4381 -1.47 0.1411
    age × sex=M  -0.5846   0.4447 -1.31 0.1892
    age’ × sex=M   1.4652   1.2388 1.18 0.2375
    age’’ × sex=M  -4.0338   4.8123 -0.84 0.4023
     Correlation Structure: Continuous AR(1)
      Formula: ~week | uid 
      Parameter estimate(s):
           Phi 
     0.8666689 
     

    \(\hat{\rho} = 0.8672\), the estimate of the correlation between two measurements taken one week apart on the same subject. The estimated correlation for measurements 10 weeks apart is \(0.8672^{10} = 0.24\).

    T
    Code
    v <- Variogram(a, form=~ week | uid)
    plot(v)

    Figure 7.4: Variogram, with assumed correlation pattern superimposed

    Check constant variance and normality assumptions:

    U
    Code
    both$resid <- r <- resid(a); both$fitted <- fitted(a)
    yl <- ylab('Residuals')
    p1 <- ggplot(both, aes(x=fitted, y=resid)) + geom_point() +
          facet_grid(~ treat) + yl
    p2 <- ggplot(both, aes(x=twstrs0, y=resid)) + geom_point()+yl
    p3 <- ggplot(both, aes(x=week, y=resid)) + yl + ylim(-20,20) +
          stat_summary(fun.data="mean_sdl", geom='smooth')
    p4 <- ggplot(both, aes(sample=resid)) + stat_qq() +
          geom_abline(intercept=mean(r), slope=sd(r)) + yl
    gridExtra::grid.arrange(p1, p2, p3, p4, ncol=2)

    Figure 7.5: Three residual plots to check for absence of trends in central tendency and in variability. Upper right panel shows the baseline score on the \(x\)-axis. Bottom left panel shows the mean \(\pm 2\times\) SD. Bottom right panel is the QQ plot for checking normality of residuals from the GLS fit.

    Now get hypothesis tests, estimates, and graphically interpret the model.

    Code
    anova(a)
    Wald Statistics for twstrs
    χ2 d.f. P
    treat (Factor+Higher Order Factors) 22.11 6 0.0012
    All Interactions 14.94 4 0.0048
    week (Factor+Higher Order Factors) 77.27 6 <0.0001
    All Interactions 14.94 4 0.0048
    Nonlinear (Factor+Higher Order Factors) 6.61 3 0.0852
    twstrs0 233.83 2 <0.0001
    Nonlinear 1.41 1 0.2354
    age (Factor+Higher Order Factors) 9.68 6 0.1388
    All Interactions 4.86 3 0.1826
    Nonlinear (Factor+Higher Order Factors) 7.59 4 0.1077
    sex (Factor+Higher Order Factors) 5.67 4 0.2252
    All Interactions 4.86 3 0.1826
    treat × week (Factor+Higher Order Factors) 14.94 4 0.0048
    Nonlinear 2.27 2 0.3208
    Nonlinear Interaction : f(A,B) vs. AB 2.27 2 0.3208
    age × sex (Factor+Higher Order Factors) 4.86 3 0.1826
    Nonlinear 3.76 2 0.1526
    Nonlinear Interaction : f(A,B) vs. AB 3.76 2 0.1526
    TOTAL NONLINEAR 15.03 8 0.0586
    TOTAL INTERACTION 19.75 7 0.0061
    TOTAL NONLINEAR + INTERACTION 28.54 11 0.0027
    TOTAL 322.98 17 <0.0001
    Code
    plot(anova(a))

    Figure 7.6: Results of anova.rms from generalized least squares fit with continuous time AR1 correlation structure

    Code
    ylm <- ylim(25, 60)
    p1 <- ggplot(Predict(a, week, treat, conf.int=FALSE),
                 adj.subtitle=FALSE, legend.position='top') + ylm
    p2 <- ggplot(Predict(a, twstrs0), adj.subtitle=FALSE) + ylm
    p3 <- ggplot(Predict(a, age, sex), adj.subtitle=FALSE,
                 legend.position='top') + ylm
    gridExtra::grid.arrange(p1, p2, p3, ncol=2)

    Figure 7.7: Estimated effects of time, baseline TWSTRS, age, and sex

    Code
    summary(a)  # Shows for week 8
    Effects   Response: twstrs
    Low High Δ Effect S.E. Lower 0.95 Upper 0.95
    week 4 12 8 6.6910 1.1060 4.524 8.858
    twstrs0 39 53 14 13.5500 0.8862 11.810 15.290
    age 46 65 19 2.5030 2.0510 -1.518 6.523
    treat — 5000U:10000U 1 2 0.5917 1.9980 -3.325 4.508
    treat — Placebo:10000U 1 3 5.4930 2.0040 1.565 9.421
    sex — M:F 1 2 -1.0850 1.7790 -4.571 2.401
    Code
    # To get results for week 8 for a different reference group
    # for treatment, use e.g. summary(a, week=4, treat='Placebo')
    
    # Compare low dose with placebo, separately at each time
    k1 <- contrast(a, list(week=c(2,4,8,12,16), treat='5000U'),
                      list(week=c(2,4,8,12,16), treat='Placebo'))
    options(width=80)
    print(k1, digits=3)
        week twstrs0 age sex Contrast S.E.  Lower  Upper     Z Pr(>|z|)
    1      2      46  56   F    -6.31 2.10 -10.43 -2.186 -3.00   0.0027
    2      4      46  56   F    -5.91 1.82  -9.47 -2.349 -3.25   0.0011
    3      8      46  56   F    -4.90 2.01  -8.85 -0.953 -2.43   0.0150
    4*    12      46  56   F    -3.07 1.75  -6.49  0.361 -1.75   0.0795
    5*    16      46  56   F    -1.02 2.10  -5.14  3.092 -0.49   0.6260
    
    Redundant contrasts are denoted by *
    
    Confidence intervals are 0.95 individual intervals
    Code
    # Compare high dose with placebo
    k2 <- contrast(a, list(week=c(2,4,8,12,16), treat='10000U'),
                      list(week=c(2,4,8,12,16), treat='Placebo'))
    print(k2, digits=3)
        week twstrs0 age sex Contrast S.E.  Lower Upper     Z Pr(>|z|)
    1      2      46  56   F    -6.89 2.07 -10.96 -2.83 -3.32   0.0009
    2      4      46  56   F    -6.64 1.79 -10.15 -3.13 -3.70   0.0002
    3      8      46  56   F    -5.49 2.00  -9.42 -1.56 -2.74   0.0061
    4*    12      46  56   F    -1.76 1.74  -5.17  1.65 -1.01   0.3109
    5*    16      46  56   F     2.62 2.09  -1.47  6.71  1.25   0.2099
    
    Redundant contrasts are denoted by *
    
    Confidence intervals are 0.95 individual intervals
    Code
    k1 <- as.data.frame(k1[c('week', 'Contrast', 'Lower', 'Upper')])
    p1 <- ggplot(k1, aes(x=week, y=Contrast)) + geom_point() +
          geom_line() + ylab('Low Dose - Placebo') +
          geom_errorbar(aes(ymin=Lower, ymax=Upper), width=0)
    k2 <- as.data.frame(k2[c('week', 'Contrast', 'Lower', 'Upper')])
    p2 <- ggplot(k2, aes(x=week, y=Contrast)) + geom_point() +
          geom_line() + ylab('High Dose - Placebo') +
          geom_errorbar(aes(ymin=Lower, ymax=Upper), width=0)
    gridExtra::grid.arrange(p1, p2, ncol=2)

    Figure 7.8: Contrasts and 0.95 confidence limits from GLS fit

    Although multiple d.f. tests such as total treatment effects or treatment \(\times\) time interaction tests are comprehensive, their increased degrees of freedom can dilute power. In a treatment comparison, treatment contrasts at the last time point (single d.f. tests) are often of major interest. Such contrasts are informed by all the measurements made by all subjects (up until dropout times) when a smooth time trend is assumed.

    V
    Code
    n <- nomogram(a, age=c(seq(20, 80, by=10), 85))
    plot(n, cex.axis=.55, cex.var=.8, lmgp=.25)  # Figure (*\ref{fig:longit-nomogram}*)

    Figure 7.9: Nomogram from GLS fit. Second axis is the baseline score.

    7.8.3 Bayesian Proportional Odds Random Effects Model

    • Develop a \(y\)-transformation invariant longitudinal model
    • Proportional odds model with no grouping of TWSTRS scores
    • Bayesian random effects model
    • Random effects Gaussian with exponential prior distribution for its SD, with mean 1.0
    • Compound symmetry correlation structure
    • Demonstrates a large amount of patient-to-patient intercept variability
    W
    Code
    require(rmsb)
    options(mc.cores=parallel::detectCores() - 1)
    bpo <- blrm(twstrs ~ treat * rcs(week, 3) + rcs(twstrs0, 3) +
                rcs(age, 4) * sex + cluster(uid), data=both, file='bpo.rds')
    # file= means that after the first time the model is run, it will not
    # be re-run unless the data, fitting options, or underlying Stan code change
    stanDx(bpo)
    Iterations: 2000 on each of 4 chains, with 4000 posterior distribution samples saved
    
    For each parameter, n_eff is a crude measure of effective sample size
    and Rhat is the potential scale reduction factor on split chains
    (at convergence, Rhat=1)
                          n_eff  Rhat
    y>=7                   1931 1.001
    y>=9                   1586 1.001
    y>=10                  1075 1.002
    y>=11                   974 1.002
    y>=13                   969 1.001
    y>=14                   891 1.002
    y>=15                   841 1.002
    y>=16                   746 1.003
    y>=17                   668 1.002
    y>=18                   627 1.003
    y>=19                   606 1.003
    y>=20                   599 1.003
    y>=21                   588 1.003
    y>=22                   568 1.003
    y>=23                   557 1.003
    y>=24                   554 1.003
    y>=25                   541 1.003
    y>=26                   531 1.002
    y>=27                   501 1.003
    y>=28                   493 1.003
    y>=29                   487 1.003
    y>=30                   486 1.003
    y>=31                   468 1.003
    y>=32                   458 1.003
    y>=33                   454 1.003
    y>=34                   446 1.003
    y>=35                   445 1.003
    y>=36                   448 1.003
    y>=37                   454 1.004
    y>=38                   454 1.004
    y>=39                   462 1.004
    y>=40                   475 1.004
    y>=41                   472 1.004
    y>=42                   490 1.004
    y>=43                   505 1.003
    y>=44                   539 1.003
    y>=45                   577 1.002
    y>=46                   610 1.002
    y>=47                   682 1.002
    y>=48                   748 1.002
    y>=49                   797 1.002
    y>=50                   862 1.002
    y>=51                   979 1.001
    y>=52                  1087 1.002
    y>=53                  1192 1.002
    y>=54                  1295 1.002
    y>=55                  1374 1.001
    y>=56                  1476 1.001
    y>=57                  1636 1.002
    y>=58                  1669 1.001
    y>=59                  1759 1.002
    y>=60                  1836 1.001
    y>=61                  1821 1.002
    y>=62                  1815 1.002
    y>=63                  1905 1.001
    y>=64                  2003 1.001
    y>=65                  2158 1.000
    y>=66                  2190 1.001
    y>=67                  2195 1.000
    y>=68                  2564 1.000
    y>=71                  3020 1.000
    treat=5000U             825 1.008
    treat=Placebo           818 1.007
    week                   2366 1.001
    week'                  4255 1.000
    twstrs0                 944 1.001
    twstrs0'                813 1.004
    age                     773 1.007
    age'                    952 1.000
    age''                   802 1.004
    sex=M                   806 1.005
    treat=5000U * week     4486 1.000
    treat=Placebo * week   3934 1.000
    treat=5000U * week'    3969 1.000
    treat=Placebo * week'  3795 1.001
    age * sex=M             789 1.004
    age' * sex=M           1016 1.001
    age'' * sex=M           953 1.005
    sigmag                  818 1.008
    Code
    print(bpo, intercepts=TRUE)
    Bayesian Proportional Odds Ordinal Logistic Model Dirichlet Priors With Concentration Parameter 0.044 for Intercepts
     blrm(formula = twstrs ~ treat * rcs(week, 3) + rcs(twstrs0, 3) + 
         rcs(age, 4) * sex + cluster(uid), data = both, file = "bpo.rds")
     
    Mixed Calibration/
    Discrimination Indexes
    Discrimination
    Indexes
    Rank Discrim.
    Indexes
    Obs 522 LOO log L -1747.06±23.82 g 3.836 [3.317, 4.348] C 0.793 [0.786, 0.8]
    Draws 4000 LOO IC 3494.11±47.64 gp 0.435 [0.419, 0.451] Dxy 0.586 [0.573, 0.599]
    Chains 4 Effective p 180.06±8 EV 0.593 [0.539, 0.637]
    p 17 B 0.148 [0.139, 0.157] v 11.444 [8.35, 14.315]
    Cluster on uid vp 0.148 [0.133, 0.158]
    Clusters 108
    σγ 1.8981 [1.5585, 2.2803]
    Mean β Median β S.E. Lower Upper Pr(β>0) Symmetry
    y≥7   -1.7191   -1.7086  4.1766   -9.6105   6.4373  0.3375  1.03
    y≥9   -2.7236   -2.7023  4.0317  -10.7243   4.8974  0.2452  1.04
    y≥10   -3.8912   -3.8459  4.0001  -12.3714   3.3560  0.1642  1.02
    y≥11   -4.3394   -4.3092  4.0018  -12.4477   3.2742  0.1388  1.03
    y≥13   -4.5433   -4.5053  4.0018  -12.2223   3.4735  0.1282  1.01
    y≥14   -4.8969   -4.8879  4.0085  -12.7268   2.9463  0.1115  1.03
    y≥15   -5.2033   -5.1678  4.0115  -12.7349   2.9840  0.0968  1.00
    y≥16   -5.5905   -5.5340  4.0093  -13.3053   2.3238  0.0813  1.02
    y≥17   -6.3937   -6.3309  4.0005  -14.1682   1.4446  0.0555  1.02
    y≥18   -6.6461   -6.5994  4.0012  -14.5443   1.0756  0.0485  1.03
    y≥19   -6.9314   -6.8968  3.9974  -14.6443   0.9429  0.0418  1.02
    y≥20   -7.1276   -7.1106  3.9954  -14.8587   0.7071  0.0378  1.01
    y≥21   -7.3106   -7.2910  3.9958  -14.8884   0.6652  0.0355  1.03
    y≥22   -7.7261   -7.7001  3.9965  -15.3654   0.2219  0.0283  1.02
    y≥23   -7.9912   -7.9845  3.9977  -15.6279   -0.1005  0.0232  1.02
    y≥24   -8.2736   -8.2762  3.9993  -16.0333   -0.4560  0.0213  1.01
    y≥25   -8.5333   -8.5168  3.9999  -16.3824   -0.7493  0.0182  1.00
    y≥26   -8.9276   -8.9112  4.0011  -16.6378   -1.0453  0.0140  1.01
    y≥27   -9.2184   -9.2061  4.0024  -17.0357   -1.4627  0.0110  1.01
    y≥28   -9.4641   -9.4420  4.0040  -17.2348   -1.6345  0.0088  1.01
    y≥29   -9.6980   -9.6731  4.0072  -17.7531   -2.1159  0.0080  1.01
    y≥30  -10.0018   -9.9876  4.0095  -18.1129   -2.5191  0.0065  1.01
    y≥31  -10.2945  -10.2780  4.0147  -18.2631   -2.6256  0.0050  1.01
    y≥32  -10.4116  -10.3932  4.0158  -18.4052   -2.7686  0.0048  1.01
    y≥33  -10.7773  -10.7734  4.0199  -18.8141   -3.1037  0.0035  1.00
    y≥34  -11.0896  -11.0931  4.0231  -19.0530   -3.3899  0.0030  1.00
    y≥35  -11.3124  -11.2993  4.0255  -19.2395   -3.5664  0.0027  1.01
    y≥36  -11.5581  -11.5286  4.0283  -19.5081   -3.8680  0.0022  1.00
    y≥37  -11.8361  -11.8045  4.0284  -19.8074   -4.1829  0.0020  1.01
    y≥38  -12.0656  -12.0284  4.0319  -20.0836   -4.4298  0.0015  1.01
    y≥39  -12.3136  -12.2821  4.0326  -20.3375   -4.7279  0.0015  1.01
    y≥40  -12.4977  -12.4734  4.0338  -20.5465   -4.8461  0.0015  1.00
    y≥41  -12.6824  -12.6636  4.0354  -20.6768   -4.9834  0.0015  1.01
    y≥42  -13.0077  -12.9797  4.0396  -20.9721   -5.2363  0.0008  1.01
    y≥43  -13.2359  -13.2081  4.0407  -21.2482   -5.5630  0.0008  1.01
    y≥44  -13.5758  -13.5430  4.0441  -21.5996   -5.8619  0.0008  1.01
    y≥45  -13.8954  -13.8714  4.0492  -21.9709   -6.2272  0.0003  1.03
    y≥46  -14.1960  -14.1662  4.0523  -22.2073   -6.4717  0.0000  1.02
    y≥47  -14.6161  -14.6033  4.0563  -22.6775   -6.7963  0.0000  1.02
    y≥48  -14.9060  -14.8792  4.0590  -22.9425   -7.0649  0.0000  1.01
    y≥49  -15.2792  -15.2793  4.0626  -23.3720   -7.4847  0.0000  1.02
    y≥50  -15.5967  -15.5930  4.0619  -23.7015   -7.7603  0.0000  1.02
    y≥51  -16.1257  -16.1037  4.0640  -24.3582   -8.4291  0.0000  1.01
    y≥52  -16.4895  -16.4835  4.0666  -24.6597   -8.7545  0.0000  1.00
    y≥53  -16.9326  -16.9465  4.0681  -25.1827   -9.2667  0.0000  1.00
    y≥54  -17.4333  -17.4173  4.0726  -25.6578   -9.7302  0.0000  1.00
    y≥55  -17.8448  -17.8246  4.0758  -26.1694  -10.2547  0.0000  1.01
    y≥56  -18.0977  -18.0779  4.0749  -26.4472  -10.5733  0.0000  1.01
    y≥57  -18.5706  -18.5416  4.0770  -26.8814  -10.9416  0.0000  1.00
    y≥58  -19.1291  -19.0777  4.0800  -27.6838  -11.7421  0.0000  1.00
    y≥59  -19.4834  -19.4351  4.0848  -28.1194  -12.1018  0.0000  1.00
    y≥60  -19.8113  -19.7595  4.0876  -28.1239  -12.0784  0.0000  1.00
    y≥61  -20.5029  -20.4527  4.0929  -28.5020  -12.3685  0.0000  1.01
    y≥62  -20.8746  -20.8267  4.1009  -28.8901  -12.7407  0.0000  1.01
    y≥63  -21.2850  -21.2655  4.1025  -29.0878  -12.9327  0.0000  1.01
    y≥64  -21.4270  -21.3833  4.1021  -29.4859  -13.3743  0.0000  1.00
    y≥65  -22.1493  -22.1173  4.1185  -30.5740  -14.2947  0.0000  1.00
    y≥66  -22.5320  -22.4943  4.1228  -30.7055  -14.4468  0.0000  0.99
    y≥67  -22.9390  -22.8732  4.1328  -30.4445  -14.1472  0.0000  0.98
    y≥68  -23.7199  -23.6754  4.1648  -31.5569  -15.1853  0.0000  1.00
    y≥71  -24.5811  -24.5467  4.2008  -32.6839  -16.2789  0.0000  0.97
    treat=5000U   0.0850   0.0884  0.7042   -1.2945   1.4331  0.5508  0.99
    treat=Placebo   2.3160   2.3034  0.7122   0.9175   3.7259  0.9998  0.99
    week   0.1223   0.1240  0.0794   -0.0374   0.2760  0.9358  1.01
    week'   0.1920   0.1923  0.0859   0.0282   0.3608  0.9890  1.03
    twstrs0   0.2308   0.2297  0.0520   0.1329   0.3352  1.0000  1.03
    twstrs0'   0.1257   0.1250  0.0629   0.0090   0.2570  0.9782  1.06
    age   -0.0189   -0.0186  0.0775   -0.1708   0.1299  0.4040  1.00
    age'   0.2015   0.2015  0.2166   -0.2008   0.6466  0.8265  0.99
    age''   -1.1010   -1.1062  0.8620   -2.8539   0.5139  0.0980  1.02
    sex=M   4.8612   4.6934  6.3748   -7.8288   17.4345  0.7808  1.02
    treat=5000U × week   0.0511   0.0491  0.1090   -0.1488   0.2726  0.6738  1.03
    treat=Placebo × week   -0.0552   -0.0538  0.1127   -0.2843   0.1540  0.3155  0.99
    treat=5000U × week'   -0.1629   -0.1623  0.1183   -0.3965   0.0607  0.0825  0.99
    treat=Placebo × week'   -0.1387   -0.1381  0.1229   -0.3804   0.0929  0.1275  1.00
    age × sex=M   -0.1072   -0.1058  0.1531   -0.4102   0.1916  0.2405  0.97
    age' × sex=M   0.1448   0.1476  0.4307   -0.6790   0.9820  0.6345  1.00
    age'' × sex=M   0.0597   0.0572  1.6794   -3.1683   3.3191  0.5142  1.02
    Code
    a <- anova(bpo)
    a
    Relative Explained Variation for twstrs. Approximate total model Wald χ2 used in denominators of REV:265.3 [215.6, 349.5].
    REV Lower Upper d.f.
    treat (Factor+Higher Order Factors) 0.122 0.064 0.217 6
    All Interactions 0.084 0.026 0.159 4
    week (Factor+Higher Order Factors) 0.587 0.466 0.693 6
    All Interactions 0.084 0.026 0.159 4
    Nonlinear (Factor+Higher Order Factors) 0.021 0.001 0.067 3
    twstrs0 0.648 0.504 0.731 2
    Nonlinear 0.015 0.000 0.052 1
    age (Factor+Higher Order Factors) 0.022 0.010 0.084 6
    All Interactions 0.014 0.000 0.054 3
    Nonlinear (Factor+Higher Order Factors) 0.019 0.005 0.073 4
    sex (Factor+Higher Order Factors) 0.018 0.001 0.065 4
    All Interactions 0.014 0.000 0.054 3
    treat × week (Factor+Higher Order Factors) 0.084 0.026 0.159 4
    Nonlinear 0.008 0.000 0.040 2
    Nonlinear Interaction : f(A,B) vs. AB 0.008 0.000 0.040 2
    age × sex (Factor+Higher Order Factors) 0.014 0.000 0.054 3
    Nonlinear 0.013 0.000 0.049 2
    Nonlinear Interaction : f(A,B) vs. AB 0.013 0.000 0.049 2
    TOTAL NONLINEAR 0.051 0.026 0.138 8
    TOTAL INTERACTION 0.099 0.051 0.192 7
    TOTAL NONLINEAR + INTERACTION 0.131 0.082 0.246 11
    TOTAL 1.000 1.000 1.000 17
    Code
    plot(a)

    • Show the final graphic (high dose:placebo contrast as function of time
    • Intervals are 0.95 highest posterior density intervals
    • \(y\)-axis: log-odds ratio
    X
    Code
    wks <- c(2,4,8,12,16)
    k <- contrast(bpo, list(week=wks, treat='10000U'),
                       list(week=wks, treat='Placebo'),
                  cnames=paste('Week', wks))
    k
               week   Contrast      S.E.      Lower      Upper Pr(Contrast>0)
    1  Week 2     2 -2.2055300 0.5757226 -3.3624971 -1.1038486         0.0003
    2  Week 4     4 -2.0950834 0.5072119 -3.0838983 -1.1036630         0.0003
    3  Week 8     8 -1.7354713 0.5683718 -2.8431190 -0.6719860         0.0013
    4* Week 12   12 -0.8209835 0.5120239 -1.7888111  0.1635676         0.0495
    5* Week 16   16  0.2322233 0.5974977 -0.9390499  1.3964865         0.6525
    
    Redundant contrasts are denoted by *
    
    Intervals are 0.95 highest posterior density intervals
    Contrast is the posterior mean 
    Code
    plot(k)

    Code
    k <- as.data.frame(k[c('week', 'Contrast', 'Lower', 'Upper')])
    ggplot(k, aes(x=week, y=Contrast)) + geom_point() +
      geom_line() + ylab('High Dose - Placebo') +
      geom_errorbar(aes(ymin=Lower, ymax=Upper), width=0)

    For each posterior draw compute the difference in means and get an exact (to within simulation error) 0.95 highest posterior density intervals for these differences.

    Code
    M <- Mean(bpo)   # create R function that computes mean Y from X*beta
    k <- contrast(bpo, list(week=wks, treat='10000U'),
                       list(week=wks, treat='Placebo'),
                  fun=M, cnames=paste('Week', wks))
    plot(k, which='diff') + theme(legend.position='bottom')

    Code
    f <- function(x) {
      hpd <- HPDint(x, prob=0.95)   # is in rmsb
      r <- c(mean(x), median(x), hpd)
      names(r) <- c('Mean', 'Median', 'Lower', 'Upper')
      r
    }
    w    <- as.data.frame(t(apply(k$esta - k$estb, 2, f)))
    week <- as.numeric(sub('Week ', '', rownames(w)))
    ggplot(w, aes(x=week, y=Mean)) + geom_point() +
      geom_line() + ylab('High Dose - Placebo') +
      geom_errorbar(aes(ymin=Lower, ymax=Upper), width=0) +
      scale_y_continuous(breaks=c(-8, -4, 0, 4))

    7.8.4 Bayesian Markov Semiparametric Model

    • First-order Markov model
    • Serial correlation induced by Markov model is similar to AR(1) which we already know fits these data
    • Markov model is more likely to fit the data than the random effects model, which induces a compound symmetry correlation structure
    • Models state transitions
    • PO model at each visit, with Y from previous visit conditioned upon just like any covariate
    • Need to uncondition (marginalize) on previous Y to get the time-response profile we usually need
    • Semiparametric model is especially attractive because one can easily “uncondition” a discrete Y model, and the distribution of Y for control subjects can be any shape
    • Let measurement times be \(t_{1}, t_{2}, \dots, t_{m}\), and the measurement for a subject at time \(t\) be denoted \(Y(t)\)
    • First-order Markov model:
    Y
    \[\begin{array}{ccc} \Pr(Y(t_{i}) \geq y | X, Y(t_{i-1})) &=& \mathrm{expit}(\alpha_{y} + X\beta\\ &+& g(Y(t_{i-1}), t_{i}, t_{i} - t_{i-1})) \end{array}\]
    • \(g\) involves any number of regression coefficients for a main effect of \(t\), the main effect of time gap \(t_{i} - t_{i-1}\) if this is not collinear with absolute time, a main effect of the previous state, and interactions between these
    • Examples of how the previous state may be modeled in \(g\):
      • linear in numeric codes for \(Y\)
      • spline function in same
      • discontinuous bi-linear relationship where there is a slope for in-hospital outcome severity, a separate slope for outpatient outcome severity, and an intercept jump at the transition from inpatient to outpatient (or vice versa)
    • Markov model is quite flexible in handling time trends and serial correlation patterns
    • Can allow for irregular measurement times:
      hbiostat.org/stat/irreg.html

    Fit the model and run standard Stan diagnostics.

    Code
    # Create a new variable to hold previous value of Y for the subject
    # For week 2, previous value is the baseline value
    setDT(both, key=c('uid', 'week'))
    both[, ptwstrs := shift(twstrs), by=uid]
    both[week == 2, ptwstrs := twstrs0]
    dd <- datadist(both)
    bmark <- blrm(twstrs ~  treat * rcs(week, 3) + rcs(ptwstrs, 4) +
                            rcs(age, 4) * sex,
                  data=both, file='bmark.rds')
    # When adding partial PO terms for week and ptwstrs, z=-1.8, 5.04
    stanDx(bmark)
    Iterations: 2000 on each of 4 chains, with 4000 posterior distribution samples saved
    
    For each parameter, n_eff is a crude measure of effective sample size
    and Rhat is the potential scale reduction factor on split chains
    (at convergence, Rhat=1)
                          n_eff  Rhat
    y>=7                   3184 1.001
    y>=9                   4462 1.000
    y>=10                  5697 1.000
    y>=11                  5271 1.000
    y>=13                  5158 1.000
    y>=14                  4650 1.000
    y>=15                  4914 1.000
    y>=16                  4537 1.000
    y>=17                  4396 1.000
    y>=18                  4416 1.000
    y>=19                  4053 1.000
    y>=20                  4240 1.000
    y>=21                  4077 0.999
    y>=22                  4000 1.000
    y>=23                  4089 1.000
    y>=24                  3917 1.000
    y>=25                  3831 1.000
    y>=26                  3985 1.000
    y>=27                  4025 1.000
    y>=28                  4045 1.000
    y>=29                  4079 1.000
    y>=30                  4176 1.000
    y>=31                  4191 1.000
    y>=32                  4220 1.000
    y>=33                  4440 1.001
    y>=34                  4641 1.001
    y>=35                  4815 1.000
    y>=36                  5164 1.000
    y>=37                  5494 1.000
    y>=38                  5583 1.000
    y>=39                  5782 1.000
    y>=40                  6204 1.000
    y>=41                  6524 1.000
    y>=42                  7442 1.000
    y>=43                  8059 1.000
    y>=44                  7701 1.000
    y>=45                  7791 0.999
    y>=46                  7227 0.999
    y>=47                  6891 1.000
    y>=48                  6440 0.999
    y>=49                  6348 0.999
    y>=50                  5776 0.999
    y>=51                  5419 1.000
    y>=52                  4916 0.999
    y>=53                  4610 0.999
    y>=54                  4657 0.999
    y>=55                  5268 1.000
    y>=56                  5100 1.000
    y>=57                  4948 1.000
    y>=58                  5046 0.999
    y>=59                  5061 0.999
    y>=60                  5890 0.999
    y>=61                  6443 1.000
    y>=62                  6370 1.000
    y>=63                  6279 1.000
    y>=64                  5954 1.000
    y>=65                  5754 1.000
    y>=66                  5650 1.000
    y>=67                  5479 1.000
    y>=68                  5437 1.000
    y>=71                  5299 1.000
    treat=5000U            8354 0.999
    treat=Placebo          7725 0.999
    week                   5807 0.999
    week'                  8450 1.000
    ptwstrs                3729 1.000
    ptwstrs'               5826 1.001
    ptwstrs''              6968 0.999
    age                   10582 0.999
    age'                   7782 0.999
    age''                  7527 0.999
    sex=M                  9600 0.999
    treat=5000U * week     9462 0.999
    treat=Placebo * week   9979 0.999
    treat=5000U * week'    9772 1.000
    treat=Placebo * week'  9038 1.000
    age * sex=M            8328 0.999
    age' * sex=M           7229 0.999
    age'' * sex=M          9491 1.000
    Code
    stanDxplot(bmark)

    Note that posterior sampling is much more efficient without random effects.

    Code
    bmark

    Bayesian Proportional Odds Ordinal Logistic Model

    Dirichlet Priors With Concentration Parameter 0.044 for Intercepts

     blrm(formula = twstrs ~ treat * rcs(week, 3) + rcs(ptwstrs, 4) + 
         rcs(age, 4) * sex, data = both, file = "bmark.rds")
     


    Frequencies of Missing Values Due to Each Variable

      twstrs   treat    week ptwstrs     age     sex 
           0       0       0       5       0       0 
     
    Mixed Calibration/
    Discrimination Indexes
    Discrimination
    Indexes
    Rank Discrim.
    Indexes
    Obs 517 LOO log L -1787.07±22.56 g 3.281 [3.003, 3.552] C 0.828 [0.825, 0.831]
    Draws 4000 LOO IC 3574.13±45.12 gp 0.416 [0.403, 0.429] Dxy 0.656 [0.65, 0.661]
    Chains 4 Effective p 90.93±5.47 EV 0.534 [0.495, 0.573]
    p 18 B 0.117 [0.113, 0.121] v 8.477 [7.071, 9.907]
    vp 0.133 [0.122, 0.142]
    Mode β Mean β Median β S.E. Lower Upper Pr(β>0) Symmetry
    treat=5000U   0.2211   0.2163   0.2213  0.5528  -0.8256   1.2918  0.6567  1.01
    treat=Placebo   1.8298   1.8389   1.8221  0.5704   0.7254   2.9616  0.9988  1.01
    week   0.4861   0.4885   0.4885  0.0801   0.3289   0.6411  1.0000  1.01
    week’  -0.2876  -0.2893  -0.2901  0.0866  -0.4631  -0.1230  0.0008  0.99
    ptwstrs   0.1997   0.2016   0.2017  0.0263   0.1518   0.2536  1.0000  1.02
    ptwstrs’  -0.0623  -0.0659  -0.0662  0.0629  -0.1871   0.0595  0.1467  1.00
    ptwstrs’’   0.5330   0.5489   0.5518  0.2510   0.0627   1.0380  0.9858  1.02
    age  -0.0295  -0.0292  -0.0289  0.0316  -0.0933   0.0333  0.1673  1.01
    age’   0.1237   0.1228   0.1223  0.0889  -0.0522   0.3056  0.9180  1.00
    age’’  -0.5070  -0.5045  -0.5049  0.3518  -1.2153   0.1810  0.0725  1.00
    sex=M  -0.4644  -0.4427  -0.4729  2.3151  -5.0803   4.0610  0.4228  1.03
    treat=5000U × week  -0.0341  -0.0334  -0.0326  0.1054  -0.2371   0.1675  0.3757  0.99
    treat=Placebo × week  -0.2717  -0.2731  -0.2699  0.1109  -0.4897  -0.0545  0.0060  0.99
    treat=5000U × week’  -0.0339  -0.0342  -0.0344  0.1147  -0.2473   0.1908  0.3782  1.01
    treat=Placebo × week’   0.1197   0.1201   0.1199  0.1207  -0.1065   0.3560  0.8408  1.01
    age × sex=M   0.0112   0.0109   0.0113  0.0560  -0.1038   0.1148  0.5808  0.97
    age’ × sex=M  -0.0510  -0.0503  -0.0519  0.1605  -0.3595   0.2579  0.3832  1.01
    age’’ × sex=M   0.2618   0.2608   0.2481  0.6267  -1.0713   1.3801  0.6615  0.99
    Code
    a <- anova(bpo)
    a
    Relative Explained Variation for twstrs. Approximate total model Wald χ2 used in denominators of REV:265.3 [215.9, 356].
    REV Lower Upper d.f.
    treat (Factor+Higher Order Factors) 0.122 0.064 0.217 6
    All Interactions 0.084 0.035 0.162 4
    week (Factor+Higher Order Factors) 0.587 0.456 0.680 6
    All Interactions 0.084 0.035 0.162 4
    Nonlinear (Factor+Higher Order Factors) 0.021 0.001 0.061 3
    twstrs0 0.648 0.521 0.742 2
    Nonlinear 0.015 0.000 0.048 1
    age (Factor+Higher Order Factors) 0.022 0.010 0.088 6
    All Interactions 0.014 0.001 0.055 3
    Nonlinear (Factor+Higher Order Factors) 0.019 0.004 0.072 4
    sex (Factor+Higher Order Factors) 0.018 0.002 0.067 4
    All Interactions 0.014 0.001 0.055 3
    treat × week (Factor+Higher Order Factors) 0.084 0.035 0.162 4
    Nonlinear 0.008 0.000 0.040 2
    Nonlinear Interaction : f(A,B) vs. AB 0.008 0.000 0.040 2
    age × sex (Factor+Higher Order Factors) 0.014 0.001 0.055 3
    Nonlinear 0.013 0.000 0.050 2
    Nonlinear Interaction : f(A,B) vs. AB 0.013 0.000 0.050 2
    TOTAL NONLINEAR 0.051 0.022 0.134 8
    TOTAL INTERACTION 0.099 0.049 0.190 7
    TOTAL NONLINEAR + INTERACTION 0.131 0.084 0.248 11
    TOTAL 1.000 1.000 1.000 17
    Code
    plot(a)

    Let’s add subject-level random effects to the model. Smallness of the standard deviation of the random effects provides support for the assumption of conditional independence that we like to make for Markov models and allows us to simplify the model by omitting random effects.

    Code
    bmarkre <- blrm(twstrs ~  treat * rcs(week, 3) + rcs(ptwstrs, 4) +
                              rcs(age, 4) * sex + cluster(uid),
                    data=both, file='bmarkre.rds')
    stanDx(bmarkre)
    Iterations: 2000 on each of 4 chains, with 4000 posterior distribution samples saved
    
    For each parameter, n_eff is a crude measure of effective sample size
    and Rhat is the potential scale reduction factor on split chains
    (at convergence, Rhat=1)
                          n_eff  Rhat
    y>=7                   2901 1.001
    y>=9                   3942 1.000
    y>=10                  3982 1.000
    y>=11                  3699 1.001
    y>=13                  3491 1.001
    y>=14                  3231 1.002
    y>=15                  2929 1.002
    y>=16                  2955 1.002
    y>=17                  2535 1.002
    y>=18                  2587 1.002
    y>=19                  2550 1.002
    y>=20                  2509 1.002
    y>=21                  2460 1.002
    y>=22                  2340 1.002
    y>=23                  2315 1.003
    y>=24                  2302 1.003
    y>=25                  2274 1.003
    y>=26                  2445 1.002
    y>=27                  2436 1.001
    y>=28                  2337 1.002
    y>=29                  2410 1.002
    y>=30                  2421 1.002
    y>=31                  2588 1.001
    y>=32                  2639 1.001
    y>=33                  2827 1.001
    y>=34                  2777 1.001
    y>=35                  2787 1.001
    y>=36                  3057 1.001
    y>=37                  3089 1.001
    y>=38                  3344 1.001
    y>=39                  3625 1.000
    y>=40                  3681 1.000
    y>=41                  3837 1.001
    y>=42                  4303 1.001
    y>=43                  4101 1.001
    y>=44                  4750 1.000
    y>=45                  5143 1.000
    y>=46                  5208 1.000
    y>=47                  5400 1.000
    y>=48                  5066 1.001
    y>=49                  4855 1.000
    y>=50                  4444 1.001
    y>=51                  3916 1.001
    y>=52                  3997 1.001
    y>=53                  3742 1.000
    y>=54                  3582 1.000
    y>=55                  3718 1.000
    y>=56                  3604 1.000
    y>=57                  3693 1.000
    y>=58                  3581 1.000
    y>=59                  3519 1.000
    y>=60                  3778 1.000
    y>=61                  3850 1.000
    y>=62                  3962 1.000
    y>=63                  3689 1.000
    y>=64                  3664 1.000
    y>=65                  3577 1.000
    y>=66                  3691 1.000
    y>=67                  3548 1.000
    y>=68                  3606 1.000
    y>=71                  4261 1.000
    treat=5000U            5799 0.999
    treat=Placebo          5942 1.000
    week                   4881 1.000
    week'                  6647 0.999
    ptwstrs                2306 1.002
    ptwstrs'               4971 1.000
    ptwstrs''              5072 1.000
    age                    5321 1.000
    age'                   5553 1.000
    age''                  7765 0.999
    sex=M                  6524 0.999
    treat=5000U * week     6313 0.999
    treat=Placebo * week   5622 1.000
    treat=5000U * week'    5791 1.000
    treat=Placebo * week'  6425 1.001
    age * sex=M            7000 1.000
    age' * sex=M           6808 0.999
    age'' * sex=M          6614 1.000
    sigmag                 1420 1.002
    Code
    bmarkre

    Bayesian Proportional Odds Ordinal Logistic Model

    Dirichlet Priors With Concentration Parameter 0.044 for Intercepts

     blrm(formula = twstrs ~ treat * rcs(week, 3) + rcs(ptwstrs, 4) + 
         rcs(age, 4) * sex + cluster(uid), data = both, file = "bmarkre.rds")
     


    Frequencies of Missing Values Due to Each Variable

           twstrs        treat         week      ptwstrs          age          sex 
                0            0            0            5            0            0 
     cluster(uid) 
                0 
     
    Mixed Calibration/
    Discrimination Indexes
    Discrimination
    Indexes
    Rank Discrim.
    Indexes
    Obs 517 LOO log L -1787.77±22.46 g 3.255 [2.992, 3.586] C 0.828 [0.825, 0.831]
    Draws 4000 LOO IC 3575.54±44.91 gp 0.415 [0.402, 0.429] Dxy 0.656 [0.65, 0.661]
    Chains 4 Effective p 94.69±5.09 EV 0.531 [0.489, 0.568]
    p 18 B 0.117 [0.113, 0.121] v 8.348 [7.044, 10.047]
    Cluster on uid vp 0.133 [0.122, 0.142]
    Clusters 108
    σγ 0.1122 [1e-04, 0.3369]
    Mean β Median β S.E. Lower Upper Pr(β>0) Symmetry
    treat=5000U   0.2203   0.2222  0.5698  -0.9025   1.3427  0.6555  0.99
    treat=Placebo   1.8295   1.8283  0.5581   0.7444   2.9443  0.9990  0.99
    week   0.4858   0.4875  0.0808   0.3243   0.6365  1.0000  0.97
    week’  -0.2852  -0.2857  0.0858  -0.4548  -0.1219  0.0005  1.03
    ptwstrs   0.1999   0.2002  0.0275   0.1459   0.2530  1.0000  1.04
    ptwstrs’  -0.0649  -0.0643  0.0655  -0.1893   0.0644  0.1600  1.05
    ptwstrs’’   0.5454   0.5474  0.2620   0.0474   1.0575  0.9820  0.99
    age  -0.0278  -0.0278  0.0321  -0.0876   0.0354  0.1952  1.02
    age’   0.1202   0.1192  0.0901  -0.0512   0.2956  0.9125  0.96
    age’’  -0.4979  -0.4914  0.3567  -1.2135   0.1790  0.0775  1.00
    sex=M  -0.3721  -0.3983  2.4945  -5.1433   4.5524  0.4315  1.03
    treat=5000U × week  -0.0319  -0.0333  0.1099  -0.2503   0.1775  0.3815  0.99
    treat=Placebo × week  -0.2710  -0.2713  0.1068  -0.4870  -0.0707  0.0053  0.98
    treat=5000U × week’  -0.0374  -0.0357  0.1194  -0.2768   0.1907  0.3733  0.95
    treat=Placebo × week’   0.1181   0.1192  0.1157  -0.1150   0.3317  0.8433  1.02
    age × sex=M   0.0089   0.0099  0.0604  -0.1114   0.1233  0.5662  0.97
    age’ × sex=M  -0.0460  -0.0481  0.1712  -0.3699   0.2937  0.3960  1.01
    age’’ × sex=M   0.2483   0.2531  0.6628  -1.0123   1.5505  0.6462  0.99

    The random effects SD is only 0.11 on the logit scale. Also, the standard deviations of all the regression parameter posterior distributions are virtually unchanged with the addition of random effects:

    Code
    plot(sqrt(diag(vcov(bmark))), sqrt(diag(vcov(bmarkre))),
         xlab='Posterior SDs in Conditional Independence Markov Model',
         ylab='Posterior SDs in Random Effects Markov Model')
    abline(a=0, b=1, col=gray(0.85))

    So we will use the model omitting random effects.

    Show the partial effects of all the predictors, including the effect of the previous measurement of TWSTRS. Also compute high dose:placebo treatment contrasts on these conditional estimates.

    Code
    ggplot(Predict(bmark))

    Code
    ggplot(Predict(bmark, week, treat))

    Code
    k <- contrast(bmark, list(week=wks, treat='10000U'),
                         list(week=wks, treat='Placebo'),
                  cnames=paste('Week', wks))
    k
               week   Contrast      S.E.      Lower      Upper Pr(Contrast>0)
    1  Week 2     2 -1.2926060 0.3845846 -2.0330827 -0.5207000         0.0010
    2  Week 4     4 -0.7463258 0.2622612 -1.2851254 -0.2655667         0.0030
    3  Week 8     8  0.2260867 0.3484320 -0.4708920  0.8989799         0.7390
    4* Week 12   12  0.7179082 0.2531154  0.2369301  1.2248555         0.9980
    5* Week 16   16  1.0895819 0.3963937  0.3532124  1.8780855         0.9968
    
    Redundant contrasts are denoted by *
    
    Intervals are 0.95 highest posterior density intervals
    Contrast is the posterior mean 
    Code
    plot(k)

    Code
    k <- as.data.frame(k[c('week', 'Contrast', 'Lower', 'Upper')])
    ggplot(k, aes(x=week, y=Contrast)) + geom_point() +
      geom_line() + ylab('High Dose - Placebo') +
      geom_errorbar(aes(ymin=Lower, ymax=Upper), width=0)

    Using posterior means for parameter values, compute the probability that at a given week twstrs will be \(\geq 40\) when at the previous visit it was 40. Also show the conditional mean twstrs when it was 40 at the previous visit.

    Code
    ex <- ExProb(bmark)
    ex40 <- function(lp, ...) ex(lp, y=40, ...)
    ggplot(Predict(bmark, week, treat, ptwstrs=40, fun=ex40))

    Code
    ggplot(Predict(bmark, week, treat, ptwstrs=40, fun=Mean(bmark)))

    • Semiparametric models provide not only estimates of tendencies of Y but also estimate the whole distribution of Y
    • Estimate the entire conditional distribution of Y at week 12 for high-dose patients having TWSTRS=42 at week 8
    • Other covariates set to median/mode
    • Use posterior mean of all the cell probabilities
    • Also show pointwise 0.95 highest posterior density intervals
    • To roughly approximate simultaneous confidence bands make the pointwise limits sum to 1 like the posterior means do
    Z
    Code
    # Get median/mode for covariates including ptwstrs (TWSTRS in previous visit)
    d <- gendata(bmark)
    d
       treat week ptwstrs age sex
    1 10000U    8      42  56   F
    Code
    d$week <- 12
    p <- predict(bmark, d, type='fitted.ind')   # defaults to posterior means
    yvals <- as.numeric(sub('twstrs=', '', p$y))
    lo <- p$Lower / sum(p$Lower)
    hi <- p$Upper / sum(p$Upper)
    plot(yvals, p$Mean, type='l', xlab='TWSTRS', ylab='',
         ylim=range(c(lo, hi)))
    lines(yvals, lo, col=gray(0.8))
    lines(yvals, hi, col=gray(0.8))

    • Repeat this showing the variation over 5 posterior draws
    A
    Code
    p <- predict(bmark, d, type='fitted.ind', posterior.summary='all')
    cols <- adjustcolor(1 : 10, 0.7)
    for(i in 1 : 5) {
      if(i == 1) plot(yvals, p[i, 1, ], type='l', col=cols[1], xlab='TWSTRS', ylab='')
      else lines(yvals, p[i, 1, ], col=cols[i])
    }

    • Turn to marginalized (unconditional on previous twstrs) quantities
    • Capitalize on PO model being a multinomial model, just with PO restrictions
    • Manipulations of conditional probabilities to get the unconditional probability that twstrs=y doesn’t need to know about PO
    • Compute all cell probabilities and use the law of total probability recursively \[\Pr(Y_{t} = y | X) = \sum_{j=1}^{k} \Pr(Y_{t} = y | X, Y_{t-1} = j) \Pr(Y_{t-1} = j | X)\]
    • predict.blrm method with type='fitted.ind' computes the needed conditional cell probabilities, optionally for all posterior draws at once
    • Easy to get highest posterior density intervals for derived parameters such as unconditional probabilities or unconditional means
    • Hmisc package soprobMarkovOrdm function (in version 4.6) computes an array of all the state occupancy probabilities for all the posterior draws
    B
    Code
    # Baseline twstrs to 42 in d
    # For each dose, get all the posterior draws for all state occupancy
    # probabilities for all visit
    ylev <- sort(unique(both$twstrs))
    tlev <- c('Placebo', '10000U')
    R <- list()
    for(trt in tlev) {   # separately by treatment
      d$treat <- trt
      u <- soprobMarkovOrdm(bmark, d, wks, ylev,
                            tvarname='week', pvarname='ptwstrs')
      R[[trt]] <- u
    }
    dim(R[[1]])    # posterior draws x times x distinct twstrs values
    [1] 4000    5   62
    Code
    # For each posterior draw, treatment, and week compute the mean TWSTRS
    # Then compute posterior mean of means, and HPD interval
    Rmean <- Rmeans <- list()
    for(trt in tlev) {
      r <- R[[trt]]
      # Mean Y at each week and posterior draw (mean from a discrete distribution)
      m <- apply(r, 1:2, function(x) sum(ylev * x))
      Rmeans[[trt]] <- m
      # Posterior mean and median and HPD interval over draws
      u <- apply(m, 2, f)   # f defined above
      u <- rbind(week=as.numeric(colnames(u)), u)
      Rmean[[trt]] <- u
    }
    r <- lapply(Rmean, function(x) as.data.frame(t(x)))
    for(trt in tlev) r[[trt]]$treat <- trt
    r <- do.call(rbind, r)
    ggplot(r, aes(x=week, y=Mean, color=treat)) + geom_line() +
      geom_ribbon(aes(ymin=Lower, ymax=Upper), alpha=0.2, linetype=0)

    • Use the same posterior draws of unconditional probabilities of all values of TWSTRS to get the posterior distribution of differences in mean TWSTRS between high and low dose
    C
    Code
    Dif <- Rmeans$`10000U` - Rmeans$Placebo
    dif <- as.data.frame(t(apply(Dif, 2, f)))
    dif$week <- as.numeric(rownames(dif))
    ggplot(dif, aes(x=week, y=Mean)) + geom_line() +
      geom_ribbon(aes(ymin=Lower, ymax=Upper), alpha=0.2, linetype=0) +
      ylab('High Dose - Placebo TWSTRS')

    • Get posterior mean of all cell probabilities estimates at week 12
    • Distribution of TWSTRS conditional high dose, median age, mode sex
    • Not conditional on week 8 value
    D
    Code
    p <- R$`10000U`[, '12', ]   # 4000 x 62
    pmean <- apply(p, 2, mean)
    yvals <- as.numeric(names(pmean))
    plot(yvals, pmean, type='l', xlab='TWSTRS', ylab='')