Memuat
Memuat
ANOVA, ANCOVA and Tukey Post Hoc Test for Fisheries and Marine Science
0 views
Comparing final catfish weight across three treatment groups with ANCOVA using initial weight as covariate and Tukey post hoc tests, demonstrated using synthetic data. One-way ANOVA compares the mean of a numeric outcome across three or more groups, while ANCOVA makes the same comparison after adjusting for a covariate such as a pretest score. It suits experimental or quasi-experimental theses comparing treatments, methods or varieties, with the Tukey post hoc test showing which pairs differ.

Comparing final catfish weight across three treatment groups with ANCOVA using initial weight as covariate and Tukey post hoc tests, demonstrated using synthetic data.
| Role | Variable | Unit |
|---|---|---|
| Main | Final catfish weight | g |
| Supporting 1 | Feed type | group |
| Supporting 2 | Initial weight | g |
ANOVA shows that Final catfish weight differs significantly across levels of Feed type (F = 10.14, p < 0.001, eta2 = 0.051); Tukey finds 2 of 3 pairs different. Adjusting for Initial weight (ANCOVA), the group effect remains significant (p < 0.001, partial eta2 = 0.117), highest in Maggot meal; Levene p = 0.843.
Computed from illustrative synthetic data, not from a real study.
One-way ANOVA compares the mean of a numeric outcome across three or more groups, while ANCOVA makes the same comparison after adjusting for a covariate such as a pretest score. It suits experimental or quasi-experimental theses comparing treatments, methods or varieties, with the Tukey post hoc test showing which pairs differ.
Report the ANOVA F, degrees of freedom, p-value and eta squared, then the pairs that differ according to Tukey with their differences and confidence intervals. For ANCOVA report the group effect after adjusting for the covariate, partial eta squared, the adjusted group means and the slope homogeneity test.
A short starting script with this project's variable names. The delivered version is adapted to your data and fully commented.
# Contoh kode R: ANOVA, ANCOVA and Tukey Post Hoc Test
suppressPackageStartupMessages({
library(ggplot2)
library(patchwork)
library(car)
library(emmeans)
library(scales)
})
set.seed(42)
# ---- Gaya grafik ----
pal <- c("#1f6feb", "#e8590c", "#2f9e44", "#ae3ec9", "#f59f00")
warna <- list(bg = "#f4f6fa", panel = "#ffffff", teks = "#1f2933", redup = "#5f6b78", grid = "#e3e8ee", bar = "#1d3557")
tata <- 1
theme_set(theme_minimal(base_size = 11) + theme(legend.position = "bottom"))
# ---- Data sintetis: tiga kelompok perlakuan dengan satu kovariat ----
n <- 200
lv <- c("A", "B", "C")
d <- data.frame(feed_type = factor(sample(rep(lv, length.out = n)), levels = lv),
initial_weight = round(rnorm(n, 30, 6), 2))
z2 <- as.numeric(scale(d$initial_weight))
efek <- c(0, runif(1, 0.25, 0.6), runif(1, 0.55, 1.1))[sample(3)] # beda rata-rata antarkelompok (dalam SD)
b_kov <- runif(1, 0.35, 0.7) # pengaruh kovariat
d$final_catfish_weight <- round(100 + 15 * (efek[as.integer(d$feed_type)] - mean(efek) + b_kov * z2 +
rnorm(n, 0, runif(1, 0.55, 0.8))), 2)
write.csv(d, "data_contoh.csv", row.names = FALSE)
# ---- Analisis 1: ANOVA satu arah ----
fit_aov <- aov(final_catfish_weight ~ feed_type, data = d)
tab_aov <- summary(fit_aov)[[1]]
print(tab_aov)
print(aggregate(final_catfish_weight ~ feed_type, data = d, FUN = function(v) c(rerata = mean(v), sd = sd(v), n = length(v))))
# ---- Uji asumsi ANOVA ----
lev <- car::leveneTest(final_catfish_weight ~ feed_type, data = d, center = median) # homogenitas varians (Levene/Brown-Forsythe)
sw <- shapiro.test(residuals(fit_aov)) # normalitas residual
print(lev); print(sw)
eta2 <- tab_aov[1, "Sum Sq"] / sum(tab_aov[, "Sum Sq"]) # eta kuadrat (= partial eta2 pada ANOVA satu arah)
# ---- Uji lanjut Tukey HSD ----
tk <- TukeyHSD(fit_aov, conf.level = 0.95)
print(tk)
tkd <- as.data.frame(tk[[1]])
names(tkd) <- c("selisih", "bawah", "atas", "p_adj")
# ---- Analisis 2: ANCOVA (kovariat initial_weight) ----
fit_slope <- lm(final_catfish_weight ~ initial_weight * feed_type, data = d) # uji homogenitas kemiringan regresi
a_slope <- car::Anova(fit_slope, type = 2)
print(a_slope)
p_slope <- a_slope[3, "Pr(>F)"]
fit_anc <- lm(final_catfish_weight ~ initial_weight + feed_type, data = d)
a_anc <- car::Anova(fit_anc, type = 2)
print(a_anc)
p_anc <- a_anc[2, "Pr(>F)"]
peta2 <- a_anc[2, "Sum Sq"] / (a_anc[2, "Sum Sq"] + a_anc["Residuals", "Sum Sq"]) # partial eta kuadrat kelompok
em <- emmeans::emmeans(fit_anc, specs = "feed_type") # rata-rata terkoreksi kovariat
print(em)
print(pairs(em, adjust = "tukey"))
emd <- as.data.frame(summary(em))
names(emd)[1:6] <- c("kelompok", "rerata", "se", "db", "bawah", "atas")
# ---- Grafik ----
bungkus <- scales::label_wrap(12)
pendek <- function(s, w = 12) ifelse(nchar(s) > w, paste0(trimws(substr(s, 1, w - 1)), "."), s)
rata <- aggregate(final_catfish_weight ~ feed_type, data = d, FUN = mean)
p1 <- ggplot(d, aes(feed_type, final_catfish_weight)) +
geom_boxplot(aes(fill = feed_type), alpha = 0.25, outlier.shape = NA, colour = warna$redup, width = 0.6) +
geom_jitter(aes(colour = feed_type), width = 0.15, alpha = 0.5, size = 1.3) +
geom_point(data = rata, shape = 23, size = 3.2, fill = warna$panel, colour = warna$teks, stroke = 1.1) +
scale_fill_manual(values = pal[1:3], guide = "none") +
scale_colour_manual(values = pal[1:3], guide = "none") +
scale_x_discrete(labels = bungkus) +
labs(title = "Distribution by group (diamond = mean)", x = NULL, y = "final_catfish_weight")
pasangan <- combn(lv, 2)
tkd$pasangan <- factor(paste(pendek(pasangan[2, ]), "-", pendek(pasangan[1, ])),
levels = rev(paste(pendek(pasangan[2, ]), "-", pendek(pasangan[1, ]))))
tkd$nyata <- ifelse(tkd$p_adj < 0.05, "p < 0.05", "n.s.")
p2 <- ggplot(tkd, aes(selisih, pasangan, colour = nyata)) +
geom_vline(xintercept = 0, linetype = "dashed", colour = warna$redup) +
geom_errorbarh(aes(xmin = bawah, xmax = atas), height = 0.25, linewidth = 0.9) +
geom_point(size = 2.8) +
scale_colour_manual(values = c("p < 0.05" = pal[2], "n.s." = warna$redup), name = NULL, drop = FALSE) +
labs(title = "Tukey HSD: mean differences (95% CI)", x = "Difference in final_catfish_weight", y = NULL) +
theme(legend.position = "bottom")
garis <- expand.grid(initial_weight = seq(min(d$initial_weight), max(d$initial_weight), length.out = 30), feed_type = factor(lv, levels = lv))
garis$prediksi <- predict(fit_anc, newdata = garis)
p3 <- ggplot(d, aes(initial_weight, final_catfish_weight, colour = feed_type)) +
geom_point(alpha = 0.45, size = 1.3) +
geom_line(data = garis, aes(y = prediksi), linewidth = 1.1) +
scale_colour_manual(values = pal[1:3], name = NULL, labels = pendek) +
labs(title = "ANCOVA: parallel fitted lines", x = "initial_weight", y = "final_catfish_weight") +
theme(legend.position = "bottom")
mentah <- aggregate(final_catfish_weight ~ feed_type, data = d, FUN = function(v) c(m = mean(v), se = sd(v) / sqrt(length(v))))
dm <- rbind(data.frame(kelompok = factor(lv, levels = lv), rerata = mentah$final_catfish_weight[, "m"],
bawah = mentah$final_catfish_weight[, "m"] - 1.96 * mentah$final_catfish_weight[, "se"],
atas = mentah$final_catfish_weight[, "m"] + 1.96 * mentah$final_catfish_weight[, "se"], jenis = "Raw mean"),
data.frame(kelompok = emd$kelompok, rerata = emd$rerata, bawah = emd$bawah, atas = emd$atas,
jenis = "Adjusted mean"))
dm$jenis <- factor(dm$jenis, levels = c("Raw mean", "Adjusted mean"))
p4 <- ggplot(dm, aes(kelompok, rerata, colour = jenis)) +
geom_pointrange(aes(ymin = bawah, ymax = atas), position = position_dodge(width = 0.5), size = 0.45, linewidth = 0.9) +
scale_colour_manual(values = c("Raw mean" = warna$redup, "Adjusted mean" = pal[4]), name = NULL) +
scale_x_discrete(labels = bungkus) +
labs(title = "Raw vs covariate-adjusted means", x = NULL, y = "final_catfish_weight") +
theme(legend.position = "bottom")
p_hasil <- if (tata == 1) (p1 | p2) / (p3 | p4) else (p1 | p3) / (p4 | p2)
print(p_hasil)
The original project file is available. It is not just the picture above: you get the working R project. It can be downloaded after payment, and we send it by WhatsApp or email.
ZIP: analisis.R + data_contoh.csv + BACA-DULU.txt
Choose how much help you need. Each higher package includes everything in the lower ones. Prices exclude 11% VAT.
Package 1
US$19
excl. 11% VAT
Duration: 3 working days
Output: R files + result figures
Deliverables
Package 2 · most chosen
US$56
excl. 11% VAT
Duration: 7 working days
Output: Code + analysis + interpretation notes
Includes all of Package 1, plus
Package 3
US$122
excl. 11% VAT
Duration: 12-15 working days
Output: Code + analysis + guidance until ready to present
Includes all of Packages 1 & 2, plus
Yes. The original project file is a ZIP containing the complete analisis.R script, a data_contoh.csv sample dataset and a BACA-DULU.txt guide. It can be downloaded after payment, and we send it by WhatsApp or email. For your own data (Package 2 and 3) the script is adapted to your dataset.
Yes. ANOVA, ANCOVA and Tukey Post Hoc Test is commonly used at this level in Fisheries and Marine Science. We adapt the analysis to your research question, your own data and your supervisor's requirements.
No. The sheet is computed from illustrative synthetic data so you can see the kind of output R Studio gives. For your order we run the same method on your own data.
That is the point of the packages. Every script is commented, and Package 3 includes two coaching sessions plus practice questions so you can explain the method and the results at your seminar and defence.
Each project has three packages: Package 1 (code and explanation) US$19, Package 2 (your data analysis and interpretation) US$56 and Package 3 (full guidance) US$122, excluding 11% VAT. Larger datasets, extra methods or tight deadlines adjust the quote after a short consultation.
The result sheet uses illustrative synthetic data. R is a programming language of the R Foundation project; RStudio and Posit are trademarks of Posit PBC.
Back to R Studio portfolio