diff --git a/Comp/r-sas_mmrm.qmd b/Comp/r-sas_mmrm.qmd index c7221da56..db2d0458e 100644 --- a/Comp/r-sas_mmrm.qmd +++ b/Comp/r-sas_mmrm.qmd @@ -2,75 +2,614 @@ title: "R vs SAS MMRM" --- -```{r} -#| echo: false -plots <- readRDS("../images/mixed/plots.rds") +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, comment = "#>", out.width = "100%", + dpi = 150, fig.path = "../images/mmrm/" +) ``` -### Data +```{r review-setup} +#| message: FALSE +#| warning: FALSE +#| include: FALSE -The data used for this comparison was the lab ADaM dataset adlbh.xpt from the Phuse Pilot Study. Results were generated for each lab parameter and time point in the dataset using three different covariance structures, i.e. unstructured, compound symmetry and autoregressive of first order (AR(1)). +library(dplyr) +library(purrr) +library(microbenchmark) +library(stringr) +library(mmrm) +library(knitr) +library(emmeans) +library(ggplot2) +library(details) +``` -### Comparison between SAS and R +```{r load-data} +#| message: FALSE +#| warning: FALSE +#| include: FALSE -With results available for SAS and R model fits, we turn our attention to generating some visual comparisons of the results. Note that here we adopt a Bland-Altman type plot which plots the difference on the y-axis and the average on the x-axis. This offers a way to inspect any bias or relationships with the size of effect and the associated bias. +load('../data/cached_mmrm_results.rda') +``` -For the extracted LS-means +```{r set-seed} +#| message: FALSE +#| warning: FALSE +#| include: FALSE -```{r} -#| echo: false -#| warning: false -#| message: false -#| out.width: "100%" -plots$lsmeans_bland_alt_emmean$`Unstructured (KR)` -plots$lsmeans_bland_alt_emmean$`Unstructured (satterthwaite)` -plots$lsmeans_bland_alt_emmean$`Heterogeneous Compound Symmetry` -plots$lsmeans_bland_alt_emmean$`Heterogeneous First Order Autoregressive` +set.seed(5123) ``` -and corresponding SEs +# Introduction -```{r} -#| echo: false -#| warning: false -#| message: false -plots$lsmeans_bland_alt_se$`Unstructured (KR)` -plots$lsmeans_bland_alt_se$`Unstructured (satterthwaite)` -plots$lsmeans_bland_alt_se$`Heterogeneous Compound Symmetry` -plots$lsmeans_bland_alt_se$`Heterogeneous First Order Autoregressive` +In this vignette we briefly compare the `mmrm::mmrm`, SAS's `PROC GLIMMIX`, `nlme::gls`, `lme4::lmer`, and `glmmTMB::glmmTMB` functions for fitting mixed models for repeated measures (MMRMs). A primary difference in these implementations lies in the covariance structures that are supported "out of the box". In particular, `PROC GLIMMIX` and `mmrm` are the only procedures which provide support for many of the most common MMRM covariance structures. Most covariance structures can be implemented in `gls`, though users are required to define them manually. `lmer` and `glmmTMB` are more limited. We find that `mmmrm` converges more quickly than other R implementations while also producing estimates that are virtually identical to `PROC GLIMMIX`'s. + +# Datasets + +Two datasets are used to illustrate model fitting with the `mmrm`, `lme4`, `nlme`, `glmmTMB` R packages as well as `PROC GLIMMIX`. These data are also used to compare these implementations' operating characteristics. + +## FEV Data + +The FEV dataset contains measurements of FEV1 (forced expired volume in one second), a measure of how quickly the lungs can be emptied. Low levels of FEV1 may indicate chronic obstructive pulmonary disease (COPD). It is summarized below. + +``` + Stratified by ARMCD + Overall PBO TRT + n 800 420 380 + USUBJID (%) + PT[1-200] 200 105 (52.5) 95 (47.5) + AVISIT + VIS1 200 105 95 + VIS2 200 105 95 + VIS3 200 105 95 + VIS4 200 105 95 + RACE (%) + Asian 280 (35.0) 152 (36.2) 128 (33.7) + Black or African American 300 (37.5) 184 (43.8) 116 (30.5) + White 220 (27.5) 84 (20.0) 136 (35.8) + SEX = Female (%) 424 (53.0) 220 (52.4) 204 (53.7) + FEV1_BL (mean (SD)) 40.19 (9.12) 40.46 (8.84) 39.90 (9.42) + FEV1 (mean (SD)) 42.30 (9.32) 40.24 (8.67) 44.45 (9.51) + WEIGHT (mean (SD)) 0.52 (0.23) 0.52 (0.23) 0.51 (0.23) + VISITN (mean (SD)) 2.50 (1.12) 2.50 (1.12) 2.50 (1.12) + VISITN2 (mean (SD)) -0.02 (1.03) 0.01 (1.07) -0.04 (0.98) +``` + +## BCVA Data + +The BCVA dataset contains data from a randomized longitudinal ophthalmology trial evaluating the change in baseline corrected visual acuity (BCVA) over the course of 10 visits. BCVA corresponds to the number of letters read from a visual acuity chart. A summary of the data is given below: + +``` + Stratified by ARMCD + Overall CTL TRT + n 8605 4123 4482 + USUBJID (%) + PT[1-1000] 1000 494 (49.4) 506 (50.6) + AVISIT + VIS1 983 482 501 + VIS2 980 481 499 + VIS3 960 471 489 + VIS4 946 458 488 + VIS5 925 454 471 + VIS6 868 410 458 + VIS7 816 388 428 + VIS8 791 371 420 + VIS9 719 327 392 + VIS10 617 281 336 + RACE (%) + Asian 297 (29.7) 151 (30.6) 146 (28.9) + Black or African American 317 (31.7) 149 (30.1) 168 (33.2) + White 386 (38.6) 194 (39.3) 192 (37.9) + BCVA_BL (mean (SD)) 75.12 (9.93) 74.90 (9.76) 75.40 (10.1) + BCVA_CHG (mean (SD)) + VIS1 5.59 (1.31) 5.32 (1.23) 5.86 (1.33) + VIS10 9.18 (2.91) 7.49 (2.58) 10.60 (2.36) +``` + +# Model Implementations {.tabset} + +Listed below are some of the most commonly used covariance structures used when fitting MMRMs. We indicate which matrices are available "out of the box" for each implementation considered in this vignette. Note that this table is not exhaustive; `PROC GLIMMIX` and `glmmTMB` support additional spatial covariance structures. + +| Covariance structures | `mmrm` | `PROC GLIMMIX` | `gls` | `lmer` | `glmmTMB` | +|:---------------------------------:|:------:|:--------------:|:-----:|:------:|:---------:| +| Ante-dependence (heterogeneous) | X | X | | | | +| Ante-dependence (homogeneous) | X | | | | | +| Auto-regressive (heterogeneous) | X | X | X | | | +| Auto-regressive (homogeneous) | X | X | X | | X | +| Compound symmetry (heterogeneous) | X | X | X | | X | +| Compound symmetry (homogeneous) | X | X | X | | | +| Spatial exponential | X | X | X | | X | +| Toeplitz (heterogeneous) | X | X | | | X | +| Toeplitz (homogeneous) | X | X | | | | +| Unstructured | X | X | X | X | X | + +Code for fitting MMRMs to the FEV data using each of the considered functions and covariance structures are provided below. Fixed effects for the visit number, treatment assignment and the interaction between the two are modeled. + +## Ante-dependence (heterogeneous) + +### `PROC GLIMMIX` + +```{=html} +
PROC GLIMMIX DATA = fev_data;
+CLASS AVISIT(ref = 'VIS1') ARMCD(ref = 'PBO') USUBJID;
+MODEL FEV1 = AVISIT|ARMCD / ddfm=satterthwaite solution chisq;
+RANDOM AVISIT / subject=USUBJID type=ANTE(1);
+
+``` +### `mmrm` + +```{=html} +
mmrm(
+  formula = FEV1 ~ ARMCD * AVISIT + adh(VISITN | USUBJID),
+  data = fev_data
+)
+
+``` +## Ante-dependence (homogeneous) + +### `mmrm` + +```{=html} +
mmrm(
+  formula =FEV1 ~ ARMCD * AVISIT + ad(VISITN | USUBJID),
+  data = fev_data
+)
+
+``` +## Auto-regressive (heterogeneous) + +### `PROC GLIMMIX` + +```{=html} +
PROC GLIMMIX DATA = fev_data;
+CLASS AVISIT(ref = 'VIS1') ARMCD(ref = 'PBO') USUBJID;
+MODEL FEV1 = AVISIT|ARMCD / ddfm=satterthwaite solution chisq;
+RANDOM AVISIT / subject=USUBJID type=ARH(1);
+
+``` +### `mmrm` + +```{=html} +
mmrm(
+  formula = FEV1 ~ ARMCD * AVISIT + ar1h(VISITN | USUBJID),
+  data = fev_data
+)
+
+``` +### `gls` + +```{=html} +
gls(
+  formula = FEV1 ~ ARMCD * AVISIT,
+  data = fev_data,
+  correlation = corCAR1(form = ~AVISIT | USUBJID),
+  weights = varIdent(form = ~1|AVISIT),
+  na.action = na.omit
+)
+
+``` +## Auto-regressive (homogeneous) + +### `PROC GLIMMIX` + +```{=html} +
PROC GLIMMIX DATA = fev_data;
+CLASS AVISIT(ref = 'VIS1') ARMCD(ref = 'PBO') USUBJID;
+MODEL FEV1 =  ARMCD|AVISIT / ddfm=satterthwaite solution chisq;
+RANDOM AVISIT / subject=USUBJID type=AR(1);
+
+``` +### `mmrm` + +```{=html} +
mmrm(
+  formula = FEV1 ~ ARMCD * AVISIT + ar1(VISITN | USUBJID),
+  data = fev_data
+)
+
+``` +### `gls` + +```{=html} +
gls(
+  formula = FEV1 ~ ARMCD * AVISIT,
+  data = fev_data,
+  correlation = corCAR1(form = ~AVISIT | USUBJID),
+  na.action = na.omit
+)
+
+``` +### `glmmTMB` + +```{=html} +
glmmTMB(
+  FEV1 ~ ARMCD * AVISIT + ar1(0 + AVISIT | USUBJID),
+  dispformula = ~ 0,
+  data = fev_data
+)
+
+``` +## Compound symmetry (heterogeneous) + +### `PROC GLIMMIX` + +```{=html} +
PROC GLIMMIX DATA = fev_data;
+CLASS AVISIT(ref = 'VIS1') ARMCD(ref = 'PBO') USUBJID;
+MODEL FEV1 = AVISIT|ARMCD / ddfm=satterthwaite solution chisq;
+RANDOM AVISIT / subject=USUBJID type=CSH;
+
+``` +### `mmrm` + +```{=html} +
mmrm(
+  formula = FEV1 ~ ARMCD * AVISIT + csh(VISITN | USUBJID),
+  data = fev_data
+)
+
+``` +### `gls` + +```{=html} +
gls(
+  formula = FEV1 ~ ARMCD * AVISIT,
+  data = fev_data,
+  correlation = corCompSymm(form = ~AVISIT | USUBJID),
+  weights = varIdent(form = ~1|AVISIT),
+  na.action = na.omit
+)
+
+``` +### `glmmTMB` + +```{=html} +
glmmTMB(
+  FEV1 ~ ARMCD * AVISIT + cs(0 + AVISIT | USUBJID),
+  dispformula = ~ 0,
+  data = fev_data
+)
+
+``` +## Compound symmetry (homogeneous) + +### `PROC GLIMMIX` + +```{=html} +
PROC GLIMMIX DATA = fev_data;
+CLASS AVISIT(ref = 'VIS1') ARMCD(ref = 'PBO') USUBJID;
+MODEL FEV1 = AVISIT|ARMCD / ddfm=satterthwaite solution chisq;
+RANDOM AVISIT / subject=USUBJID type=CS;
+
+``` +### `mmrm` + +```{=html} +
mmrm(
+  formula = FEV1 ~ ARMCD * AVISIT + cs(VISITN | USUBJID),
+  data = fev_data
+)
+
+``` +### `gls` + +```{=html} +
gls(
+  formula = FEV1 ~ ARMCD * AVISIT,
+  data = fev_data,
+  correlation = corCompSymm(form = ~AVISIT | USUBJID),
+  na.action = na.omit
+)
+
+``` +## Spatial exponential + +### `PROC GLIMMIX` + +```{=html} +
PROC GLIMMIX DATA = fev_data;
+CLASS AVISIT(ref = 'VIS1') ARMCD(ref = 'PBO') USUBJID;
+MODEL FEV1 = AVISIT|ARMCD / ddfm=satterthwaite solution chisq;
+RANDOM / subject=USUBJID type=sp(exp)(visitn) rcorr;
+
+``` +### `mmrm` + +```{=html} +
mmrm(
+  formula = FEV1 ~ ARMCD * AVISIT + sp_exp(VISITN | USUBJID),
+  data = fev_data
+)
+
+``` +### `gls` + +```{=html} +
gls(
+  formula = FEV1 ~ ARMCD * AVISIT,
+  data = fev_data,
+  correlation = corExp(form = ~AVISIT | USUBJID),
+  weights = varIdent(form = ~1|AVISIT),
+  na.action = na.omit
+)
+
+``` +### `glmmTMB` + +```{=html} +
# NOTE: requires use of coordinates
+glmmTMB(
+  FEV1 ~ ARMCD * AVISIT + exp(0 + AVISIT | USUBJID),
+  dispformula = ~ 0,
+  data = fev_data
+)
+
+``` +## Toeplitz (heterogeneous) + +### `PROC GLIMMIX` + +```{=html} +
PROC GLIMMIX DATA = fev_data;
+CLASS AVISIT(ref = 'VIS1') ARMCD(ref = 'PBO') USUBJID;
+MODEL FEV1 = AVISIT|ARMCD / ddfm=satterthwaite solution chisq;
+RANDOM AVISIT / subject=USUBJID type=TOEPH;
+
+``` +### `mmrm` + +```{=html} +
mmrm(
+  formula = FEV1 ~ ARMCD * AVISIT + toeph(AVISIT | USUBJID),
+  data = fev_data
+)
+
+``` +### `glmmTMB` + +```{=html} +
 glmmTMB(
+  FEV1 ~ ARMCD * AVISIT + toep(0 + AVISIT | USUBJID),
+  dispformula = ~ 0,
+  data = fev_data
+)
+
+``` +## Toeplitz (homogeneous) + +### `PROC GLIMMIX` + +```{=html} +
PROC GLIMMIX DATA = fev_data;
+CLASS AVISIT(ref = 'VIS1') ARMCD(ref = 'PBO') USUBJID;
+MODEL FEV1 = AVISIT|ARMCD / ddfm=satterthwaite solution chisq;
+RANDOM AVISIT / subject=USUBJID type=TOEP;
+
+``` +### `mmrm` + +```{=html} +
mmrm(
+  formula = FEV1 ~ ARMCD * AVISIT + toep(AVISIT | USUBJID),
+  data = fev_data
+)
+
+``` +## Unstructured + +### `PROC GLIMMIX` + +```{=html} +
PROC GLIMMIX DATA = fev_data;
+CLASS AVISIT(ref = 'VIS1') ARMCD(ref = 'PBO') USUBJID;
+MODEL FEV1 = ARMCD|AVISIT / ddfm=satterthwaite solution chisq;
+RANDOM AVISIT / subject=USUBJID type=un;
+
+``` +### `mmrm` + +```{=html} +
mmrm(
+  formula = FEV1 ~ ARMCD * AVISIT + us(AVISIT | USUBJID),
+  data = fev_data
+)
+
+``` +### `gls` + +```{=html} +
gls(
+  formula = FEV1 ~  ARMCD * AVISIT,
+  data = fev_data,
+  correlation = corSymm(form = ~AVISIT | USUBJID),
+  weights = varIdent(form = ~1|AVISIT),
+  na.action = na.omit
+)
+
``` +### `lmer` -For the derived contrasts +```{=html} +
lmer(
+  FEV1 ~ ARMCD * AVISIT + (0 + AVISIT | USUBJID),
+  data = fev_data,
+  control = lmerControl(check.nobs.vs.nRE = "ignore"),
+  na.action = na.omit
+)
+
+``` +### `glmmTMB` + +```{=html} +
glmmTMB(
+  FEV1 ~ ARMCD * AVISIT + us(0 + AVISIT | USUBJID),
+  dispformula = ~ 0,
+  data = fev_data
+)
+
+``` +# Benchmarking + +Next, the MMRM fitting procedures are compared using the FEV and BCVA datasets. FEV1 measurements are modeled as a function of race, treatment arm, visit number, and the interaction between the treatment arm and the visit number. Change in BCVA is assumed to be a function of race, baseline BCVA, treatment arm, visit number, and the treatment--visit interaction. In both datasets, repeated measures are modeled using an unstructured covariance matrix. The implementations' convergence times are evaluated first, followed by a comparison of their estimates. Finally, we fit these procedures on simulated BCVA-like data to assess the impact of missingness on convergence rates. + +## Convergence Times + +### FEV Data + +The `mmrm`, `PROC GLIMMIX`, `gls`, `lmer`, and `glmmTMB` functions are applied to the FEV dataset 10 times. The convergence times are recorded for each replicate and are reported in the table below. ```{r} -#| echo: false -#| warning: false -#| message: false -plots$contrasts_bland_alt_estimate$`Unstructured (KR)` -plots$contrasts_bland_alt_estimate$`Unstructured (satterthwaite)` -plots$contrasts_bland_alt_estimate$`Heterogeneous Compound Symmetry` -plots$contrasts_bland_alt_estimate$`Heterogeneous First Order Autoregressive` +#| message: FALSE +#| warning: FALSE +#| echo: FALSE + +# format table in markdown +cached_mmrm_results$conv_time_fev |> + arrange(median) |> + transmute( + Implementation = expression, + Median = median, + `First Quartile` = lower, + `Third Quartile` = upper + ) |> + knitr::kable( + caption = "Comparison of convergence times: milliseconds", digits = 2 + ) ``` -and corresponding 95%CI widths +It is clear from these results that `mmrm` converges significantly faster than other R functions. Though not demonstrated here, this is generally true regardless of the sample size and covariance structure used. `mmrm` is faster than `PROC GLIMMIX`. + +### BCVA Data + +The MMRM implementations are now applied to the BCVA dataset 10 times. The convergence times are presented below. ```{r} -#| echo: false -#| warning: false -#| message: false -plots$contrasts_bland_alt_ci$`Unstructured (KR)` -plots$contrasts_bland_alt_ci$`Unstructured (satterthwaite)` -plots$contrasts_bland_alt_ci$`Heterogeneous Compound Symmetry` -plots$contrasts_bland_alt_ci$`Heterogeneous First Order Autoregressive` +#| message: FALSE +#| warning: FALSE +#| echo: FALSE +# format table in markdown +cached_mmrm_results$conv_time_bcva |> + arrange(median) |> + transmute( + Implementation = expression, + Median = median, + `First Quartile` = lower, + `Third Quartile` = upper + ) |> + knitr::kable( + caption = "Comparison of convergence times: seconds", digits = 2 + ) ``` -### Analysis of SAS and R Comparison +We again find that `mmrm` produces the fastest convergence times on average. + +## Marginal Treatment Effect Estimates Comparison + +We next estimate the marginal mean treatment effects for each visit in the FEV and BCVA datasets using the MMRM fitting procedures. All R implementations' estimates are reported relative to `PROC GLIMMIX`'s estimates. Convergence status is also reported. + +### FEV Data + +```{r review-treatment-fev} +#| message: FALSE +#| warning: FALSE +#| echo: FALSE +# plot estimates +ggplot( + cached_mmrm_results$rel_diff_ests_tbl_fev, + aes(x = parameter, y = rel_diff, color = estimator, shape = converged) +) + + geom_point(position = position_dodge(width = 0.5)) + + geom_hline(yintercept = 0, linetype = 2, alpha = 0.5) + + scale_color_discrete(name = "Procedure") + + scale_shape_discrete(name = "Convergence") + + ylab("Relative Difference") + + xlab("Marginal Treatment Effect") + + ggtitle("Average Treatment Effect Estimates Relative to SAS Estimates") + + theme_classic() +``` + +The R procedures' estimates are very similar to those output by `PROC GLIMMIX`, though `mmrm` and `gls` generate the estimates that are closest to those produced when using SAS. All methods converge using their default optimization arguments. + +### BCVA Data + +```{r review-treatment-bcva} +#| message: FALSE +#| warning: FALSE +#| echo: FALSE +# plot estimates +ggplot( + cached_mmrm_results$rel_diff_ests_tbl_bcva, + aes(x = parameter, y = rel_diff, color = estimator, shape = converged) +) + + geom_point(position = position_dodge(width = 0.5)) + + geom_hline(yintercept = 0, linetype = 2, alpha = 0.5) + + scale_color_discrete(name = "Procedure") + + scale_shape_discrete(name = "Convergence") + + ylab("Relative Difference") + + xlab("Marginal Treatment Effect") + + ggtitle("Average Treatment Effect Estimates Relative to SAS Estimates") + + theme_classic() + +# excluding glmmTMB +cached_mmrm_results$rel_diff_ests_tbl_bcva |> + dplyr::filter(estimator != "glmmTMB") |> + ggplot( + aes(x = parameter, y = rel_diff, color = estimator, shape = converged) + ) + + geom_point(position = position_dodge(width = 0.5)) + + geom_hline(yintercept = 0, linetype = 2, alpha = 0.5) + + scale_color_discrete(name = "Procedure") + + scale_shape_discrete(name = "Convergence") + + ylab("Relative Difference") + + xlab("Marginal Treatment Effect") + + ggtitle( + "Average Treatment Effect Estimates Relative to SAS Estimates + (Excluding glmmTMB)" + ) + + theme_classic() +``` + +`mmrm`, `gls` and `lmer` produce estimates that are virtually identical to `PROC GLIMMIX`'s, while `glmmTMB` does not. This is likely explained by `glmmTMB`'s failure to converge. Note too that `lmer` fails to converge. + +## Impact of Missing Data on Convergence Rates + +The results of the previous benchmark suggest that the amount of patients missing from later time points affect certain implementations' capacity to converge. We investigate this further by simulating data using a data-generating process similar to that of the BCVA datasets, though with various rates of patient dropout. + +Ten datasets of 200 patients are generated each of the following levels of missingness: none, mild, moderate, and high. In all scenarios, observations are missing at random. The number patients observed at each visit is obtained for one replicated dataset at each level of missingness is presented in the table below. + +```{r review-missingness-table, warning=FALSE, message=FALSE, echo=FALSE} +## construct the table +cached_mmrm_results$df_missingness |> + kable(caption = "Number of patients per visit") +``` + +The convergence rates of all implementations for stratified by missingness level is presented in the plot below. + +```{r review-convergence-rate-missingness} +#| message: FALSE +#| warning: FALSE +#| echo: FALSE +## plot the convergence rates +cached_mmrm_results$conv_rate |> + mutate( + missingness = factor( + missingness, + levels = c("none", "mild", "moderate", "high") + ) + ) |> + ggplot(aes(x = method, y = convergence_rate)) + + geom_point() + + facet_grid(rows = vars(missingness)) + + xlab("Method") + + ylab("Convergence Rate (10 Replicates)") + + ggtitle("Convergence Rates by Missingness Levels") + + scale_y_continuous(labels = scales::percent_format(accuracy = 1)) + + theme_bw() +``` -Using SAS PROC MIXED and R functions such as gls, lmer, mod_grid, and mod_emm, results were broadly aligned. Results not being exact can be attributed to many factors such as rounding precision, data handling, and many other internal processing nuances. However, Bland-Altman type plots showed small but randomly distributed differences across a broad range of parameters from the input data. Apart from a small subset of the parameters, there were no trends observed which would have suggested systemic differences between the languages. These analyses were based on a single set of data so more research must be done. However, based on comparing R documentation with SAS documentation, as well as the results displayed above in this paper, it is evident that the R and the SAS methods cover do produce similarly valid results for the options which were tested. +`mmrm`, `gls`, and `PROC GLIMMIX` are resilient to missingness, only exhibiting some convergence problems in the scenarios with the most missingness. These implementations converged in all the other scenarios' replicates. `glmmTMB`, on the other hand, has convergence issues in the no-, mild-, and high-missingness datasets, with the worst convergence rate occurring in the datasets with the most dropout. Finally, `lmer` is unreliable in all scenarios, suggesting that it's convergence issues stem from something other than the missing observations. -### Future work +Note that the default optimization schemes are used for each method; these schemes can be modified to potentially improve convergence rates. -- Run SAS code by also removing assessments at `avisitn=0` from the response variable, and using `trtp` (or `trtpn`) and `avisit` (or `avisitn`) -- Investigating the differences -- Implement `lmer` equivalent to MMRM with compound symmetry -- Comparisons for other models, i.e. only random, random and repeated, no repeated +A more comprehensive simulation study using data-generating processes similar to the one used here is outlined in the [`simulations/missing-data-benchmarks`](https://github.com/openpharma/mmrm/tree/main/simulations/missing-data-benchmarks) subdirectory. In addition to assessing the effect of missing data on software convergence rates, we also evaluate these methods' fit times and empirical bias, variance, 95% coverage rates, type I error rates and type II error rates. `mmrm` is found to be the most most robust software for fitting MMRMs in scenarios where a large proportion of patients are missing from the last time points. Additionally, `mmrm` has the fastest average fit times regardless of the amount of missingness. All implementations considered produce similar empirical biases, variances, 95% coverage rates, type I error rates and type II error rates. diff --git a/R/anova_cache/html/__packages b/R/anova_cache/html/__packages deleted file mode 100644 index e9619db91..000000000 --- a/R/anova_cache/html/__packages +++ /dev/null @@ -1,12 +0,0 @@ -tidyverse -ggplot2 -tibble -tidyr -readr -purrr -dplyr -stringr -forcats -emmeans -broom -knitr diff --git a/R/anova_cache/html/unnamed-chunk-2_a959c7d69d77a7d483cef0c7910ca827.RData b/R/anova_cache/html/unnamed-chunk-2_a959c7d69d77a7d483cef0c7910ca827.RData deleted file mode 100644 index 974e1cf99..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-2_a959c7d69d77a7d483cef0c7910ca827.RData and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-2_a959c7d69d77a7d483cef0c7910ca827.rdb b/R/anova_cache/html/unnamed-chunk-2_a959c7d69d77a7d483cef0c7910ca827.rdb deleted file mode 100644 index f8496e225..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-2_a959c7d69d77a7d483cef0c7910ca827.rdb and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-2_a959c7d69d77a7d483cef0c7910ca827.rdx b/R/anova_cache/html/unnamed-chunk-2_a959c7d69d77a7d483cef0c7910ca827.rdx deleted file mode 100644 index 40aa6461a..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-2_a959c7d69d77a7d483cef0c7910ca827.rdx and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-3_03313b7a8524dd7a7440d66f44058036.RData b/R/anova_cache/html/unnamed-chunk-3_03313b7a8524dd7a7440d66f44058036.RData deleted file mode 100644 index 9fadf3af5..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-3_03313b7a8524dd7a7440d66f44058036.RData and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-3_03313b7a8524dd7a7440d66f44058036.rdb b/R/anova_cache/html/unnamed-chunk-3_03313b7a8524dd7a7440d66f44058036.rdb deleted file mode 100644 index e69de29bb..000000000 diff --git a/R/anova_cache/html/unnamed-chunk-3_03313b7a8524dd7a7440d66f44058036.rdx b/R/anova_cache/html/unnamed-chunk-3_03313b7a8524dd7a7440d66f44058036.rdx deleted file mode 100644 index c28f3f9db..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-3_03313b7a8524dd7a7440d66f44058036.rdx and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-4_2c39991c34fb054f157959a554b2175c.RData b/R/anova_cache/html/unnamed-chunk-4_2c39991c34fb054f157959a554b2175c.RData deleted file mode 100644 index 110f4a38d..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-4_2c39991c34fb054f157959a554b2175c.RData and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-4_2c39991c34fb054f157959a554b2175c.rdb b/R/anova_cache/html/unnamed-chunk-4_2c39991c34fb054f157959a554b2175c.rdb deleted file mode 100644 index e69de29bb..000000000 diff --git a/R/anova_cache/html/unnamed-chunk-4_2c39991c34fb054f157959a554b2175c.rdx b/R/anova_cache/html/unnamed-chunk-4_2c39991c34fb054f157959a554b2175c.rdx deleted file mode 100644 index c28f3f9db..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-4_2c39991c34fb054f157959a554b2175c.rdx and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-5_1e3227cddc2c5d4b92f6b3238e69038a.RData b/R/anova_cache/html/unnamed-chunk-5_1e3227cddc2c5d4b92f6b3238e69038a.RData deleted file mode 100644 index 98511cc8a..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-5_1e3227cddc2c5d4b92f6b3238e69038a.RData and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-5_1e3227cddc2c5d4b92f6b3238e69038a.rdb b/R/anova_cache/html/unnamed-chunk-5_1e3227cddc2c5d4b92f6b3238e69038a.rdb deleted file mode 100644 index e69de29bb..000000000 diff --git a/R/anova_cache/html/unnamed-chunk-5_1e3227cddc2c5d4b92f6b3238e69038a.rdx b/R/anova_cache/html/unnamed-chunk-5_1e3227cddc2c5d4b92f6b3238e69038a.rdx deleted file mode 100644 index c28f3f9db..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-5_1e3227cddc2c5d4b92f6b3238e69038a.rdx and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-6_87216dbd4f33263d00c668206b754202.RData b/R/anova_cache/html/unnamed-chunk-6_87216dbd4f33263d00c668206b754202.RData deleted file mode 100644 index 810700feb..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-6_87216dbd4f33263d00c668206b754202.RData and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-6_87216dbd4f33263d00c668206b754202.rdb b/R/anova_cache/html/unnamed-chunk-6_87216dbd4f33263d00c668206b754202.rdb deleted file mode 100644 index e69de29bb..000000000 diff --git a/R/anova_cache/html/unnamed-chunk-6_87216dbd4f33263d00c668206b754202.rdx b/R/anova_cache/html/unnamed-chunk-6_87216dbd4f33263d00c668206b754202.rdx deleted file mode 100644 index c28f3f9db..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-6_87216dbd4f33263d00c668206b754202.rdx and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-7_a6ade8d6f53133a4cb343d600ba67101.RData b/R/anova_cache/html/unnamed-chunk-7_a6ade8d6f53133a4cb343d600ba67101.RData deleted file mode 100644 index fc65344d6..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-7_a6ade8d6f53133a4cb343d600ba67101.RData and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-7_a6ade8d6f53133a4cb343d600ba67101.rdb b/R/anova_cache/html/unnamed-chunk-7_a6ade8d6f53133a4cb343d600ba67101.rdb deleted file mode 100644 index e69de29bb..000000000 diff --git a/R/anova_cache/html/unnamed-chunk-7_a6ade8d6f53133a4cb343d600ba67101.rdx b/R/anova_cache/html/unnamed-chunk-7_a6ade8d6f53133a4cb343d600ba67101.rdx deleted file mode 100644 index c28f3f9db..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-7_a6ade8d6f53133a4cb343d600ba67101.rdx and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-8_fa270a2f00df15b12e7fd336f04352a4.RData b/R/anova_cache/html/unnamed-chunk-8_fa270a2f00df15b12e7fd336f04352a4.RData deleted file mode 100644 index e00452f52..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-8_fa270a2f00df15b12e7fd336f04352a4.RData and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-8_fa270a2f00df15b12e7fd336f04352a4.rdb b/R/anova_cache/html/unnamed-chunk-8_fa270a2f00df15b12e7fd336f04352a4.rdb deleted file mode 100644 index e69de29bb..000000000 diff --git a/R/anova_cache/html/unnamed-chunk-8_fa270a2f00df15b12e7fd336f04352a4.rdx b/R/anova_cache/html/unnamed-chunk-8_fa270a2f00df15b12e7fd336f04352a4.rdx deleted file mode 100644 index c28f3f9db..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-8_fa270a2f00df15b12e7fd336f04352a4.rdx and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-9_172a7fa3eda2baf75813b4ecc2a279ce.RData b/R/anova_cache/html/unnamed-chunk-9_172a7fa3eda2baf75813b4ecc2a279ce.RData deleted file mode 100644 index fca6206c6..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-9_172a7fa3eda2baf75813b4ecc2a279ce.RData and /dev/null differ diff --git a/R/anova_cache/html/unnamed-chunk-9_172a7fa3eda2baf75813b4ecc2a279ce.rdb b/R/anova_cache/html/unnamed-chunk-9_172a7fa3eda2baf75813b4ecc2a279ce.rdb deleted file mode 100644 index e69de29bb..000000000 diff --git a/R/anova_cache/html/unnamed-chunk-9_172a7fa3eda2baf75813b4ecc2a279ce.rdx b/R/anova_cache/html/unnamed-chunk-9_172a7fa3eda2baf75813b4ecc2a279ce.rdx deleted file mode 100644 index c28f3f9db..000000000 Binary files a/R/anova_cache/html/unnamed-chunk-9_172a7fa3eda2baf75813b4ecc2a279ce.rdx and /dev/null differ diff --git a/R/nonpara_wilcoxon_ranksum.qmd b/R/nonpara_wilcoxon_ranksum.qmd new file mode 100644 index 000000000..1f5c92279 --- /dev/null +++ b/R/nonpara_wilcoxon_ranksum.qmd @@ -0,0 +1,79 @@ +--- +title: "Wilcoxon Rank Sum (Mann Whitney-U) in R" +--- + +```{r} +#| echo: FALSE +#| include: FALSE + library(tidyverse) +``` + +# Overview + + Wilcoxon rank sum test, or equivalently, Mann-Whitney U-test is a rank based non-paramatric method. The aim is to examine the differences between two groups. To be more specific, it tests whether the median difference between pairs is equal to zero. + + It is the non-parametric equivalent to two-sample t-test, where the two groups are not paired. + +## Available R package + + The `stats` package implements various classic statistical tests, including wilcoxon rank sum test. + +```{r} +#| eval: false +#| echo: true +# x, y are two unpaired vectors. Do not necessary need to be of the same length. +stats::wilcox.test(x, y, paired = F) +``` + + +## Example: Birth Weight + +*Data source: Table 30.4, Kirkwood BR. and Sterne JAC. Essentials of medical statistics. Second Edition. ISBN 978-0-86542-871-3* + +Comparison of birth weights (kg) of children born to 15 non-smokers with those of children born to 14 heavy smokers. + +```{r} +#| eval: true +#| echo: true +# bw_ns: non smokers +# bw_s: smokers +bw_ns <- c(3.99, 3.89, 3.6, 3.73, 3.31, + 3.7, 4.08, 3.61, 3.83, 3.41, + 4.13, 3.36, 3.54, 3.51, 2.71) +bw_s <- c(3.18, 2.74, 2.9, 3.27, 3.65, + 3.42, 3.23, 2.86, 3.6, 3.65, + 3.69, 3.53, 2.38, 2.34) +``` + + Can visualize the data on two histograms. Red lines indicate the location of medians. + +```{r} +#| eval: true +#| echo: true +par(mfrow =c(1,2)) +hist(bw_ns, main = 'Birthweight: non-smokers') +abline(v = median(bw_ns), col = 'red', lwd = 2) +hist(bw_s, main = 'Birthweight: smokers') +abline(v = median(bw_s), col = 'red', lwd = 2) +``` + + +It is possible to see that for non-smokers, the median birthweight is higher than those of smokers. Now we can formally test it with wilcoxon rank sum test. + +The default test is two-sided with confidence level of 0.95, and does continuity correction. + +```{r} +#| eval: true +#| echo: true +# default is two sided +stats::wilcox.test(bw_ns, bw_s, paired = F) +``` + We can also carry out a one-sided test, by specifying `alternative = greater` (if the first item is greater than the second). + +```{r} +#| eval: true +#| echo: true +# default is two sided +stats::wilcox.test(bw_ns, bw_s, paired = F, alternative = 'greater') +``` + diff --git a/data/cached_mmrm_results.rda b/data/cached_mmrm_results.rda new file mode 100644 index 000000000..ff3fa9eb7 Binary files /dev/null and b/data/cached_mmrm_results.rda differ diff --git a/data/stat_method_tbl.csv b/data/stat_method_tbl.csv index 296a8b1e5..325f50dec 100644 --- a/data/stat_method_tbl.csv +++ b/data/stat_method_tbl.csv @@ -11,7 +11,7 @@ Generalized Linear Models,Logistic Regression,[R](R/logistic_regr),, Generalized Linear Models,Poisson/Negative Binomial Regression,,, Generalized Linear Models,Categorical Repeated Measures,,, Generalized Linear Models,Categorical Multiple Imputation,,, -Non-parametric Analysis,Wilcoxon signed rank,,, +Non-parametric Analysis,Wilcoxon signed rank,[R](R/nonpara_wilcoxon_ranksum),, Non-parametric Analysis,Mann-Whitney U/Wilcoxon rank sum,,, Non-parametric Analysis,Kolmogorov-Smirnov test,,, Non-parametric Analysis,Kruskall-Wallis test,[R](R/kruskal_wallis),[SAS](SAS/kruskal_wallis),[R vs SAS](Comp/r-sas_kruskalwallis) @@ -19,7 +19,7 @@ Non-parametric Analysis,Friedman test,,, Non-parametric Analysis,Jonckheere test,,, Categorical Data Analysis,Binomial test,,, Categorical Data Analysis,McNemar's test,[R](R/mcnemar),[SAS](SAS/mcnemar),[R vs SAS](Comp/r-sas_mcnemar) -Categorical Data Analysis,Chi-Square Association/Fishers exact,[R](R/association),, +Categorical Data Analysis,Chi-Square Association/Fishers exact,[R](R/association),,[R vs SAS](Comp/r-sas_chi-sq) Categorical Data Analysis,Cochran Mantel Haenszel,[R](R/cmh),[SAS](SAS/cmh),[R vs SAS](Comp/r-sas_cmh) Categorical Data Analysis,Confidence Intervals for proportions,,, Linear Mixed Models,MMRM,[R](R/mmrm),[SAS](SAS/mmrm),[R vs SAS](Comp/r-sas_mmrm) @@ -44,4 +44,4 @@ Multivariate methods,Canonical correlation,,, Multivariate methods,PLS,,, Other Methods,Nearest neighbour,,, Other Methods,Causal inference,,, -Other Methods,Machine learning,,, +Other Methods,Machine learning,,, \ No newline at end of file diff --git a/images/mmrm/review-convergence-rate-missingness-1.png b/images/mmrm/review-convergence-rate-missingness-1.png new file mode 100644 index 000000000..7c6942e10 Binary files /dev/null and b/images/mmrm/review-convergence-rate-missingness-1.png differ diff --git a/images/mmrm/review-treatment-bcva-1.png b/images/mmrm/review-treatment-bcva-1.png new file mode 100644 index 000000000..52c06b3f0 Binary files /dev/null and b/images/mmrm/review-treatment-bcva-1.png differ diff --git a/images/mmrm/review-treatment-bcva-2.png b/images/mmrm/review-treatment-bcva-2.png new file mode 100644 index 000000000..eed43a3fc Binary files /dev/null and b/images/mmrm/review-treatment-bcva-2.png differ diff --git a/images/mmrm/review-treatment-fev-1.png b/images/mmrm/review-treatment-fev-1.png new file mode 100644 index 000000000..249a8f0f3 Binary files /dev/null and b/images/mmrm/review-treatment-fev-1.png differ diff --git a/renv.lock b/renv.lock index 17681d4de..d8211048f 100644 --- a/renv.lock +++ b/renv.lock @@ -8,7 +8,29 @@ } ] }, + "Bioconductor": { + "Version": "3.16" + }, "Packages": { + "BiocManager": { + "Package": "BiocManager", + "Version": "1.30.22", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "utils" + ], + "Hash": "d57e43105a1aa9cb54fdb4629725acb1" + }, + "BiocVersion": { + "Package": "BiocVersion", + "Version": "3.16.0", + "Source": "Bioconductor", + "Requirements": [ + "R" + ], + "Hash": "44c5824508b9a10e52dbb505c34fa880" + }, "DBI": { "Package": "DBI", "Version": "1.1.3", @@ -127,6 +149,20 @@ ], "Hash": "1e035db628cefb315c571202d70202fe" }, + "Rdpack": { + "Package": "Rdpack", + "Version": "2.5", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R", + "methods", + "rbibutils", + "tools", + "utils" + ], + "Hash": "2a4dbc2502d3fd9f1c5d24b14a223583" + }, "SparseM": { "Package": "SparseM", "Version": "1.81", @@ -141,6 +177,22 @@ ], "Hash": "2042cd9759cc89a453c4aefef0ce9aae" }, + "TMB": { + "Package": "TMB", + "Version": "1.9.6", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "Matrix", + "R", + "RcppEigen", + "graphics", + "methods", + "stats", + "utils" + ], + "Hash": "b062cb79db56803311ea22b90ed6f57a" + }, "V8": { "Package": "V8", "Version": "4.2.2", @@ -398,6 +450,18 @@ ], "Hash": "f61dbaec772ccd2e17705c1e872e9e7c" }, + "checkmate": { + "Package": "checkmate", + "Version": "2.3.0", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R", + "backports", + "utils" + ], + "Hash": "ed4275b13c6ab74b89a31def0b6bf835" + }, "cli": { "Package": "cli", "Version": "3.6.0", @@ -546,6 +610,28 @@ ], "Hash": "6b9602c7ebbe87101a9c8edb6e8b6d21" }, + "details": { + "Package": "details", + "Version": "0.3.0", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R", + "clipr", + "desc", + "grid", + "htmltools", + "httr", + "knitr", + "magrittr", + "png", + "stats", + "utils", + "withr", + "xml2" + ], + "Hash": "64c0eb2b740ab1ac553d928b3a75d72a" + }, "diffobj": { "Package": "diffobj", "Version": "0.3.5", @@ -574,7 +660,7 @@ }, "dplyr": { "Package": "dplyr", - "Version": "1.1.2", + "Version": "1.1.3", "Source": "Repository", "Repository": "CRAN", "Requirements": [ @@ -593,7 +679,7 @@ "utils", "vctrs" ], - "Hash": "dea6970ff715ca541c387de363ff405e" + "Hash": "e85ffbebaad5f70e1a2e2ef4302b4949" }, "dtplyr": { "Package": "dtplyr", @@ -1426,6 +1512,17 @@ ], "Hash": "c6b2fdb18cf68ab613bd564363e1ba0d" }, + "microbenchmark": { + "Package": "microbenchmark", + "Version": "1.4.10", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "graphics", + "stats" + ], + "Hash": "db81b552e393ed092872cf7023469bc2" + }, "mime": { "Package": "mime", "Version": "0.12", @@ -1446,6 +1543,30 @@ ], "Hash": "587ce77fd3c7bada7eadb2d18b62930d" }, + "mmrm": { + "Package": "mmrm", + "Version": "0.2.2", + "Source": "Bioconductor", + "Repository": "CRAN", + "Requirements": [ + "R", + "Rcpp", + "RcppEigen", + "Rdpack", + "TMB", + "checkmate", + "lifecycle", + "methods", + "nlme", + "numDeriv", + "parallel", + "stats", + "stringr", + "testthat", + "utils" + ], + "Hash": "cb4cddcde5482069a9afdcb4e246b5c3" + }, "modelr": { "Package": "modelr", "Version": "0.1.10", @@ -1745,6 +1866,18 @@ ], "Hash": "5e3c5dc0b071b21fa128676560dbe94d" }, + "rbibutils": { + "Package": "rbibutils", + "Version": "2.2.16", + "Source": "Repository", + "Repository": "CRAN", + "Requirements": [ + "R", + "tools", + "utils" + ], + "Hash": "8c06968e0a5b0209c5f34239b1302336" + }, "reactR": { "Package": "reactR", "Version": "0.4.4", diff --git a/renv/activate.R b/renv/activate.R index 38d74598d..cb5401f93 100644 --- a/renv/activate.R +++ b/renv/activate.R @@ -8,6 +8,21 @@ local({ # the project directory project <- getwd() + # use start-up diagnostics if enabled + diagnostics <- Sys.getenv("RENV_STARTUP_DIAGNOSTICS", unset = "FALSE") + if (diagnostics) { + start <- Sys.time() + profile <- tempfile("renv-startup-", fileext = ".Rprof") + utils::Rprof(profile) + on.exit({ + utils::Rprof(NULL) + elapsed <- signif(difftime(Sys.time(), start, units = "auto"), digits = 2L) + writeLines(sprintf("- renv took %s to run the autoloader.", format(elapsed))) + writeLines(sprintf("- Profile: %s", profile)) + print(utils::summaryRprof(profile)) + }, add = TRUE) + } + # figure out whether the autoloader is enabled enabled <- local({ @@ -504,7 +519,7 @@ local({ # open the bundle for reading # We use gzcon for everything because (from ?gzcon) - # > Reading from a connection which does not supply a ‘gzip’ magic + # > Reading from a connection which does not supply a 'gzip' magic # > header is equivalent to reading from the original connection conn <- gzcon(file(bundle, open = "rb", raw = TRUE)) on.exit(close(conn)) @@ -767,10 +782,12 @@ local({ renv_bootstrap_validate_version <- function(version, description = NULL) { # resolve description file - description <- description %||% { - path <- getNamespaceInfo("renv", "path") - packageDescription("renv", lib.loc = dirname(path)) - } + # + # avoid passing lib.loc to `packageDescription()` below, since R will + # use the loaded version of the package by default anyhow. note that + # this function should only be called after 'renv' is loaded + # https://github.com/rstudio/renv/issues/1625 + description <- description %||% packageDescription("renv") # check whether requested version 'version' matches loaded version of renv sha <- attr(version, "sha", exact = TRUE) @@ -841,7 +858,7 @@ local({ hooks <- getHook("renv::autoload") for (hook in hooks) if (is.function(hook)) - tryCatch(hook(), error = warning) + tryCatch(hook(), error = warnify) # load the project renv::load(project) @@ -982,10 +999,15 @@ local({ } - renv_bootstrap_version_friendly <- function(version, sha = NULL) { + renv_bootstrap_version_friendly <- function(version, shafmt = NULL, sha = NULL) { sha <- sha %||% attr(version, "sha", exact = TRUE) - parts <- c(version, sprintf("[sha: %s]", substring(sha, 1L, 7L))) - paste(parts, collapse = " ") + parts <- c(version, sprintf(shafmt %||% " [sha: %s]", substring(sha, 1L, 7L))) + paste(parts, collapse = "") + } + + renv_bootstrap_exec <- function(project, libpath, version) { + if (!renv_bootstrap_load(project, libpath, version)) + renv_bootstrap_run(version, libpath) } renv_bootstrap_run <- function(version, libpath) { @@ -1012,11 +1034,6 @@ local({ } - - renv_bootstrap_in_rstudio <- function() { - commandArgs()[[1]] == "RStudio" - } - renv_json_read <- function(file = NULL, text = NULL) { jlerr <- NULL @@ -1155,26 +1172,8 @@ local({ # construct full libpath libpath <- file.path(root, prefix) - # attempt to load - if (renv_bootstrap_load(project, libpath, version)) - return(TRUE) - - if (renv_bootstrap_in_rstudio()) { - setHook("rstudio.sessionInit", function(...) { - renv_bootstrap_run(version, libpath) - - # Work around buglet in RStudio if hook uses readline - tryCatch( - { - tools <- as.environment("tools:rstudio") - tools$.rs.api.sendToConsole("", echo = FALSE, focus = FALSE) - }, - error = function(cnd) {} - ) - }) - } else { - renv_bootstrap_run(version, libpath) - } + # run bootstrap code + renv_bootstrap_exec(project, libpath, version) invisible()