How do I check a result with a second program? (reproducibility, R script, type III sums of squares)
In brief
A result is reproducible when another person, with the same data and the same steps, arrives at the same numbers. For this, Statable gives every supported analysis as an R script: a text file with commands for the free statistics software R, plus the data used as a CSV file. Whoever runs the script calculates the analysis a second time, with a different program. If the numbers match, a calculation error is very unlikely, and every step is written down.
When do I use it?
- For the appendix of a thesis or an article: reviewers see how the calculation was done.
- To cross-check an important number before it goes into the report.
- To continue working in R, for example for a chart or a model that Statable does not offer.
- For learning: each line shows which calculation step is behind a number in the output.
Requirements
You need R (free, for Windows, macOS and Linux), ideally with the RStudio interface. The script uses only R itself and the included packages MASS and nnet; it loads nothing from the web. The CSV file has to be in the same folder as the script.
Reading a script: the t-test
For a comparison of two groups the script contains, among others, these lines. y is the measured value, g the group:
ergebnis <- t.test(y ~ g, var.equal = TRUE)
ergebnis_welch <- t.test(y ~ g)
levene <- anova(lm(abs(y - ave(y, g)) ~ g))
- The first line calculates the classical t-test, which assumes equal spread in both groups (var.equal = TRUE).
- The second line calculates the Welch test. In R it is the default of t.test() when var.equal is missing.
- The third line is the Levene test as in SPSS: an analysis of variance of the distances of each value from the mean of its group (ave(y, g) is this group mean).
At the end of each script, the numbers that Statable showed are listed as a comment. This allows a direct comparison.
An example with real data: why aov() shows other F values
In the Palmer penguin data (333 animals with species, sex and body mass) the cells are unequally filled, for example 61 male and 58 female Gentoo penguins. A two-way analysis of variance of body mass by species and sex gives these F values:
| Method | Species | Sex |
|---|---|---|
| Type III (PSPP, SPSS, Statable) | 746.92 | 311.84 |
| Type I, species first | 758.36 | 387.46 |
| Type I, sex first | 749.02 | 406.14 |
Type I with species first is anova(lm(mass ~ species * sex)) in R, with sex first anova(lm(mass ~ sex * species)).
The functions anova() and aov() in R calculate in order (type I): each variable gets only what the variables before it have not yet explained. With unequally filled cells the result therefore depends on the order. PSPP, SPSS and Statable calculate type III: each variable is tested as if it were the last one in the model. The interaction is last in all three methods and therefore the same everywhere (F = 8.76). The R script from Statable recalculates type III with its own lines and not with aov(), so that the numbers match.
Frequency weight: rows become cases
Sometimes a table is already summarised in the file: the penguins by species and island fit into 5 rows with a column "count", 344 animals in total. With this column as frequency weight the crosstab gives the same chi-square as the 344 single rows: χ² = 299.6 with df = 4. For this the script repeats each row as often as its weight says, like PSPP with WEIGHT BY. It does not use lm(weights = ...): R treats these weights as the precision of a row, not as a number of cases. Degrees of freedom and p-values would then count rows instead of cases; for the mean body mass from 3 rows (one per species) 2 instead of 341 degrees of freedom would remain.
How it works in Statable
- One result: open "Export ▾" in the result and choose "As R script with data (.zip)". The whole report: on the page "Report", "As R script (.zip)".
- Unzip the ZIP file, open R or RStudio and change to the unzipped folder (in RStudio: Session > Set Working Directory > To Source File Location).
- Run the script with source() or go through it line by line and compare the output with the comment lines at the end.
Reading and reporting the result
R writes numbers with a point and usually with more digits. The same means: the same after rounding to the digits Statable shows. In the report one sentence about the method is enough; the script belongs in the appendix:
Body mass differed by sex, F(1, 327) = 311.84, p < .001 (type III sums of squares). Calculated with Statable, recalculated in R with the script in the appendix.
Limits
- Not every analysis has a script; then the menu has no entry. There is none for a survey weight, because it calculates differently from a frequency weight.
- In factor analysis with promax rotation the script calculates like PSPP. The R function stats::promax uses a slightly different procedure; its loadings can differ in the second decimal place.
- A second program finds calculation errors, not reasoning errors: a wrongly chosen test stays wrongly chosen in R too.
What is often misunderstood
For specialists
Type III sums of squares require sum or effect contrasts; with the treatment contrasts that are the R default, drop1() would give other, hard-to-read values for the main effects. The script therefore compares models with effect coding. The choice between type II and type III is debated in the literature; Statable follows PSPP and SPSS so that results from teaching can be recognised. The Levene test of the script uses distances to the group mean (Levene 1960); the variant around the median is called the Brown-Forsythe test.
Sources
Sandve, G. K., Nekrutenko, A., Taylor, J. & Hovig, E. (2013). Ten simple rules for reproducible computational research. PLoS Computational Biology, 9(10), e1003285. R Core Team: R FAQ, section 7 (order of factors in anova()); help pages for t.test, anova and promax. Levene, H. (1960). Robust tests for equality of variances. In I. Olkin (Ed.), Contributions to Probability and Statistics. Stanford University Press. Data: Gorman, K. B., Williams, T. D. & Fraser, W. R. (2014). PLoS ONE, 9(3), e90081 (palmerpenguins, CC0).
Take away
- Reproducible means: others arrive at the same numbers with the same data and steps.
- If numbers differ, compare the method first: type I or type III, Welch or classical.
- A frequency weight turns one row into several cases; lm(weights = ...) does not.
- The same means: the same after rounding.