-
Notifications
You must be signed in to change notification settings - Fork 75
Example 2
In this example, example02.b.R file contains:
# This file is a generated template, your changes will not be overwritten
Example02Class <- R6::R6Class(
"Example02Class",
inherit = Example02Base,
private = list(
.init = function() {
preformatted <- jmvcore::Preformatted$new(self$options, 'ttest')
self$results$add(preformatted)
},
.run = function() {
if (is.null(self$options$dependent) ||
is.null(self$options$group))
return()
data <- data.frame(
x = self$data[[self$options$group]],
y = self$data[[self$options$dependent]])
ttest <- t.test(y ~ x, data, var.equal=self$options$equVar)
results <- self$results$get('ttest')
results$content <- paste0(capture.output(ttest), collapse='\n')
})
)
In this example, we begin by checking the values of the dependent and group options.
if (is.null(self$options$dependent) ||
is.null(self$options$group))
return()
As can be seen, the values of options can be accessed from self$options$.... When this analysis is first selected in jamovi, the options UI appears but no variables are assigned to dependent or group. As such, dependent and group will begin with the value of NULL. This initial check, checks to see if either dependent or group are NULL. If either are, the analysis returns without doing anything.
The next piece of code creates a new data.frame from the data provided.
data <- data.frame(
x = self$data[[self$options$group]],
y = self$data[[self$options$dependent]])
The group column is assigned to a column called x, and the dependent column is assigned to a column called y. self$data is itself a data.frame, but the renaming of columns makes it easier to specify a formula for the t-test function below.
ttest <- t.test(y ~ x, data, var.equal=self$options$equVar)
In this code, the t-test is performed. The t.test() function is from the stats package, and is included with R. As can be seen, the var.equal argument is read directly from the equVar option of the analysis.
In this final piece of code, the results of the t.test() function are converted to a character string, and assigned to the preformatted results object.
results <- self$results$get('ttest')
results$content <- paste0(capture.output(ttest), collapse='\n')
We use the capture.output() function (from utils) to convert the t-test results to a character vector. However, capture.output() produces a multi-element character vector, with each line represented as an element. The paste0(..., collapse='\n') joins all these together into a single string (with newline characters in the appropriate places).
You can see this analysis in action by selecting Example 2 from the jmvexamples menu. We recommend you use the included ToothGrowth example data set, and assign len to the Dependent Variable, and supp to the Group Variable. You can also toggle the Equal Variances checkbox. As can be seen, with each change to the UI, the analysis is rerun, and the results kept up-to-date.
Although this analysis is functional, it's output is not the easiest to read. In the next example, we will present the results in an attractive APA formatted table.
[Next >> Example 3](Example 3)
Tutorial
- [Getting Started](Getting Started)
- [Example 0](Example 0)
- [Example 1](Example 1)
- [Example 2](Example 2)
- [Example 3](Example 3)
- [Example 4](Example 4)
- [Example 5](Example 5)
API