---
title: "Confidence Interval Fix"
author: "Jonathan Bakdash and Laura Marusich"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Confidence Interval Fix}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, tidy = FALSE)
options(width = 80)
library("knitr", "rmarkdown", "rmcorr") 
require(knitr)
require(rmarkdown)
require(rmcorr)
require(psych)

#Kludge to add cocor b/c of lib loc error 
#if(!require("cocor"))
#  install.packages("cocor")
```

### Confidence Interval Fix in 0.5.4
In versions of rmcorr prior to 0.5.4, confidence intervals (CIs) were incorrectly calculated using the error degrees of freedom instead of using the effective sample size (error df + 2). Generally, the difference is slight, with the previous incorrect CIs tending to be a bit too broad (conservative). However, when *both* the number of participants *and* repeated measures are small, the prior incorrect confidence intervals were substantially too wide. In other words, a difference of 2 in the df has diminishing impact on the width of CIs as the error df increases.

This fix does not change the effect size, *p*-value, or the error degrees of freedom. All remain the same as prior versions. Bootstrapped CIs are also unaffected.

#### Example with Slight Difference in CIs
We use the data from raz2005 to compare the old, incorrect CI calculation to the current, correct CI. The sample size is *N* = 72 with *k* = 2 repeated measures. Note the difference in CI bounds is slight: old, incorrect 95\% CI [-0.8053581, -0.5637514] and current, correct 95\% CI [ -0.804153, -0.566080].
```{r}
brainvolage.rmc <- rmcorr(participant = Participant, measure1 = Age, measure2 = Volume, dataset = raz2005)

#Old, incorrect CI using error df
psych::r.con(brainvolage.rmc$r, brainvolage.rmc$df, p = 0.95)

#Current, correct CI using effective sample size
brainvolage.rmc$CI

#Manual calculation
psych::r.con(brainvolage.rmc$r, brainvolage.rmc$df + 2, p = 0.95)
```
#### Example with Substantial Difference in CIs
For a small sample size (*N* = 5 total participants) with *k* = 2 repeated measures and *r*<sub>*rm*</sub> = 0.88, we show a substantial difference in the old 95\% CI [-0.5257087, 0.9974701] compared to the correct 95\% CI [0.2394418, 0.9868084].
```{r}
#Old, incorrect CI using error df
psych::r.con(0.88, 4, p = 0.95)

#Current, correct CI using effective sample size
psych::r.con(0.88, 6, p = 0.95)
```
#### Another Slight Example: Same as Above with a Larger Sample Size
The same example as above except with the number of participants quadrupled: *N* = 20. Note the difference in coverage of CIs is now slight.
```{r}
#Old, incorrect CI using error df
psych::r.con(0.88, 19, p = 0.95)

#Current, correct CI using effective sample size
psych::r.con(0.88, 21, p = 0.95)
```
```