-
Notifications
You must be signed in to change notification settings - Fork 1
/
tuts0105-creating-rich-results.Rmd
185 lines (132 loc) · 8.09 KB
/
tuts0105-creating-rich-results.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
title: "Creating Rich Results"
---
In this section, we will add rich results to our analysis; a nice APA formatted table like this:
![](tuts0105-creating-rich-results-apa.png){ width=337px }
Rich results are visually appealing, and make it easier for users of your module to 'parse' and understand the results your module produces. A lot of people find statistics challenging, so we might as well make them as accessible and easy to read as possible.
## The Results Definition
In order to provide rich results, we need to add some entries into the results definition. This is the `ttest.r.yaml` file which was created earlier in this tutorial series:
```{yaml }
---
name: ttest
title: Independent Samples T-Test
jrs: '1.1'
items:
- name: text
title: Independent Samples T-Test
type: Preformatted
```
This is pretty similar to the `ttest.a.yaml` file; there's a name, a title, and a jamovi results spec (`jrs`) (note, this is different to the `jas` in the `.a.yaml` file). `items` comes next, and it describes all the items that make up the results. For example, the results could be made up of two tables and a plot. Each item in the results is listed here.
At present, the results contains a single `Preformatted` results item called `text` with the title `Independent Samples T-Test`. If you think back to the [previous section](tuts0104-implementing-an-analysis.html), we accessed a results object with the following:
```{r eval=FALSE}
self$results$text
```
Now you can see where this seemingly arbitrary name `text` came from. This is how different results items are accessed.
## Adding a Table
Let's add a table for our t-test results. For the time being, we'll leave the existing 'Preformatted' results item and add the table underneath. This will let us check that the results we populate the table with are correct.
Modify `ttest.r.yaml` so it reads:
```{yaml }
---
name: ttest
title: Independent Samples T-Test
jrs: '1.1'
items:
- name: text
title: Independent Samples T-Test
type: Preformatted
- name: ttest
title: Independent Samples T-Test
type: Table
rows: 1
columns:
- name: var
title: ''
type: text
- name: t
type: number
- name: df
type: integer
- name: p
type: number
format: zto,pvalue
```
With these modifications, we've added a new table called `ttest` with the title `Independent Samples T-Test`. We've also specified that it is to have 1 row, and that it has four columns; `var`, `t`, `df` and `p`. Notice that we've specified an empty string for the title of column `var`. If a title isn't specified the column name is used instead.
The `type` for each column is specified as either `text`, `number` or `integer`. The type affects the way the column is formatted. `text` columns have their content left aligned, `number` and `integer` columns have their content right aligned. `integer` columns are formatted as whole numbers (no decimal places).
The `format` field is a string of comma separated values. It provides additional information on how a column should be formatted. In the case of the p column, it has two formats applied, `zto` and `pvalue`. `zto` indicates that a value ranges between zero and one; this causes it to be rendered to a fixed number of decimal places. In contrast, values without this format are formatted to a fixed number of significant figures. This makes the biggest difference with values close to zero; without `zto` the value 0.006 would be rendered `0.00600` (3 significant figures), where as with `zto` it would be rendered `0.006` (3 decimal places). With values like correlation co-efficients and other values which range between zero and one, the latter is generally preferred.
The other format is `pvalue`, which indicates that values in this column are [p-values](https://en.wikipedia.org/wiki/P-value). If any of these values drop below a threshold (.001 by default), they will be replaced with `< .001`.
Now that we've set up our table, we can see what it looks like by reinstalling the module:
```{r eval=FALSE}
jmvtools::install()
```
If you return to jamovi now, you'll see that the installed module has been updated and our earlier analysis has been rerun. The table we added is now visible in the results. This is pretty handy: whenever you reinstall a module, it is updated immediately. Your results should now look as follows:
![](tuts0105-creating-rich-results-blank-table.png){ width=509px }
## Populating a Table
Now we've created a table, let's fill it in with the values from the t-test. For this, we need to extract the actual values from the t-test object returned by R's `t.test` function. Rather awkwardly, analyses in R don't return consistent results objects; there's generally no predictable way to know how to access the underlying values. Some R functions return lists, others return S3 objects, others S4. Some require reading the values with indices (`[[x]]`), others require the use of slots (`@`). It generally requires a bit of trial and error to figure out how to access the underlying values of a results object. One useful function for this is the `mode()` function. For example, we can call mode on a `t.test` results object like so:
```{r}
results <- t.test(1:5)
mode(results)
```
This tells us that the results object from the t.test function is a list (although somewhat in disguise). We can then use the functions we normally use on lists, for example:
```{r}
results <- t.test(1:5)
names(results)
```
With a little more tinkering we can figure out that `results$statistic` contains the *t* statistic, `results$parameter` contains the degrees of freedom, and `results$p.value` contains the p value (that last one probably didn't require that much tinkering).
We can assign each of these values to the row in our table with the `setRow()` function:
```{r eval=FALSE}
table <- self$results$ttest
table$setRow(rowNo=1, values=list(
var=self$options$dep,
t=results$statistic,
df=results$parameter,
p=results$p.value
))
```
Now if we reinstall our module:
```{r eval=FALSE}
jmvtools::install()
```
Our results in jamovi should look something like this:
![](tuts0105-creating-rich-results-filled-table.png){ width=509px }
Now we have everything working, let's remove the text results:
```{r eval=FALSE}
ttestISClass <- R6Class("ttestISClass",
inherit=ttestISBase,
private=list(
.run=function() {
formula <- constructFormula(self$options$dep, self$options$group)
formula <- as.formula(formula)
results <- t.test(formula, self$data)
table <- self$results$ttest
table$setRow(rowNo=1, values=list(
var=self$options$dep,
t=results$statistic,
df=results$parameter,
p=results$p.value
))
})
)
```
then perform a reinstall with `jmvtools::install()`. Now we have a nice, attractive, minimalist t-test.
![](tuts0105-creating-rich-results-final-table.png){ width=287px }
Let's also take a look at running this analysis in an R session. We can install the package into R with:
```{r, eval=FALSE}
devtools::install() # note here we're using devtools, not jmvtools
```
and run the analysis with:
```{r, eval=FALSE}
library(SuperAwesome)
data("ToothGrowth")
ttest(ToothGrowth, dep='len', group='supp')
```
```
Independent Samples T-Test
Independent Samples T-Test
──────────────────────────────
t df p
──────────────────────────────
len 1.92 55 0.061
──────────────────────────────
```
As can be seen, a nicely formatted table has been generated in R as well. Everything is maintained, right down to the number of decimal places. In this way jamovi can make R packages --- even if you have no intention of publishing as jamovi modules --- more attractive, easier to read, and more consistent too.
Next: [Adding Plots](tuts0106-adding-plots.html)