Error(s) in re-building vignettes:
  ...
--- re-building ‘algorithm.Rmd’ using rmarkdown
--- finished re-building ‘algorithm.Rmd’

--- re-building ‘between_within.Rmd’ using rmarkdown
--- finished re-building ‘between_within.Rmd’

--- re-building ‘coef_vcov.Rmd’ using rmarkdown
--- finished re-building ‘coef_vcov.Rmd’

--- re-building ‘covariance.Rmd’ using rmarkdown
--- finished re-building ‘covariance.Rmd’

--- re-building ‘empirical_wls.Rmd’ using rmarkdown
--- finished re-building ‘empirical_wls.Rmd’

--- re-building ‘hypothesis_testing.Rmd’ using rmarkdown
--- finished re-building ‘hypothesis_testing.Rmd’

--- re-building ‘introduction.Rmd’ using rmarkdown


This package supports estimation of one- and multi-dimensional contrasts
(t-test and F-test calculation) with the [`df_1d()`](https://openpharma.github.io/mmrm/main/reference/df_1d.html) and
[`df_md()`](https://openpharma.github.io/mmrm/main/reference/df_md.html) functions.
Both functions utilize the chosen adjustment method from the initial `mmrm` call
for the calculation of degrees of freedom and (for Kenward-Roger methods) the
variance estimates for the test-statistics.

## One-dimensional contrasts

Compute the test of a one-dimensional (vector) contrast for a `mmrm` object with
Satterthwaite degrees of freedom.


``` r
fit <- mmrm(
  formula = FEV1 ~ RACE + SEX + ARMCD * AVISIT + us(AVISIT | USUBJID),
  data = fev_data
)

contrast <- numeric(length(component(fit, "beta_est")))
contrast[3] <- 1

df_1d(fit, contrast)
#> $est
#> [1] 5.643565
#> 
#> $se
#> [1] 0.6656093
#> 
#> $df
#> [1] 157.1382
#> 
#> $t_stat
#> [1] 8.478795
#> 
#> $p_val
#> [1] 1.564869e-14
```

This works similarly when choosing a Kenward-Roger adjustment:


``` r
fit_kr <- mmrm(
  formula = FEV1 ~ RACE + SEX + ARMCD * AVISIT + us(AVISIT | USUBJID),
  data = fev_data,
  method = "Kenward-Roger"
)

df_1d(fit_kr, contrast)
#> $est
#> [1] 5.643565
#> 
#> $se
#> [1] 0.6740941
#> 
#> $df
#> [1] 157.1382
#> 
#> $t_stat
#> [1] 8.372073
#> 
#> $p_val
#> [1] 2.931654e-14
```

We see that because this is a one-dimensional contrast, the degrees of freedoms
are identical for Satterthwaite and Kenward-Roger. However, the standard errors
are different and therefore the p-values are different.

Additional options for the degrees of freedom `method` are Residual and Between-Within.

## Multi-dimensional contrasts

Compute the test of a multi-dimensional (matrix) contrast for the above defined
`mmrm` object with Satterthwaite degrees of freedom:


``` r
contrast <- matrix(data = 0, nrow = 2, ncol = length(component(fit, "beta_est")))
contrast[1, 2] <- contrast[2, 3] <- 1

df_md(fit, contrast)
#> $num_df
#> [1] 2
#> 
#> $denom_df
#> [1] 165.5553
#> 
#> $f_stat
#> [1] 36.91143
#> 
#> $p_val
#> [1] 5.544575e-14
```

And for the Kenward-Roger adjustment:


``` r
df_md(fit_kr, contrast)
#> $num_df
#> [1] 2
#> 
#> $denom_df
#> [1] 165.5728
#> 
#> $f_stat
#> [1] 35.99422
#> 
#> $p_val
#> [1] 1.04762e-13
```

We see that for the multi-dimensional contrast we get slightly different
denominator degrees of freedom for the two adjustment methods.

Also the simpler Residual and Between-Within `method` choices can be used of
course together with multidimensional contrasts.

## Support for emmeans

This package includes methods that allow `mmrm` objects to be used with the
`emmeans` package. `emmeans` computes estimated marginal means (also called
least-square means) for the coefficients of the MMRM. For example, in order
to see the least-square means by visit and by treatment arm:


``` r
library(emmeans)
#> mmrm() registered as emmeans extension
#> Welcome to emmeans.
#> Caution: You lose important information if you filter this package's results.
#> See '? untidy'
lsmeans_by_visit <- emmeans(fit, ~ ARMCD | AVISIT)
lsmeans_by_visit
#> AVISIT = VIS1:
#>  ARMCD emmean    SE  df lower.CL upper.CL
#>  PBO     33.3 0.755 148     31.8     34.8
#>  TRT     37.1 0.763 143     35.6     38.6
#> 
#> AVISIT = VIS2:
#>  ARMCD emmean    SE  df lower.CL upper.CL
#>  PBO     38.2 0.612 147     37.0     39.4
#>  TRT     41.9 0.602 143     40.7     43.1
#> 
#> AVISIT = VIS3:
#>  ARMCD emmean    SE  df lower.CL upper.CL
#>  PBO     43.7 0.462 130     42.8     44.6
#>  TRT     46.8 0.509 130     45.7     47.8
#> 
#> AVISIT = VIS4:
#>  ARMCD emmean    SE  df lower.CL upper.CL
#>  PBO     48.4 1.190 134     46.0     50.7
#>  TRT     52.8 1.190 133     50.4     55.1
#> 
#> Results are averaged over the levels of: RACE, SEX 
#> Confidence level used: 0.95
```

Note that the degrees of freedom choice is inherited here from the initial `mmrm`
fit.
Furthermore, we can also obtain the differences between the treatment arms for each visit
by applying `pairs()` on the object returned by `emmeans()` earlier:


``` r
pairs(lsmeans_by_visit, reverse = TRUE)
#> AVISIT = VIS1:
#>  contrast  estimate    SE  df t.ratio p.value
#>  TRT - PBO     3.77 1.070 146   3.514  0.0006
#> 
#> AVISIT = VIS2:
#>  contrast  estimate    SE  df t.ratio p.value
#>  TRT - PBO     3.73 0.859 145   4.346 <0.0001
#> 
#> AVISIT = VIS3:
#>  contrast  estimate    SE  df t.ratio p.value
#>  TRT - PBO     3.08 0.690 131   4.467 <0.0001
#> 
#> AVISIT = VIS4:
#>  contrast  estimate    SE  df t.ratio p.value
#>  TRT - PBO     4.40 1.680 133   2.617  0.0099
#> 
#> Results are averaged over the levels of: RACE, SEX
```

(This is similar like the `pdiff` option in SAS `PROC MIXED`.)
Note that we use here the `reverse` argument to obtain treatment minus
placebo results, instead of placebo minus treatment results.

To further obtain the confidence interval of the least square mean differences, we can apply
`confint()` on the result returned by `pairs()` .

This is similar to the `LSMEANS` in SAS, with `CL` and `DIFF` options.


``` r
confint(pairs(lsmeans_by_visit, reverse = TRUE))
#> AVISIT = VIS1:
#>  contrast  estimate    SE  df lower.CL upper.CL
#>  TRT - PBO     3.77 1.070 146     1.65     5.90
#> 
#> AVISIT = VIS2:
#>  contrast  estimate    SE  df lower.CL upper.CL
#>  TRT - PBO     3.73 0.859 145     2.03     5.43
#> 
#> AVISIT = VIS3:
#>  contrast  estimate    SE  df lower.CL upper.CL
#>  TRT - PBO     3.08 0.690 131     1.72     4.44
#> 
#> AVISIT = VIS4:
#>  contrast  estimate    SE  df lower.CL upper.CL
#>  TRT - PBO     4.40 1.680 133     1.07     7.72
#> 
#> Results are averaged over the levels of: RACE, SEX 
#> Confidence level used: 0.95
```

## Support for car

This package includes methods that allow `mmrm` objects to be used with
`car::Anova()`. This function conducts type II/III hypothesis testing for
the effects in `mmrm` models, using either F-tests (default) or Chi-squared tests. 

### Mathematical basis

For each effect, the Chi-squared test statistic is calculated using:
$$
\chi^2=[\mathbf{L}\cdot b]^T\cdot [\mathbf{L}\cdot\mathbf{V}\cdot\mathbf{L}^T]^{-1}\cdot\mathbf{L}\cdot b
$$
where $\mathbf{L}$ is the desired contrast matrix, $b$ is a one-column matrix containing the coefficients of the non-aliased terms in the model, and $\mathbf{V}$ is the variance-covariance matrix of the coefficients.

The F-test statistic is calculated by dividing the Chi-squared test statistic by the numerator degrees of freedom $\text{df}_\text{num}$, which is equal to the number of parameters associated with the effect and therefore the number of rows in the contrast matrix $\mathbf{L}$. It is also multiplied by a scaling factor $\lambda$ (e.g., when utilizing the adjusted Kenward-Roger degrees of freedom), which can be set to 1 if not needed:

$$
F = \lambda\frac{\chi^2}{\text{df}_\text{num}}
$$

### Examples

In order to see if the used covariates are related to the response using a type II test:





Quitting from ./subsections/_intro-hypothesis_testing.Rmd:130-133 [car_type2]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<error/rlang_error>
Error in `library()`:
! there is no package called 'car'
---
Backtrace:
    ▆
 1. └─base::library(car)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Quitting from introduction.Rmd:49-50 [unnamed-chunk-6]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<error/rlang_error>
Error in `library()`:
! there is no package called 'car'
---
Backtrace:
    ▆
 1. └─base::library(car)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Error: processing vignette 'introduction.Rmd' failed with diagnostics:
there is no package called 'car'
--- failed re-building ‘introduction.Rmd’

--- re-building ‘kenward.Rmd’ using rmarkdown
--- finished re-building ‘kenward.Rmd’

--- re-building ‘methodological_introduction.Rmd’ using rmarkdown
--- finished re-building ‘methodological_introduction.Rmd’

--- re-building ‘mmrm_review_methods.Rmd’ using rmarkdown

Quitting from mmrm_review_methods.Rmd:26-43 [review-setup]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<error/rlang_error>
Error in `library()`:
! there is no package called 'glmmTMB'
---
Backtrace:
    ▆
 1. └─base::library(glmmTMB)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Error: processing vignette 'mmrm_review_methods.Rmd' failed with diagnostics:
there is no package called 'glmmTMB'
--- failed re-building ‘mmrm_review_methods.Rmd’

--- re-building ‘package_structure.Rmd’ using rmarkdown
--- finished re-building ‘package_structure.Rmd’

--- re-building ‘predict.Rmd’ using rmarkdown
--- finished re-building ‘predict.Rmd’

--- re-building ‘satterthwaite.Rmd’ using rmarkdown
--- finished re-building ‘satterthwaite.Rmd’

SUMMARY: processing the following files failed:
  ‘introduction.Rmd’ ‘mmrm_review_methods.Rmd’

Error: Vignette re-building failed.
Execution halted