Getting Started with the TH Package

Willian Silva Barros

Introduction

The TH package was developed to support the teaching and learning of hypothesis testing in R. Each function presents a graphical sequence containing the null and alternative hypotheses, significance level, reference distribution, critical values, rejection and non-rejection regions, calculated statistic, and statistical decision.

Version 1.0.0 preserves the educational graphical approach while correcting unsafe argument defaults, directional F-test logic, chi-square formulations, Welch degrees of freedom, input validation, graphical side effects, and inferential wording. Numeric Ha values remain supported for backward compatibility.

Installation

install.packages(
  "TH_1.0.0.tar.gz",
  repos = NULL,
  type = "source"
)

Loading the package

library(TH)
packageVersion("TH")
#> [1] '1.0.0'
help(package = "TH")

Available functions

Function Statistical procedure
tz1() One-Sample Z Test
tz2() Two-Sample Z Test
tt1() One-Sample t Test
tt2i() Two-Sample t Test with Equal Variances
tt2d() Two-Sample t Test with Unequal Variances
tt2p() Paired t Test
tf2() F Test for Two Variances
tq2a() Chi-Square Goodness-of-Fit Test
tq2i() Chi-Square Test of Independence
tq2h() Chi-Square Test of Homogeneity

Meaning of the Ha argument

For tests concerning means, Ha = 1 selects a two-sided test, Ha = 2 a right-tailed test, and Ha = 3 a left-tailed test. The equivalent character values are "two.sided", "greater", and "less". In tf2(), only two-sided and right-tailed alternatives are supported. The three chi-square procedures use only the standard upper-tail omnibus test, so Ha must be 2 or "greater".

Choosing a function

Statistical situation Function
One mean and known population variance tz1()
Two means and known population variances tz2()
One mean and unknown population variance tt1()
Two independent means with equal variances tt2i()
Two independent means with unequal variances tt2d()
Two paired measurements tt2p()
Two variances tf2()
Observed versus expected frequencies tq2a()
Association between two categorical variables tq2i()
Homogeneity across groups or populations tq2h()

Examples for all functions

One-Sample Z Test

Performs a Z test for one population mean when the population variance is known.

X <- c(52, 49, 51, 50, 53, 54, 48, 52, 51, 50)
tz1(X, mp = 50, vp = 4, alfa = 0.05, Ha = 1, unidade = " units")

## Two-Sample Z Test

Performs a Z test for comparing two population means when both population variances are known.

X <- c(15, 16, 14, 17, 15, 16, 14, 15)
Y <- c(11, 12, 13, 10, 12, 11, 13, 12)
tz2(X, Y, vpX = 4, vpY = 4, alfa = 0.05, Ha = 2, plot = FALSE)

One-Sample t Test

Performs Student’s t test for one population mean when the population variance is unknown.

X <- c(11, 12, 13, 12, 14, 13, 12, 15)
tt1(X, mp = 10, alfa = 0.05, Ha = 2, unidade = " units", plot = FALSE)

Two-Sample t Test with Equal Variances

Compares two independent population means under the assumption of equal population variances.

X <- c(18, 20, 19, 21, 22, 20)
Y <- c(14, 15, 16, 15, 14, 16)
tt2i(X, Y, alfa = 0.05, Ha = 1, plot = FALSE)

Two-Sample t Test with Unequal Variances

Compares two independent population means without assuming equal population variances.

X <- c(20, 21, 19, 22, 18, 20)
Y <- c(10, 15, 5, 20, 0, 10)
tt2d(X, Y, alfa = 0.05, Ha = 2, plot = FALSE)

Paired t Test

Performs a t test for two dependent or paired measurements.

before <- c(70, 72, 68, 75, 74, 71, 69, 73)
after <- c(66, 69, 65, 71, 70, 68, 66, 69)
tt2p(before, after, mpD = 0, alfa = 0.05, Ha = 3, unidade = " units", plot = FALSE)

F Test for Two Variances

Performs an F test for comparing two population variances.

X <- c(8, 12, 16, 20, 24, 28)
Y <- c(14, 15, 14, 15, 14, 15)
tf2(X, Y, alfa = 0.05, Ha = 2, plot = FALSE)

Chi-Square Goodness-of-Fit Test

Compares observed frequencies with expected frequencies for mutually exclusive categories.

categories <- c(rep("A", 50), rep("B", 30), rep("C", 20))
tq2a(categories, p = c(1, 1, 1), alfa = 0.05, Ha = 2, r = 0, plot = FALSE)

Chi-Square Test of Independence

Assesses the association between two categorical variables.

FO <- matrix(c(30, 10, 5, 10, 25, 20), nrow = 2, byrow = TRUE)
tq2i(NULL, NULL, FO = FO, alfa = 0.05, Ha = 2, r = 0, plot = FALSE)

Chi-Square Test of Homogeneity

Compares categorical distributions across groups or populations.

FO <- matrix(c(30, 15, 5, 10, 25, 15, 20, 20, 10), nrow = 3, byrow = TRUE)
tq2h(NULL, NULL, FO = FO, alfa = 0.05, Ha = 2, r = 0, plot = FALSE)

Interpreting the graphical output

Read the output in the following order:

  1. identify the null and alternative hypotheses;
  2. verify the significance level;
  3. locate the critical value or critical values;
  4. identify the rejection region;
  5. locate the calculated test statistic;
  6. record the decision to reject or not reject the null hypothesis;
  7. interpret the decision in the context of the problem.

Failure to reject the null hypothesis does not prove that it is true. It indicates that the data did not provide sufficient evidence to reject it at the selected significance level.

Assumptions and limitations

The appropriate test must be selected according to the sampling design, independence of observations, distributional assumptions, knowledge of population variances, equality of variances when required, and adequacy of expected frequencies in chi-square procedures.

Version 1.0.0 is an educational implementation with validated inputs, executable examples, automated regression tests, structured invisible results, and optional graphical output. Raw-data and summary-statistics modes are mutually exclusive. Users remain responsible for checking study design and statistical assumptions.

Help and citation

?tz1
citation("TH")
browseVignettes("TH")

Author

Willian Silva Barros
Federal University of Pelotas
Email: willian.barros@ufpel.edu.br

Session information

sessionInfo()
#> R version 4.6.1 (2026-06-24 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 11 x64 (build 26200)
#> 
#> Matrix products: default
#>   LAPACK version 3.12.1
#> 
#> locale:
#> [1] LC_COLLATE=C                       LC_CTYPE=Portuguese_Brazil.utf8   
#> [3] LC_MONETARY=Portuguese_Brazil.utf8 LC_NUMERIC=C                      
#> [5] LC_TIME=Portuguese_Brazil.utf8    
#> 
#> time zone: America/Sao_Paulo
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] TH_1.0.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.39     R6_2.6.1          fastmap_1.2.0     xfun_0.59        
#>  [5] cachem_1.1.0      knitr_1.51        htmltools_0.5.9   rmarkdown_2.31   
#>  [9] lifecycle_1.0.5   cli_3.6.6         sass_0.4.10       jquerylib_0.1.4  
#> [13] compiler_4.6.1    rstudioapi_0.19.0 tools_4.6.1       evaluate_1.0.5   
#> [17] bslib_0.11.0      yaml_2.3.12       otel_0.2.0        jsonlite_2.0.0   
#> [21] rlang_1.3.0