7  Describe single-case data frames

7.1 Descriptive statistics

The describe function call

describe(data, dvar, pvar, mvar)

describe is the basic command to get an overview on descriptive statistics. As an argument it only takes the name of an scdf object. For each case of the scdf and each phase within a case descriptive statistics are provided. The output table contains statistical indicators followed by a dot and the name of the phase (e.g., n.A for the number of measurements of phase A).

Statistics of the describe command
Parameter What it means ...
n Number of measurements.
mis Number of missing values.
m Mean values.
md Median of values.
sd Standard deviation of values.
mad Median average deviation of values.
min/max Min and max of values.
trend Slope of a regression line through values by time.
describe(exampleABC)
Describe Single-Case Data

       Marie Rosalind  Lise
Design A-B-C    A-B-C A-B-C
n.A       10       15    20
n.B       10        8     7
n.C       10        7     3
mis.A      0        0     0
mis.B      0        0     0
mis.C      0        0     0

          Marie Rosalind    Lise
m.A      52.000   52.267  52.350
m.B      72.100   73.250  73.571
m.C      68.000   66.429  71.333
md.A       53.5     52.0    52.0
md.B       72.5     72.0    73.0
md.C         69       68      76
sd.A      8.287    8.146  10.869
sd.B     11.367   13.134  10.644
sd.C     12.702   10.486  21.385
mad.A    11.119    7.413  10.378
mad.B    10.378   10.378  16.309
mad.C    17.791   11.861  20.756
min.A        39       37      35
min.B        47       54      60
min.C        51       52      48
max.A        63       65      74
max.B        85       97      87
max.C        87       78      90
trend.A  -1.915    0.500  -0.088
trend.B  -0.612    0.643   1.929
trend.C  -0.194   -2.929 -14.000

The resulting table could be exported into a csv file to be used in other software (e.g., to inserted in a word processing document). Therefore, first write the results of the describe command into an R object and then use the write.csv to export the descriptives element of the object.

# write the results into a new R object named `res`
res <- describe(exampleABC)
# create a new file containing the descriptives on your harddrive
write.csv(res$descriptives, file = "descriptive data.csv")

The file is written to the currently active working directory. If you are not sure where that is, type getwd() (you can use the setwd() command to define a different working directory. To get further details type help(setwd) into R).

Conflicting function names

Sometimes R packages include the same function names. For example, the describe() function is also part of the psych package. Now, if you have loaded the psych package with library(psych) after scan the describe() function of scan will be masked (describe() would now call the corresponding function of the psych package).
There are two solutions to this problem:

  1. activate the psych library before the scan library (now the psych describe() function will be masked) or
  2. include the package name into the function call with the prefix scan::: scan::describe().

7.2 Standardized mean differences

```{r}
#| results: asis
function_structure("smd")
```
The smd function call

smd(data, dvar, pvar, mvar, decreasing = FALSE, phases = c(1, 2))

Standardized mean differences are measures of effect sizes. The smd functions calculated various variations.

smd(exampleAB)
Standardized mean differences

                            Johanna Karolina  Anja
mA                            54.60    51.80 53.60
mB                            74.13    73.47 74.07
sdA                            2.41     6.83  3.05
sdB                            8.94     9.76  7.57
sd cohen                       6.55     8.43  5.77
sd hedges                      7.97     9.19  6.83
Glass' delta                   8.11     3.17  6.71
Hedges' g                      2.45     2.36  3.00
Hedges' g correction           2.35     2.26  2.87
Hedges' g durlak correction    2.23     2.14  2.72
Cohen's d                      2.98     2.57  3.54

7.3 Auto-regression

```{r}
#| results: asis
function_structure("autocorr")
```
The autocorr function call

autocorr(data, dvar, pvar, mvar, lag_max = 3, …)

The autocorr function calculates autocorrelations within each phase and across all phases. The lag_max argument defines the lag up to which the autocorrelation will be computed.

autocorr(exampleABC, lag_max = 4)
Autocorrelations

Marie 
 Phase Lag 1 Lag 2 Lag 3 Lag 4
     A  0.29 -0.11  0.10  0.12
     B -0.28 -0.10 -0.14 -0.09
     C  0.00 -0.33 -0.14 -0.25
   all  0.21  0.10  0.25  0.12

Rosalind 
 Phase Lag 1 Lag 2 Lag 3 Lag 4
     A  0.37 -0.29 -0.33 -0.34
     B -0.34  0.24 -0.40  0.04
     C -0.07 -0.32  0.27  0.02
   all  0.49  0.38  0.22  0.17

Lise 
 Phase Lag 1 Lag 2 Lag 3 Lag 4
     A  0.04 -0.32 -0.05 -0.09
     B -0.63  0.50 -0.40  0.31
     C -0.38 -0.12    NA    NA
   all  0.33  0.36  0.23  0.27

7.4 Trend analysis

```{r}
#| results: asis
function_structure("trend")
```
The trend function call

trend(data, dvar, pvar, mvar, offset = “deprecated”, first_mt = 0, model = NULL)

The trend function provides an overview of linear trends in single-case data. By default, it gives you the intercept and slope of a linear and a squared regression of measurement-time on scores. Models are computed separately for each phase and across all phases. For a more advanced application, you can add regression models using the R specific formula class.

# Simple example
trend(exampleABC$Marie)
Trend for each phase

              Intercept      B   Beta
Linear.ALL       55.159  0.612  0.392
Linear.A         60.618 -1.915 -0.700
Linear.B         74.855 -0.612 -0.163
Linear.C         68.873 -0.194 -0.046
Quadratic.ALL    59.135  0.017  0.330
Quadratic.A      57.937 -0.208 -0.712
Quadratic.B      73.217 -0.039 -0.098
Quadratic.C      68.490 -0.017 -0.038

Note. Measurement-times start at 0  for each phase
# Complex example
trend(
  exampleAB$Johanna, 
  model = c("Cubic" = values ~ I(mt^3), 
            "Log Time" = values ~ log(mt+1))
)
Trend for each phase

              Intercept      B   Beta
Linear.ALL       52.271  1.787  0.908
Linear.A         54.400  0.100  0.066
Linear.B         62.758  1.625  0.813
Quadratic.ALL    58.582  0.086  0.864
Quadratic.A      54.841 -0.040 -0.110
Quadratic.B      66.985  0.106  0.767
Cubic.ALL        61.354  0.004  0.806
Cubic.A          55.050 -0.022 -0.251
Cubic.B          68.784  0.007  0.721
Log Time.ALL     43.532 12.149  0.848
Log Time.A       54.032  0.593  0.156
Log Time.B       57.300  9.051  0.791

Note. Measurement-times start at 0  for each phase