The data set df_stress
is included in the sprtt
package. Thus, the data set is available when the package is loaded.
In the R code sections:
# comment
: is a commentfunction()
: is R code#> results of function()
: is console outputThe sprtt
package is the implementation of sequential probability ratio tests using the associated t-statistic (sprtt). This vignette describes an exemplary use case to improve the understanding of the package and the sequential t-test.
Other recommended vignettes cover:
a general guide, how to use the package.
A team of researchers wants to know if the stress of the employees increased in the company itFlow AG over the last year. The Human Resources department has gathered data from a new self-care tool they implemented for the employees. It is difficult to predict how many employees will participate at the second measurement point (one year after the baseline). The team suggests using a sequential t-test instead of a Students t-test, because the sequential procedure can reduce the required sample size and is stopped right after the decision for one of the hypotheses is made. Furthermore, the sequential t-test is able to gather evidence for both the alternative and the null hypothesis.
The researchers know that the company has received more orders than in the year before. Thus, they expect an increase in self-reported stress.
The parameters of the sequential t-test are specified as follows:
The researchers expect an increase in stress but they have no prior knowledge about the expected effect size. Thus, they define the lower limit of a substantial effect of interest.
<- 0.2 d
They choose the common \(\alpha\) level of .05 and the same level for the \(\beta\) error probability, which leads to a power of .95 (\(1 - \beta\)).
<- 0.05
alpha <- 0.95 power
The data are repeated measures, therefore the test is a paired sequential t-test.
<- TRUE paired
The researchers expect that the stress will have increased after one year. Thus, they specify that the true difference in means is greater than 0.
(mean(one_year_stress) - mean(baseline_stress)) > 0
<- "greater" alternative
The HR department receives the new data piece by piece and passes it directly on to the researchers. The test is performed for the first time and starts with the first two data points.
# first data from the Human Resources department ---
# current sample size
<- 2
n_person
# get data
<- df_stress[1:n_person,]
df
# print data
df#> baseline_stress one_year_stress
#> 1 7.175250 7.844337
#> 2 4.918343 5.527191
# sequential t-test
<- seq_ttest(df$one_year_stress,
results $baseline_stress,
dfalpha = alpha,
power = power,
d = d,
paired = paired,
alternative = alternative,
verbose = FALSE)
# print results: console output
results#>
#> ***** Sequential Paired t-test *****
#>
#> data: df$one_year_stress and df$baseline_stress
#> test statistic:
#> log-likelihood ratio = 0.33191, decision = continue sampling
#> SPRT thresholds:
#> lower log(B) = -2.94444, upper log(A) = 2.94444
The decision from the first test is:
@decision
results#> [1] "continue sampling"
As a result, the researchers take one more data point and run the test again.
# new data from the Human Resources department ---
# get one more person
<- n_person + 1
n_person <- df_stress[1:n_person,]
df
# print new data
df#> baseline_stress one_year_stress
#> 1 7.175250 7.844337
#> 2 4.918343 5.527191
#> 3 4.634266 5.783046
# sequential t-test
<- seq_ttest(df$one_year_stress,
results $baseline_stress,
dfalpha = alpha,
power = power,
d = d,
paired = paired,
alternative = alternative,
verbose = FALSE)
# print results
@decision
results#> [1] "continue sampling"
This process is repeated until the decision is made for one of the two hypotheses. To simulate this sequential process, a while-loop embraces the sequential t-test function in the code below, which will not stop until one of the hypotheses is accepted or the maximum of the data is reached.
# define the starting point
<- "continue sampling"
decision <- 3
n_person
# simulation of the sequential procedure
while(decision == "continue sampling") {
# get the current data
<- df_stress[1:n_person,]
df
# run the sequential test and save the results
<- seq_ttest(df$one_year_stress,
results $baseline_stress,
dfalpha = alpha,
power = power,
d = d,
paired = paired,
alternative = alternative)
# save the current desicion
<- results@decision
decision
# add a new person
<- n_person + 1
n_person
# break if the maximum of the data is reached
if (n_person > nrow(df_stress)) {
break
}
}
# console output
results#>
#> ***** Sequential Paired t-test *****
#>
#> data: df$one_year_stress and df$baseline_stress
#> test statistic:
#> log-likelihood ratio = 2.98795, decision = accept H1
#> SPRT thresholds:
#> lower log(B) = -2.94444, upper log(A) = 2.94444
#> Log-Likelihood of the:
#> alternative hypothesis = -2.23785
#> null hypothesis = -5.2258
#> alternative hypothesis: true difference in means is greater than 0.
#> specified effect size: Cohen's d = 0.2
#> degrees of freedom: df = 47
#> sample estimates:
#> mean of the differences
#> 0.47931
#> Note: to get access to the object of the results use the @ or []
#> instead of the $ operator.
The while-loop comes to an end after 48 data points.
# Required results for the report
# likelihood ratio (LR)
<- round(results@likelihood_ratio, digits = 2)
LR
LR#> [1] 19.85
# sample size (N) = degrees of freedom +2 (two-samples) or +1 (one-sample & paired)
<- results@df + 1
N
N#> [1] 48
# baseline stress (M and SD)
<- round(mean(df$baseline_stress), digits = 2)
mean_t1
mean_t1#> [1] 4.99
<- round(sd(df$baseline_stress), digits = 2)
sd_t1
sd_t1#> [1] 1.02
# after one year stress (M and SD)
<- round(mean(df$one_year_stress), digits = 2)
mean_t2
mean_t2#> [1] 5.47
<- round(sd(df$one_year_stress), digits = 2)
sd_t2
sd_t2#> [1] 1.53
# NOT INCLUDED IN THE PACKAGE
# calculate effect size: Cohen´s d
<- effsize::cohen.d(df$one_year_stress,
d_results $baseline_stress,
dfpaired = TRUE)
<- round(d_results$estimate, digits = 2)
d
d#> [1] 0.34
# confidence intervall
<- round(d_results$conf.int[[1]], digits = 2)
d_lower
d_lower#> [1] 0.11
<- round(d_results$conf.int[[2]], digits = 2)
d_upper
d_upper#> [1] 0.57
Starting at N = 2, the test stops sampling at a total sample size of N = 48 with LR48 = 19.85. This ratio indicates that the data are about 20 times more likely under H1 than under H0. Thus, we accept the alternative hypothesis: The perceived stress at the second measurement (M = 5.47, SD = 1.53) is higher than one year ago at the baseline measurement (M = 4.99, SD = 1.02), Cohen`s d = 0.34, 95% CI [0.11, 0.57].1,2