TukeyTest: Tukey HSD Test for Multiple Comparisons v2.0

View source: R/TukeyTest.R

TukeyTestR Documentation

Tukey HSD Test for Multiple Comparisons v2.0

Description

Performs Tukey's Honest Significant Difference (HSD) test for all pairwise comparisons after fitting an ANOVA model. This post hoc method uses the studentized range distribution and is appropriate when variances are equal across groups and observations are independent.

Usage

TukeyTest(modelo, comparar = NULL, alpha = 0.05)

Arguments

modelo

An aov or lm object (full model: includes blocks, factors, etc.).

comparar

Character vector with the name(s) of the factor(s) to compare: - One name: main effect (e.g., "treatment" or "A") - Several names: interaction (e.g., c("A","B") for A:B) If omitted, it uses the first factor in modelo$xlevels.

alpha

Significance level (default 0.05).

Details

Tukey's test controls the family-wise error rate and is widely used when group comparisons have not been planned in advance.

Advantages: - Strong control of Type I error rate. - Ideal for balanced designs with equal variances.

Disadvantages: - Assumes equal variances and sample sizes. - Less powerful with heteroscedasticity.

Value

An object of class "tukey" and "comparaciones" containing:

  • Resultados: a data.frame with columns Comparacion, Diferencia, SE, t_value, p_value (unadjusted), p_ajustada (Tukey), Valor_Critico (critical difference), and Significancia.

  • Promedios: a named vector of group means as defined by comparar.

  • Orden_Medias: group names ordered from highest to lowest mean.

  • Metodo: "Tukey test".

  • Termino: the term being compared (e.g., "A", "B", or "A:B").

  • MSerror, df_error, N: useful for plots with error bars.

References

Tukey, J. W. (1949). "Comparing individual means in the analysis of variance." Biometrics, 5(2), 99–114. <https://doi.org/10.2307/3001913>

Examples


#Caso DCA
data(d_e, package = "Analitica")
mod1 <- aov(Sueldo_actual ~ as.factor(labor), data = d_e)
summary(mod1)
resultado <- TukeyTest(mod1)
summary(resultado)
plot(resultado)

#Caso DBA
mod2 <- aov(Sueldo_actual ~ as.factor(labor) + Sexo, data = d_e)
summary(mod2)
# Comparar niveles de 'tratamiento' (ajustando el error por el modelo con bloque)
res <- TukeyTest(mod2, comparar = "as.factor(labor)")
summary(res)
plot(res)

#Caso DFA Two Ways
mod3 <- aov(Sueldo_actual ~ as.factor(labor) * Sexo, data = d_e)
summary(mod3)
# promedios de as.factor(labor) (promediando sobre B)
resA <- TukeyTest(mod3, comparar = "as.factor(labor)")
summary(resA)
plot(resA)

# promedios de la interaccion entre factor A y factor B
resB <- TukeyTest(mod3, comparar = c("as.factor(labor)","Sexo"))
summary(resB)
plot(resB)


Analitica documentation built on Nov. 5, 2025, 5:13 p.m.