How to use this document

This document is both a tutorial and an assignment.

  1. Run the code chunks in order, from top to bottom (click the green ▶ button on each chunk, or use Ctrl/Cmd + Shift + Enter).
  2. Read the explanation before and the output after each chunk.
  3. Answer every question marked Q1, Q2, … by typing in the line that starts with > **Your answer:**. Use full sentences and refer to the actual numbers in your output.
  4. Do the independent practice in Part 8.
  5. When finished, click Knit to create an HTML report and submit it as instructed by your lecturer.

Important: because everything uses set.seed(42), your simulated data will be identical to everyone else’s if you run the chunks in order. Do not change the seed or the order unless a question tells you to.

Learning objectives. By the end you should be able to:

  • explain the difference between parametric and non-parametric tests and why assumptions matter;
  • check normality and equal variance using plots and tests;
  • run and interpret One-way ANOVA with Tukey post-hoc tests;
  • run and interpret Kruskal-Wallis with Dunn post-hoc tests;
  • explain what each test is actually doing (variance ratio vs ranks);
  • choose and justify a test for a new dataset.

Part 0. The big picture

0.1 What is a “parameter”?

A parameter is a number that describes a population, such as the mean (\(\mu\)) or the standard deviation (\(\sigma\)).

  • Parametric tests (t-test, ANOVA) assume the data come from a distribution with a known shape (usually normal) and compare parameters (means).
  • Non-parametric tests (Mann-Whitney, Kruskal-Wallis) make fewer assumptions about the shape. Most of them work on the ranks of the data instead of the raw values.
Parametric (One-way ANOVA) Non-parametric (Kruskal-Wallis)
Works with Raw values Ranks of the values
Compares Group means Group rank distributions (often read as medians)
Main assumptions Independence, approx. normal residuals, equal variances Independence, similar shape/spread in all groups
Sensitive to outliers? Yes Much less
Statistic F H (approx. chi-square)
Post-hoc Tukey HSD Dunn’s test
Power when assumptions hold Highest Slightly lower

0.2 Decision workflow used in this assignment

 Look at the data (box plot, histogram, QQ plot)
              |
 Check normality in each group / residuals (QQ plot + Shapiro-Wilk)
              |
        Roughly normal?
        /            \
      YES             NO
       |               |
 Check equal       Consider: transformation (e.g., log)
 variances         or Kruskal-Wallis
 (Levene)               |
   /     \              |
 Equal  Unequal         |
  |       |             |
 ANOVA  Welch ANOVA   Kruskal-Wallis
  |       |             |
 Tukey  Games-Howell   Dunn

0.3 Why ranks? A 1-minute demonstration

Non-parametric tests replace each value by its rank (1 = smallest). Run the chunk below.

x <- c(3, 150, 7, 12, 5)          # one extreme value (150)
x_extreme <- c(3, 15000, 7, 12, 5) # make the extreme value 100x bigger

tibble(
  value        = x,
  rank         = rank(x),
  value_extreme = x_extreme,
  rank_extreme  = rank(x_extreme)
)
cat("Mean of x:", mean(x), " | Mean of x_extreme:", mean(x_extreme), "\n")
## Mean of x: 35.4  | Mean of x_extreme: 3005.4
cat("Median of x:", median(x), " | Median of x_extreme:", median(x_extreme), "\n")
## Median of x: 7  | Median of x_extreme: 7

Q1. What happened to the mean when the extreme value changed from 150 to 15000? What happened to the ranks and the median?

Your answer: The mean changes dramatically from 35.4 to 3005.4, but the rank and median stays the same after the value changed

Q2. Using your answer to Q1, explain in your own words why rank-based tests are described as “robust to outliers”.

Your answer: Because when theres outliers, a rank-based test doesnt care, because rank-based test only use ranks, it doesnt care if the 1st place is having an ultra big number while the others are only small numbers, this makes this type of test results the same even if you have huge gap between your numeric data, hence the name “robust to outliers”

Part 1. Data generation

We simulate two datasets so that we can see both workflows. Simulation is useful for learning because we know the truth.

  • df_normal: three groups drawn from normal distributions (true means 60, 65, 80; same SD = 10).
  • df_skewed: three groups drawn from a gamma distribution, which is right-skewed (long tail to the right), and then multiplied by 10, 15, 20, so the groups differ in both centre and spread.
# A seed makes random numbers reproducible: everyone gets the same "random" data
set.seed(42)
# --- Dataset 1: Normally distributed data ---
df_normal <- tibble(
  Group = factor(rep(c("A", "B", "C"), each = 30)),
  Score = c(rnorm(30, mean = 60, sd = 10),   # Group A
            rnorm(30, mean = 65, sd = 10),   # Group B
            rnorm(30, mean = 80, sd = 10))   # Group C
)

# --- Dataset 2: Right-skewed data ---
# rgamma(shape = 2, rate = 0.5) is a right-skewed distribution.
# Multiplying by a bigger number shifts AND stretches the group.
df_skewed <- tibble(
  Group = factor(rep(c("X", "Y", "Z"), each = 30)),
  Score = c(rgamma(30, shape = 2, rate = 0.5) * 10,  # Group X
            rgamma(30, shape = 2, rate = 0.5) * 15,  # Group Y
            rgamma(30, shape = 2, rate = 0.5) * 20)  # Group Z
)
# Always look at your data first!
summary(df_normal)
##  Group      Score      
##  A:30   Min.   :33.44  
##  B:30   1st Qu.:58.73  
##  C:30   Median :69.56  
##         Mean   :68.79  
##         3rd Qu.:79.06  
##         Max.   :95.13
df_normal
summary(df_skewed)
##  Group      Score        
##  X:30   Min.   :  6.009  
##  Y:30   1st Qu.: 21.600  
##  Z:30   Median : 54.064  
##         Mean   : 58.581  
##         3rd Qu.: 81.877  
##         Max.   :201.283
df_skewed

Q3. How many observations are in each dataset? How many groups? Is the design balanced (equal group sizes)? (Hint: use count(df_normal, Group).)

count(df_normal, Group)

#saya nambahin table count(df_normal, Group)-nya dari hint, biar keliatan kalau tiap sampel punya n yang sama, yaitu 30

Your answer: Each dataset contains 90 observations (rows) divided into 3 groups (Groups A, B, C in df_normal; Groups X, Y, and Z in df_skewed). Yes, the design is balanced because every group has an equal sample size of 30 observations each.

Q4. In df_normal the true population means are 60, 65 and 80. Why might the sample means we compute later be slightly different from these numbers?

Your answer: Because each group only takes a small random sample of 30 values from the population, so random chance (sampling variability) makes the sample averages slightly different from the true means of 60, 65, and 80.


Part 2. Step 1: Explore and visualise

Rule #1 of statistics: plot before you test. Plots show things that p-values hide (outliers, skewness, unequal spread).

2.1 Descriptive statistics

Two useful clues about skewness:

  • In a symmetric distribution, mean ≈ median.
  • In a right-skewed distribution, mean > median (the long tail pulls the mean up).
# A small helper function for skewness (0 = symmetric, >0 = right-skewed)
skew <- function(x) mean((x - mean(x))^3) / (mean((x - mean(x))^2))^1.5

cat("--- df_normal ---\n")
## --- df_normal ---
df_normal %>%
  group_by(Group) %>%
  summarise(n = n(), mean = mean(Score), median = median(Score),
            sd = sd(Score), skewness = skew(Score), .groups = "drop")
cat("\n--- df_skewed ---\n")
## 
## --- df_skewed ---
df_skewed %>%
  group_by(Group) %>%
  summarise(n = n(), mean = mean(Score), median = median(Score),
            sd = sd(Score), skewness = skew(Score), .groups = "drop")

Q5. For which dataset is the mean clearly larger than the median within groups? What does this suggest about the shape?

Your answer: The mean is clearly larger than the median in df_skewed (Groups X, Y, Z: 42.51 vs. 34.86 ; 66.39 vs. 62.53 ; 66.84 vs. 60.22). This shows that the distribution is right-skewed (a long tail of high values pulls the mean above the median).

Q6. Look at the sd column in df_skewed. Does the spread look similar across X, Y and Z? Compare with df_normal. Why is this important for ANOVA? (You will test this formally in Part 3.)

Your answer:No, the spread (SD) in df_skewed is not similar across groups, it increases from Group X (28.80) to Group Y (37.69) to Group Z (48.41). As for the standard deviations (SD) in df_normal, the SD are much closer to each other (12.55, 10.50, and 7.83, each numbers are hovering around the true SD of 10). This is important because standard ANOVA assumes equal variance (homogeneity of variance) across all groups, breaking this assumption in df_skewed can make ANOVA results inaccurate.

2.2 Box plots

A box plot shows the median (middle line), the interquartile range (box) and outliers (points beyond the whiskers).

box_normal <- df_normal %>%
  ggplot(aes(x = Group, y = Score, fill = Group)) +
  geom_boxplot(alpha = 0.7, outlier.shape = NA) +
  geom_jitter(width = 0.1, size = 1, alpha = 0.5) +
  labs(title = "Normal data", y = "Score") +
  theme_minimal() + theme(legend.position = "none")

box_skewed <- df_skewed %>%
  ggplot(aes(x = Group, y = Score, fill = Group)) +
  geom_boxplot(alpha = 0.7, outlier.shape = NA) +
  geom_jitter(width = 0.1, size = 1, alpha = 0.5) +
  labs(title = "Skewed data", y = "Score") +
  theme_minimal() + theme(legend.position = "none")

box_normal + box_skewed

Q7. In the skewed data, is the median line in the middle of the box, or closer to the bottom? Are the upper whiskers longer than the lower ones? What does that tell you?

Your answer:The median line is closer to the bottom in the skewed data, and yes, the upper whiskers are longer than the lower ones, this tells us that the X,Y,Z data here are not normaly distributed, most observations are clustered at lower values but theres a long tail of high values that stretches the upper whisker end of the distribution

2.3 Histograms

hist_normal <- ggplot(df_normal, aes(x = Score, fill = Group)) +
  geom_histogram(alpha = 0.7, bins = 12, color = "white") +
  facet_wrap(~Group) +
  labs(title = "Normal data", y = "Frequency") +
  theme_minimal() + theme(legend.position = "none")

hist_skewed <- ggplot(df_skewed, aes(x = Score, fill = Group)) +
  geom_histogram(alpha = 0.7, bins = 12, color = "white") +
  facet_wrap(~Group) +
  labs(title = "Skewed data", y = "Frequency") +
  theme_minimal() + theme(legend.position = "none")

hist_normal + hist_skewed

Q8. Describe the shape of the histograms in each dataset in one or two sentences. Histograms with only 30 points can look “messy”. Why might the QQ plot (next) be a better tool?

Your answer:The normal data histograms (A, B, C) are roughly symmetric and bell-shaped, while the skewed data histograms (X, Y, Z) are right-skewed, with most values clustered on the left and a long tail tapering off to the right. But to be honest, if I wasnt told which data has normal distribution, I would have thought the skewed data is also normally distributed. With only 30 points, histogram bars look messy and misleading, thats why the QQ plot is better.

2.4 QQ plots

A QQ (quantile-quantile) plot compares your data with what a perfect normal distribution would look like.

  • Points follow the dashed line → consistent with normality.
  • Points curve away (especially an upward bend at the right end) → skewness or outliers.
qq_normal <- ggplot(df_normal, aes(sample = Score, color = Group)) +
  stat_qq(size = 1) + stat_qq_line(linetype = "dashed") +
  facet_wrap(~Group) +
  labs(title = "QQ plot: normal data", x = "Theoretical quantiles", y = "Sample quantiles") +
  theme_minimal() + theme(legend.position = "none")

qq_skewed <- ggplot(df_skewed, aes(sample = Score, color = Group)) +
  stat_qq(size = 1) + stat_qq_line(linetype = "dashed") +
  facet_wrap(~Group) +
  labs(title = "QQ plot: skewed data", x = "Theoretical quantiles", y = "Sample quantiles") +
  theme_minimal() + theme(legend.position = "none")

qq_normal + qq_skewed

Q9. In which dataset do the points stay close to the dashed line? In the other dataset, describe how the points deviate from the line (e.g., which end curves up or down).

Your answer: The points stay closed to the dashed line on the normal data, meanwhile the skewed data has its points curves up above the dashed line on both ends


Part 3. Step 2: Check the assumptions formally

Plots give an impression; tests give a number. Use both.

3.1 Normality: Shapiro-Wilk test

\[H_0: \text{the data in this group come from a normal distribution}\] \[H_1: \text{the data are not normally distributed}\]

Decision rule (α = 0.05): if \(p > 0.05\) we fail to reject \(H_0\) (no evidence against normality). If \(p < 0.05\) we reject \(H_0\) (evidence of non-normality).

Notice the unusual logic: for assumption checks we hope for a large p-value.

cat("--- Normal data ---\n")
## --- Normal data ---
df_normal %>% group_by(Group) %>% shapiro_test(Score)
cat("--- Skewed data ---\n")
## --- Skewed data ---
df_skewed %>% group_by(Group) %>% shapiro_test(Score)

Q10. Fill in the table using your output (write “reject” or “fail to reject”, and “normal” or “not normal”).

Dataset Group p-value Decision on H0 Conclusion
normal A 0.35000461 fail to reject normal
normal B 0.06386228 fail to reject normal
normal C 0.27350724 fail to reject normal
skewed X 0.01286251 reject not normal
skewed Y 0.39771287 fail to reject normal
skewed Z 0.01373573 reject not normal

Q11. “Failing to reject H0 proves the data are normal.” Is this statement correct? Explain.

Your answer:No it isnt correct, failing to reject H0 only means there is not enough statistical evidence to conclude that the data deviates from normality, it does not prove that the population is truly normal

3.2 Normality tests depend on sample size

Shapiro-Wilk is not perfect. The chunk below samples from the skewed gamma distribution at different sample sizes and records how often Shapiro-Wilk correctly detects non-normality (p < 0.05). This is the test’s power.

set.seed(1)
sizes <- c(5, 10, 20, 30, 50, 100, 300)

power_tbl <- tibble(
  n = sizes,
  prop_detected = map_dbl(sizes, ~ mean(replicate(500, {
    shapiro.test(rgamma(.x, shape = 2, rate = 0.5))$p.value < 0.05
  })))
)
power_tbl
ggplot(power_tbl, aes(x = n, y = prop_detected)) +
  geom_line() + geom_point(size = 2) +
  geom_hline(yintercept = 0.05, linetype = "dashed") +
  scale_x_log10() +
  labs(x = "Sample size per group (log scale)",
       y = "Proportion of samples flagged as non-normal",
       title = "Shapiro-Wilk detection rate for skewed data") +
  theme_minimal()

Q12. With n = 5 or 10, how often does Shapiro-Wilk detect that the gamma data are non-normal? What about n = 100 or more? What lesson does this teach about relying on a normality test alone with small samples?

Your answer:With n = 5, the Shapiro-Wilk test detects non-normality only 9.2% of the time (0.092), and with n = 10, only 22.4% of the time (0.224). In contrast, with n = 100 or more (n = 100 and n = 300), it detects non-normality 100% of the time (1.000). This teaches us that with small sample sizes, the Shapiro-Wilk test has very low statistical power and frequently fails to reject H₀ even when data are actually skewed (low accuracy), so we should not rely on a normality test alone especially if we only had few samples.

3.3 Equal variances: Levene’s test

ANOVA also assumes the spread is the same in every group (homogeneity of variance, or homoscedasticity).

\[H_0: \sigma^2_1 = \sigma^2_2 = \sigma^2_3 \qquad H_1: \text{at least one variance differs}\]

Again, for ANOVA we hope for \(p > 0.05\).

cat("--- Normal data ---\n")
## --- Normal data ---
leveneTest(Score ~ Group, data = df_normal)
cat("\n--- Skewed data ---\n")
## 
## --- Skewed data ---
leveneTest(Score ~ Group, data = df_skewed)

Q13. Report the Levene’s p-value for each dataset and state whether the equal-variance assumption is met.

Your answer:Both datasets pass the equal-variance check, because both p-values (0.2431 and 0.1472) are greater than 0.05, there’s no statistically significant evidence that the spread (variance) differs among three groups of each datasets.

3.4 Decision log (assumption summary)

Q14. Based on Parts 2 and 3, complete this decision log.

df_normal df_skewed
Normality OK? (plots + Shapiro) YES NO
Equal variances OK? (Levene) YES YES
Chosen test One-way ANOVA Kruskal-Wallis test
One-sentence justification normal distribution and equal variances assumptions are met data is not normally distributed but the equal variances assumption is met

Part 4. Parametric workflow: One-way ANOVA

We use df_normal.

4.1 Hypotheses

\[H_0: \mu_A = \mu_B = \mu_C \qquad H_1: \text{at least one mean is different}\]

Note that ANOVA is an omnibus test: it tells you whether any group differs, not which one.

4.2 How does ANOVA work? Build the F statistic by hand

The idea: compare the variation between group means to the variation within groups.

\[F = \frac{MS_{between}}{MS_{within}} = \frac{SS_{between}/(k-1)}{SS_{within}/(N-k)}\]

  • Large F → group means are far apart compared with the natural noise inside groups → evidence against \(H_0\).
  • F near 1 → differences between groups are about the size of random noise.
k <- nlevels(df_normal$Group)     # number of groups
N <- nrow(df_normal)              # total sample size
grand_mean <- mean(df_normal$Score)

group_stats <- df_normal %>%
  group_by(Group) %>%
  summarise(n = n(), mean = mean(Score), var = var(Score), .groups = "drop")
group_stats
SS_between <- sum(group_stats$n * (group_stats$mean - grand_mean)^2)
SS_within  <- sum((group_stats$n - 1) * group_stats$var)

MS_between <- SS_between / (k - 1)
MS_within  <- SS_within / (N - k)

F_manual <- MS_between / MS_within
p_manual <- pf(F_manual, df1 = k - 1, df2 = N - k, lower.tail = FALSE)

cat("SS between:", round(SS_between, 2), " | SS within:", round(SS_within, 2), "\n")
## SS between: 7885.25  | SS within: 9544.41
cat("MS between:", round(MS_between, 2), " | MS within:", round(MS_within, 2), "\n")
## MS between: 3942.63  | MS within: 109.71
cat("F =", round(F_manual, 3), " | p =", format.pval(p_manual, digits = 3), "\n")
## F = 35.938  | p = 4.2e-12

Q15. Which is larger, MS between or MS within? What does the size of their ratio (F) tell you about the groups?

Your answer: MS between is much larger than MS within (3942.63 > 109.71 ). The ratio (35.938), tells us that the variation between the group means is roughly 36 times greater than the random variation within the groups.Theres a strong evidence that at least one group mean is significantly different from the others.

Q16. What are the degrees of freedom of the numerator and denominator in this analysis? Show how you got them from k and N.

Your answer:Numerator (df between)= K-1 = 3-1 = 2 ; Denominator (df within)= N-K = 90 - 3 = 87

4.3 Run the ANOVA with R

anova_result <- aov(Score ~ Group, data = df_normal)
summary(anova_result)
##             Df Sum Sq Mean Sq F value  Pr(>F)    
## Group        2   7885    3943   35.94 4.2e-12 ***
## Residuals   87   9544     110                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Q17. Compare the F value and p-value from summary(anova_result) with your manual calculation above. Are they the same? State your statistical decision and conclusion in plain English.

Your answer: The values are roughly similar (my calc (35.85 ) vs R calc(35.94)), but this happens due to difference in rounding

statistical decision = p-value (4.2 x 10^-12) is much smaller than the significance level (alpha = 0.05), we reject the null hypothesis (H0).

Conclusion: There is statistically significant evidence that the group means are not all equal, at least one group’s mean differs significantly from the others.

4.4 Effect size: how big is the difference?

A tiny p-value does not mean a large effect. Eta-squared (\(\eta^2\)) is the proportion of total variance explained by the grouping.

\[\eta^2 = \frac{SS_{between}}{SS_{between} + SS_{within}}\]

Rough guide: 0.01 small, 0.06 medium, 0.14 large.

eta_sq <- SS_between / (SS_between + SS_within)
cat("Eta-squared =", round(eta_sq, 3), "\n")
## Eta-squared = 0.452

Q18. What percentage of the variation in Score is explained by Group? Is this a small, medium or large effect?

Your answer:45.2% of the variation in Score is explained by Group (eta^2 = 0.452). Since (0.452 > 0.14), this is a large effect.

4.5 Check the residuals

ANOVA’s assumptions are really about the residuals (each value minus its group mean). It is good practice to check them after fitting the model.

par(mfrow = c(1, 2))
plot(anova_result, which = 1)   # Residuals vs fitted: look for equal spread
plot(anova_result, which = 2)   # Normal QQ plot of residuals

par(mfrow = c(1, 1))

shapiro_test(residuals(anova_result))

Q19. In the “Residuals vs Fitted” plot, is the vertical spread roughly the same for the three groups? Are the residuals roughly normal? Do the residual checks agree with Part 3?

Your answer:Yes, the vertical spread is roughly the same across the three groups (the third group is just slightly narrower, which is fine). In the Q-Q plot, the points stay close to the diagonal line, showing that the residuals are roughly normal. These checks agree with Part 3 because both confirm that the normality and equal variance assumptions are met.

4.6 Post-hoc: Tukey HSD

Because the ANOVA was significant, we ask which pairs differ. Running many separate t-tests inflates the false-positive rate (with 3 groups and 3 comparisons, the chance of at least one false positive at α = 0.05 is about 14%). Tukey’s HSD controls this family-wise error.

tukey_result <- TukeyHSD(anova_result)
tukey_result
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Score ~ Group, data = df_normal)
## 
## $Group
##          diff       lwr       upr    p adj
## B-A  3.095047 -3.353515  9.543608 0.489563
## C-A 21.221824 14.773263 27.670385 0.000000
## C-B 18.126777 11.678216 24.575339 0.000000
# Confidence intervals that do NOT cross zero indicate significant differences
plot(tukey_result)

Q20. Which pairs of groups differ significantly? Give the estimated mean difference and the adjusted p-value for each pair. Do the results match the true means (60, 65, 80)?

Your answer: C-A (mean difference = 21.22, adjusted p-value = 0.000000) and C-B (mean difference = 18.13, adjusted p-value = 0.000000) differ significantly, while B-A does not differ significantly (mean difference = 3.10, adjusted p-value = 0.4896). The estimated differences closely match the true population differences calculated from the given true means (80 - 60 = 20 for C-A, 80 - 65 = 15 for C-B, and 65 - 60 = 5 for B-A), though the 5 point difference between A (60) and B (65) is too small relative to sample variability to be detected as statistically significant.

Q21. Why can’t we simply say “the ANOVA was significant, so all three groups differ from one another”?**

Your answer: ANOVA is an omnibus test that only tells us that at least one group mean differs from the others, not that every pair differs. As shown by the Tukey HSD results, Group C differs significantly from both A and B, whereas Groups A and B do not significantly differ from each other (p(adj)=0.49).

4.7 (Optional) All-in-one plot

ggstatsplot::ggbetweenstats(
  data = df_normal, x = Group, y = Score,
  type = "parametric", var.equal = TRUE,
  pairwise.display = "all",
  centrality.plotting = TRUE, bf.message = FALSE
)

###Saya ubah centrality.plotting = FALSE => TRUE agar mean-nya terlihat dan bisa hitung mean difference, saya juga ubah pairwise.display = Significant => all agar P-value A-B juga terlihat

4.8 Write up the result

Q22. Write a 2-3 sentence results paragraph in the style below, using your own numbers.

Example format: “A one-way ANOVA showed a significant effect of group on score, F(df1, df2) = , p = , η² = . Tukey’s HSD indicated that differed from ___ (mean difference = , p = ), while ___.”

Your answer: A one-way ANOVA showed a significant effect of group on score, F(2, 55.8) = 44.74, p = 2.54e-12, omega-squared = 0.60. Tukey’s HSD indicated that Group C differed significantly from both Group A (mean difference = 21.22, p = 2.98x10^-9) and Group B (mean difference = 18.13, p = 2.98x10^-9 ), while Group B and Group A did not differ significantly from each other (mean difference = 3.09, adjusted p = 0.56).


Part 5. Non-parametric workflow: Kruskal-Wallis H test

We use df_skewed (skewed data, unequal spreads).

5.1 Hypotheses

\[H_0: \text{the distributions of X, Y and Z are the same (no group tends to give higher values)}\] \[H_1: \text{at least one group tends to give higher (or lower) values than another}\]

If the groups have similar shapes, this can be read as a comparison of medians.

5.2 How does Kruskal-Wallis work? Build H by hand

  1. Pool all observations and rank them from 1 to N.
  2. Add up the ranks for each group (\(R_i\)).
  3. If \(H_0\) is true, each group’s mean rank should be about the same (around \((N+1)/2\)).

\[H = \frac{12}{N(N+1)} \sum_{i=1}^{k} \frac{R_i^2}{n_i} - 3(N+1)\]

H is compared with a chi-square distribution with \(k-1\) degrees of freedom.

df_ranked <- df_skewed %>% mutate(Rank = rank(Score))

rank_summary <- df_ranked %>%
  group_by(Group) %>%
  summarise(n = n(), rank_sum = sum(Rank), mean_rank = mean(Rank), .groups = "drop")
rank_summary
N_s <- nrow(df_skewed)
k_s <- nlevels(df_skewed$Group)

H_manual <- 12 / (N_s * (N_s + 1)) * sum(rank_summary$rank_sum^2 / rank_summary$n) -
  3 * (N_s + 1)
p_H_manual <- pchisq(H_manual, df = k_s - 1, lower.tail = FALSE)

cat("Expected mean rank under H0 =", (N_s + 1) / 2, "\n")
## Expected mean rank under H0 = 45.5
cat("H =", round(H_manual, 3), " | p =", format.pval(p_H_manual, digits = 3), "\n")
## H = 6.31  | p = 0.0426
ggplot(df_ranked, aes(x = Group, y = Rank, fill = Group)) +
  geom_boxplot(alpha = 0.7) +
  geom_hline(yintercept = (N_s + 1) / 2, linetype = "dashed") +
  labs(title = "Ranks of the skewed data (dashed line = expected mean rank under H0)") +
  theme_minimal() + theme(legend.position = "none")

Q23. What is the mean rank of each group? How far are they from the expected mean rank under \(H_0\)? What does that tell you about X, Y and Z?

Your answer:The mean ranks are 35.83 for Group X, 51.63 for Group Y, and 49.03 for Group Z. Compared to the expected mean rank under H0 of 45.5, Group X is noticeably lower by 9.67 rank units (35.83 - 45.5 = -9.67), while Group Y is higher by 6.13 units (51.63 - 45.5 = +6.13) and Group Z is higher by 3.53 units (49.03 - 45.5 = +3.53). This tells us that Group X tends to have lower values overall than Groups Y and Z, while Groups Y and Z have similar distributions of higher ranks (and with H = 6.31, p = 0.0426, at least one group differs significantly from the others).

Q24. After ranking, are the extreme high values in the skewed data still “extreme”? Link your answer to Q1-Q2.

Your answer:No, after ranking, the extreme high values in the skewed data are no longer “extreme”. Converting raw scores into ranks from 1 to 90 compresses extreme outliers so that the highest value simply receives rank 90—only 1 unit above the second-highest value (rank 89) regardless of how huge the raw gap was. Linking back to Q1-Q2, while raw extreme outliers heavily inflate the group mean and standard deviation (making parametric ANOVA unreliable), ranking neutralizes their huge gaps so the Kruskal-Wallis test is robust against skewness and outliers.

5.3 Run the Kruskal-Wallis test with R

kruskal_result <- df_skewed %>% kruskal_test(Score ~ Group)
kruskal_result

Q25. Complete the sentence using your output: “The Kruskal-Wallis test gave H(____) = ____, p = ____. We (reject / fail to reject) \(H_0\). There (is / is not) a statistically significant difference in score distributions across the three groups.” Does the value of H match your manual calculation (small differences can arise from tie corrections)?

Your answer:The Kruskal-Wallis test gave H(2) = 6.309744, p = 0.0426. We reject H0. There is a statistically significant difference in score distributions across the three groups. Yes, the value of H (6.309744) matches the manual calculation from the previous step (6.31), as there were no tied ranks in the continuous data requiring tie adjustments.

5.4 Effect size

For Kruskal-Wallis, rstatix provides an eta-squared based on H (same rough guide as before: 0.01 small, 0.06 medium, 0.14 large).

df_skewed %>% kruskal_effsize(Score ~ Group)

Q26. How large is the effect? Is it consistent with the way we generated the data (multipliers of 10, 15, 20)?

Your answer: The effect size is small (eta-squared =0.0495), meaning group membership explains only about 5% of the variance in ranks. Yes, this is consistent with how the skewed data were generated (multipliers of 10, 15, and 20): because skewed distributions (such as exponential or gamma distributions) have large right tails and high within-group spread/overlap relative to the differences in scale multipliers, group differences account for a relatively small proportion of overall rank variation.

5.5 Post-hoc: Dunn’s test

Dunn’s test compares every pair using the rank information, with an adjustment for multiple comparisons (here Bonferroni, which multiplies each p-value by the number of comparisons; it is conservative).

dunn_result <- df_skewed %>%
  dunn_test(Score ~ Group, p.adjust.method = "holm")
dunn_result

Q27. Which pairs differ significantly (look at p.adj)? Is the pattern what you expect from the way the data were generated? Note: with only 30 observations per group and heavily overlapping skewed distributions, some neighbouring groups may not be significantly different. Explain why.

Your answer:None of the pairs differ significantly because all adjusted p-values are greater than 0.05 and marked “ns” (p.adj = 0.0575 for X-Y, p.adj = 0.1511 for X-Z, and p.adj = 1.0000 for Y-Z). Partially: based on how the data were generated (multipliers of 10, 15, and 20), we would expect at least the two farthest groups (X and Z) to differ significantly. However, because skewed data spreads out widely, the three groups overlap heavily, and with only 30 observations per group it is hard to separate them clearly. On top of that, the overall Kruskal-Wallis test was only barely significant (p = 0.0426), and the strict Bonferroni adjustment multiplies each raw p-value by 3 to prevent false positives, pushing every pair above the 0.05.

Q28. Re-run the chunk above with p.adjust.method = "none" and then with "holm". How do the adjusted p-values change? Why is adjusting necessary?

Your answer:With p.adjust.method = “none”, no correction is applied, so p.adj is identical to the raw p-values (0.0192, 0.0504, and 0.6999) and the first pair (X-Y) appears statistically significant. With p.adjust.method = “holm”, the adjusted p-values increase to 0.0575, 0.1007, and 0.6999, making all three pairs non-significant (ns)—though Holm is less harsh than Bonferroni on the larger p-values. Adjusting p-values is necessary because running multiple pairwise tests on the same dataset increases the overall chance of getting a false positive (finding a “significant” difference purely by random luck), adjusting controls this error rate so the overall false-alarm risk stays at 5%.

5.6 (Optional) All-in-one plot

ggstatsplot::ggbetweenstats(
  data = df_skewed, x = Group, y = Score,
  type = "nonparametric",
  p.adjust.method = "bonferroni",
  pairwise.display = "significant",
  centrality.plotting = FALSE, bf.message = FALSE
)


Part 6. What happens if we choose the “wrong” test?

Now we deliberately swap the tests to see the consequences.

6.1 Swap the tests on the same data

cat("=== ANOVA on SKEWED data ===\n")
## === ANOVA on SKEWED data ===
summary(aov(Score ~ Group, data = df_skewed))
##             Df Sum Sq Mean Sq F value Pr(>F)  
## Group        2  11625    5813   3.796 0.0263 *
## Residuals   87 133230    1531                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
cat("\n=== Kruskal-Wallis on NORMAL data ===\n")
## 
## === Kruskal-Wallis on NORMAL data ===
df_normal %>% kruskal_test(Score ~ Group)

Q29. Compare these results with the “correct” tests in Parts 4 and 5. Do the conclusions change in this particular sample? Does a similar conclusion in one sample prove that the choice of test does not matter? (Think about what could change with a different random sample, or with a smaller sample.)

Your answer:No, the conclusions do not change in this particular sample: both tests still reject H0 (p < 0.05) for both datasets (on the normal data, Kruskal-Wallis gives H = 43.62, p = 3.37e-10, matching ANOVA’s significant result; on the skewed data, ANOVA also gives p < 0.05, matching Kruskal-Wallis’s p = 0.0426). However, getting the same decision in one sample does not mean the choice of test does not matter. With a different random sample or a smaller sample size, using the wrong test can lead to false conclusions: ANOVA on skewed data can be heavily distorted by extreme outliers (inflating variance and reducing power or causing false positives), while Kruskal-Wallis on normal data discards exact values in favor of ranks and has lower statistical power to detect real differences when sample sizes are small.

6.2 Simulation: Type I error and power

A Type I error is rejecting \(H_0\) when it is actually true (false positive). Power is the probability of rejecting \(H_0\) when it is false. We repeat the experiment 1000 times with small samples (n = 10 per group) from the skewed gamma distribution:

  • Scenario 1 (H0 true): all three groups have the same distribution. A good test rejects about 5% of the time.
  • Scenario 2 (H0 false): the groups are scaled by 1, 1.5 and 2.
set.seed(2024)

run_sim <- function(n_sim = 1000, n = 10, scales = c(1, 1, 1)) {
  g <- factor(rep(seq_along(scales), each = n))
  res <- replicate(n_sim, {
    y <- unlist(lapply(scales, function(s) rgamma(n, shape = 2, rate = 0.5) * s))
    c(ANOVA = summary(aov(y ~ g))[[1]][["Pr(>F)"]][1],
      Kruskal_Wallis = kruskal.test(y ~ g)$p.value)
  })
  rowMeans(res < 0.05)
}

sim_results <- bind_cols(
  Scenario = c("H0 true (Type I error rate)", "H0 false (power)"),
  bind_rows(
    run_sim(scales = c(1, 1, 1)),
    run_sim(scales = c(1, 1.5, 2))
  )
)
sim_results

Q30. (a) In Scenario 1, how close are the two rejection rates to 0.05? (b) In Scenario 2, which test rejects \(H_0\) more often (has higher power) for skewed data? (c) Many textbooks say ANOVA is “fairly robust” to non-normality when group sizes are equal. Does your simulation support or challenge this? Explain in 3-4 sentences.

Your answer: In Scenario 1 (H0 true), both rejection rates are very close to the nominal 0.05 level: ANOVA rejects H0 4.1% of the time (0.041) and Kruskal-Wallis rejects H0 4.7% of the time (0.047), meaning both control false positives well. (b) In Scenario 2 (H0 false), ANOVA rejects H0 slightly more often than Kruskal-Wallis (0.354 vs. 0.351), though their power is virtually identical at around 35%. (c) This simulation generally supports the textbook claim that ANOVA is fairly robust to non-normality when group sizes are equal: even with skewed gamma data and n = 10 per group, ANOVA maintains a proper Type I error rate (0.041, close to 0.05) without inflated false positives and matches Kruskal-Wallis in power. However, this robustness applies to moderate skewness with balanced group sizes; with more extreme skewness, heavy outliers, or unequal sample sizes, Kruskal-Wallis remains the safer choice.**


Part 7. Middle paths: when assumptions are only partly violated

Real data is rarely perfectly normal or perfectly skewed. Two common options:

  1. Transform the data (e.g., log) so that it is closer to normal, and then use ANOVA.
  2. Welch’s ANOVA (+ Games-Howell post-hoc) when the data are fine for normality but the variances are unequal.

7.1 Log-transformation

df_skewed <- df_skewed %>% mutate(log_Score = log(Score))

qq_log <- ggplot(df_skewed, aes(sample = log_Score, color = Group)) +
  stat_qq(size = 1) + stat_qq_line(linetype = "dashed") +
  facet_wrap(~Group) +
  labs(title = "QQ plot after log transformation") +
  theme_minimal() + theme(legend.position = "none")
qq_log

df_skewed %>% group_by(Group) %>% shapiro_test(log_Score)
leveneTest(log_Score ~ Group, data = df_skewed)

Q31. Did the log transformation improve normality and equal variance? Look at the QQ plots, Shapiro-Wilk and Levene’s test. Why does a log transformation often help right-skewed data?

Your answer:Yes, the log transformation improved both normality overall and equal variance across the groups. In the QQ plots, the points for Groups X, Y, and Z now follow the diagonal reference lines much more closely. In the Shapiro-Wilk test, Groups X (p = 0.0823) and Z (p = 0.1769) now pass normality (p > 0.05, improved from 0.0129 and 0.0137), while Group Y is borderline at p = 0.0346 due to a slight left-tail dip after transformation. In Levene’s test, equal variance clearly improved as the F-statistic dropped from 1.9585 to 0.5546 and the p-value increased from 0.1472 to 0.5763 (p > 0.05), showing much more uniform spread across groups. A log transformation helps right-skewed data because it compresses large values in the long right tail much more than small values—pulling extreme high scores closer to the center to create symmetry—and converts multiplicative scale differences (such as multipliers of 10, 15, and 20) into additive ones, which stabilizes variance across groups.

7.2 Welch’s ANOVA and Games-Howell

Welch’s ANOVA does not require equal variances.

df_skewed %>% welch_anova_test(Score ~ Group)
df_skewed %>% games_howell_test(Score ~ Group)

Q32. Compare Welch’s ANOVA with the classical ANOVA and with Kruskal-Wallis on df_skewed. Are the conclusions the same? When would you prefer Welch’s ANOVA over Kruskal-Wallis?

Your answer: Yes, the overall conclusions are the same across all three tests on df_skewed: Welch’s ANOVA (F(2, 55.62) = 5.00, p = 0.01), classical ANOVA (p < 0.05), and Kruskal-Wallis (H(2) = 6.31, p = 0.0426) all reject H0 and find a statistically significant difference among the groups (notably, Welch’s follow-up Games-Howell test also detects a significant difference between Groups X and Y at p.adj = 0.0213, whereas Dunn’s test with Bonferroni was slightly above 0.05). You would prefer Welch’s ANOVA over Kruskal-Wallis when the main assumption violated is equal variance (heteroscedasticity) rather than severe skewness or extreme outliers—for example, when data are roughly normal (or sample sizes are reasonably large) but groups have very different spreads, and you want to compare actual group means rather than converting data into ranks.


Part 8. Independent practice with real data

Now apply the workflow to two built-in R datasets. Write your own code in the chunks (the chunks are set to eval = FALSE; change to eval = TRUE once you have written your code).

For each dataset, you must:

  1. Make a box plot and QQ plot.
  2. Compute descriptive statistics per group.
  3. Run Shapiro-Wilk (per group) and Levene’s test.
  4. Fill in a decision log and choose a test (justify it).
  5. Run the main test and the correct post-hoc test.
  6. Report effect size and write a results paragraph.

8.1 Dataset A: PlantGrowth

Dry weight of plants (weight) under a control and two treatment conditions (group).

data("PlantGrowth")
glimpse(PlantGrowth)
## Rows: 30
## Columns: 2
## $ weight <dbl> 4.17, 5.58, 5.18, 6.11, 4.50, 4.61, 5.17, 4.53, 5.33, 5.14, 4.8…
## $ group  <fct> ctrl, ctrl, ctrl, ctrl, ctrl, ctrl, ctrl, ctrl, ctrl, ctrl, trt…
# --- Step 1: Visualise ---
# YOUR CODE HERE# --- Step 1: Visualise (box plot + QQ plot) ---
ggplot(PlantGrowth, aes(x = group, y = weight, fill = group)) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.15, alpha = 0.6) +
  labs(title = "Plant Dry Weight by Group", x = "Group", y = "Weight") +
  theme_minimal() +
  theme(legend.position = "none")

ggplot(PlantGrowth, aes(sample = weight, color = group)) +
  stat_qq() +
  stat_qq_line() +
  facet_wrap(~ group) +
  labs(title = "QQ Plot of Plant Weight by Group") +
  theme_minimal() +
  theme(legend.position = "none")

# --- Step 2: Descriptives ---
# YOUR CODE HERE
# --- Step 2: Descriptives ---
PlantGrowth %>%
  group_by(group) %>%
  summarise(
    n = n(),
    mean = mean(weight),
    sd = sd(weight),
    median = median(weight),
    IQR = IQR(weight)
  )
# --- Step 3: Assumptions ---
# Shapiro-Wilk per group:
PlantGrowth %>%
  group_by(group) %>%
  shapiro_test(weight)
# Levene's test:
leveneTest(weight ~ group, data = PlantGrowth)
# --- Step 4: Main test (choose ANOVA / Welch / Kruskal-Wallis) ---
anova_pg <- aov(weight ~ group, data = PlantGrowth)
summary(anova_pg)
##             Df Sum Sq Mean Sq F value Pr(>F)  
## group        2  3.766  1.8832   4.846 0.0159 *
## Residuals   27 10.492  0.3886                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# --- Step 5: Post-hoc test (only if the main test is significant) ---
TukeyHSD(anova_pg)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = weight ~ group, data = PlantGrowth)
## 
## $group
##             diff        lwr       upr     p adj
## trt1-ctrl -0.371 -1.0622161 0.3202161 0.3908711
## trt2-ctrl  0.494 -0.1972161 1.1852161 0.1979960
## trt2-trt1  0.865  0.1737839 1.5562161 0.0120064
# --- Step 6: Effect size (Saya tambahkan sendiri untuk cari nilai eta_squared) ---
eta_squared(anova_pg)
##     group 
## 0.2641483

Q33. Decision log for PlantGrowth (assumptions, chosen test, justification) and your results paragraph, including an effect size.

**Your answer:

Check / Decision PlantGrowth Result
Normality OK? (QQ plot + Shapiro-Wilk) Yes (points follow the QQ line closely; Shapiro-Wilk p = 0.747 for ctrl, p = 0.452 for trt1, and p = 0.564 for trt2, all p > 0.05)
Equal variances OK? (Levene’s test) Yes (Levene’s test F(2, 27) = 1.119, p = 0.341 > 0.05)
Chosen test Classical One-Way ANOVA (with Tukey’s HSD post-hoc test)
Justification Both the normality assumption (all group Shapiro-Wilk p > 0.05) and homogeneity of variance assumption (Levene’s p = 0.341) are satisfied, making parametric one-way ANOVA the most powerful and appropriate test.

Results paragraph: A one-way ANOVA showed a statistically significant effect of treatment condition on plant dry weight, F(2, 27) = 4.85, p = 0.0159, eta-squared = 0.264 (large effect, explaining 26.4% of the variance in weight). Tukey’s HSD post-hoc test indicated that plants in trt2 had significantly higher dry weight than those in trt1 (mean difference = 0.865, adjusted p = 0.0120), whereas neither trt1 (mean difference = -0.371, adjusted p = 0.3909) nor trt2 (mean difference = 0.494, adjusted p = 0.1980) differed significantly from the control group (ctrl).

8.2 Dataset B: InsectSprays

The number of insects (count) found in agricultural plots treated with six different insecticides (spray). Count data often behave differently from continuous data!

data("InsectSprays")
glimpse(InsectSprays)
## Rows: 72
## Columns: 2
## $ count <dbl> 10, 7, 20, 14, 14, 12, 10, 23, 17, 20, 14, 13, 11, 17, 21, 11, 1…
## $ spray <fct> A, A, A, A, A, A, A, A, A, A, A, A, B, B, B, B, B, B, B, B, B, B…
InsectSprays %>% count(spray)
# --- Step 1: Visualise ---
ggplot(InsectSprays, aes(x = spray, y = count, fill = spray)) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.15, alpha = 0.6) +
  labs(title = "Insect Count by Spray Type", x = "Spray", y = "Count") +
  theme_minimal() +
  theme(legend.position = "none")

ggplot(InsectSprays, aes(sample = count, color = spray)) +
  stat_qq() +
  stat_qq_line() +
  facet_wrap(~ spray) +
  labs(title = "QQ Plot of Insect Count by Spray") +
  theme_minimal() +
  theme(legend.position = "none")

# --- Step 2: Descriptives (compare group means and SDs) ---
InsectSprays %>%
  group_by(spray) %>%
  summarise(
    n = n(),
    mean = mean(count),
    sd = sd(count),
    median = median(count),
    IQR = IQR(count)
  )
# --- Step 3: Assumptions ---
InsectSprays %>%
  group_by(spray) %>%
  shapiro_test(count)
leveneTest(count ~ spray, data = InsectSprays)
# --- Step 4: Main test ---
InsectSprays %>% kruskal_test(count ~ spray)
InsectSprays %>% kruskal_effsize(count ~ spray)
# --- Step 5: Post-hoc test ---
InsectSprays %>% dunn_test(count ~ spray, p.adjust.method = "bonferroni")

Q34. Decision log for InsectSprays, the test you chose, and your results paragraph. Which sprays are most / least effective according to your post-hoc results?

Your answer: | Check / Decision | InsectSprays Result | |—|—| | Normality OK? (QQ plot + Shapiro-Wilk) | No (count data show right skewness and discrete zeros/ties in QQ plots; Shapiro-Wilk fails for Spray C with p = 0.044 < 0.05 and is borderline for Spray D with p = 0.068) | | Equal variances OK? (Levene’s test) | No (groups with higher mean counts like A, B, and F have much larger SDs [4.27-6.21] than C, D, and E [1.73-2.50]; Levene’s test F(5, 66) = 3.82, p = 0.0042 < 0.05) | | Chosen test | Kruskal-Wallis test (with Dunn’s post-hoc test using Bonferroni adjustment) | | Justification | Because this is discrete count data that violates both normality (Spray C Shapiro-Wilk p = 0.044) and homogeneity of variance (Levene’s p = 0.0042), non-parametric Kruskal-Wallis is the appropriate choice. |

Results paragraph: A Kruskal-Wallis test showed a statistically significant difference in insect counts across the six insecticide sprays, H(5) = 54.69, p = 1.51e-10, with a large effect size (eta-squared = 0.753, explaining 75.3% of rank variance). Dunn’s post-hoc comparisons with Bonferroni adjustment revealed two clear clusters: Sprays C (median = 1.5), E (median = 3.0), and D (median = 5.0) had significantly lower insect counts than Sprays A (median = 14.0), B (median = 16.5), and F (median = 15.0) (all pairwise p.adj < 0.05 for comparisons between the {C, D, E} group and the {A, B, F} group, except D vs. A which was borderline at p.adj = 0.060), while no significant differences were found within {C, D, E} or within {A, B, F} (all p.adj = 1.00).

Most / least effective sprays: Because lower insect counts mean fewer surviving pests, Sprays C, E, and D are the most effective insecticides (with Spray C having the lowest overall counts), whereas Sprays A, B, and F are the least effective (leaving roughly 3 to 10 times as many insects alive).

Q35. Compare the two datasets. Which one pushed you towards a non-parametric (or alternative) approach, and which diagnostic revealed the problem most clearly?

Your answer: The one that pushed me towards non parametric approach are the second dataset (insect spray), the diagnostic that revealed the problem most clearly is the Shapiro-Wilk, because it shows that the data is not normally distributed.


Part 9. Reflection

Q36. Complete the comparison in your own words

One-way ANOVA Kruskal-Wallis
What is being compared? Group means Group mean ranks / distributions
Key assumptions Independence of observations, normality within each group, and equal variances across groups (homogeneity of variance) Independence of observations, ordinal or continuous response variable, and similar distribution shapes across groups (if interpreting as a test of medians)
Post-hoc test used Tukey’s HSD (or Games-Howell when variances are unequal) Dunn’s test (with p-value adjustment such as Bonferroni or Holm)
Advantage Higher statistical power when data are normally distributed and uses all information from the raw scores Robust against skewness, extreme outliers, and non-normal data
Disadvantage Sensitive to extreme outliers and heavy skewness Slightly lower statistical power on normally distributed data

Q37. A classmate says: “I will always use Kruskal-Wallis because it needs fewer assumptions.” Give two reasons why this may not be the best strategy.

Your answer: 1. Lower statistical power on normal data: Because Kruskal-Wallis converts exact numerical measurements into ranks (1, 2, 3, …), it throws away information about how far apart the values actually are. When data are normally distributed (especially with smaller sample sizes), Kruskal-Wallis has less statistical power than one-way ANOVA, making it more likely to miss a real difference between groups (a Type II error). 2. Less informative results and unequal-variance sensitivity: Kruskal-Wallis compares rank distributions rather than actual group means in their original measurement units, making the results harder to interpret practically (and post-hoc tests like Dunn’s test are often more conservative than Tukey’s HSD). Additionally, Kruskal-Wallis still assumes similar distribution shapes/spreads across groups if you want to interpret it as comparing medians, so if groups have very different variances, Welch’s ANOVA is often a better choice.**

Q38. A researcher has 6 observations per group from 3 groups, and the Shapiro-Wilk p-values are all > 0.05. Should they conclude that ANOVA is appropriate? Explain what else they should consider.

Your answer:No, they should not conclude that ANOVA is appropriate based solely on Shapiro-Wilk p > 0.05. With only n = 6 observations per group, the Shapiro-Wilk test has very low statistical power (as demonstrated in Q12, where n = 5 detected non-normality less than 10% of the time), meaning it will almost always give p > 0.05 even when the underlying data are genuinely skewed. Before choosing ANOVA, the researcher should also: (1) check equal variances across groups (e.g., via Levene’s test or comparing group SDs), (2) inspect visual diagnostics like boxplots and QQ plots for obvious outliers or skewness, and (3) use subject-matter / domain knowledge about the variable (e.g., whether it is bounded count or reaction-time data that is naturally skewed) to decide whether a non-parametric test like Kruskal-Wallis is safer.

Q39. (Challenge) Kruskal-Wallis is sometimes described as “a test of medians”. Using the by-hand calculation in Part 5, explain why this description is only correct when the groups have the same shape. What does the test actually compare?

Your answer: In the by-hand calculation from Part 5, the formula for H compares each group’s mean rank against the overall expected mean rank (45.5)—it never actually calculates or compares group medians. What the Kruskal-Wallis test truly compares is the overall distribution of ranks across groups (whether values in one group tend to be ranked higher or lower than values in another group). Calling it a “test of medians” is only accurate when all groups have the same distribution shape and spread, because when shapes are identical, shifting one group’s ranks higher also shifts its median higher by the same amount. If groups have different shapes (for example, one group is symmetric while another is heavily skewed with large tail values), two groups can have the exact same median yet very different mean ranks, causing Kruskal-Wallis to find a significant difference even when their medians are equal.


Session information

sessionInfo()
## R version 4.6.0 (2026-04-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=English_Indonesia.utf8  LC_CTYPE=English_Indonesia.utf8   
## [3] LC_MONETARY=English_Indonesia.utf8 LC_NUMERIC=C                      
## [5] LC_TIME=English_Indonesia.utf8    
## 
## time zone: Asia/Jakarta
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] patchwork_1.3.2 car_3.1-5       carData_3.0-6   rstatix_1.1.0  
##  [5] lubridate_1.9.5 forcats_1.0.1   stringr_1.6.0   dplyr_1.2.1    
##  [9] purrr_1.2.2     readr_2.2.0     tidyr_1.3.2     tibble_3.3.1   
## [13] ggplot2_4.0.3   tidyverse_2.0.0
## 
## loaded via a namespace (and not attached):
##  [1] ggstatsplot_1.1.4      tidyselect_1.2.1       Rmpfr_1.1-3           
##  [4] farver_2.1.2           statsExpressions_2.2.2 S7_0.2.2              
##  [7] fastmap_1.2.0          TH.data_1.1-5          bayestestR_0.20.0     
## [10] digest_0.6.39          estimability_2.0.0     timechange_0.4.0      
## [13] lifecycle_1.0.5        multcompView_0.1-12    survival_3.8-6        
## [16] magrittr_2.0.5         compiler_4.6.0         rlang_1.3.0           
## [19] sass_0.4.10            tools_4.6.0            yaml_2.3.12           
## [22] ggsignif_0.6.4         knitr_1.52             labeling_0.4.3        
## [25] RColorBrewer_1.1-3     SuppDists_1.1-9.9      multcomp_1.4-32       
## [28] abind_1.4-8            withr_3.0.3            grid_4.6.0            
## [31] datawizard_1.5.0       paletteer_1.7.0        emmeans_2.0.4         
## [34] scales_1.4.0           MASS_7.3-65            insight_1.7.0         
## [37] cli_3.6.6              mvtnorm_1.4-2          rmarkdown_2.32        
## [40] generics_0.1.4         otel_0.2.0             RcppParallel_6.2.1    
## [43] rstudioapi_0.19.0      tzdb_0.5.0             parameters_0.30.0     
## [46] cachem_1.1.0           splines_4.6.0          effectsize_1.0.3      
## [49] vctrs_0.7.3            boot_1.3-32            Matrix_1.7-5          
## [52] sandwich_3.1-3         jsonlite_2.0.0         hms_1.1.4             
## [55] ggrepel_0.9.8          Formula_1.2-6          BWStest_0.2.3         
## [58] jquerylib_0.1.4        PMCMRplus_1.9.12       glue_1.8.1            
## [61] rematch2_2.1.2         codetools_0.2-20       stringi_1.8.9         
## [64] gtable_0.3.6           prismatic_1.1.2        gmp_0.7-5.1           
## [67] pillar_1.11.1          htmltools_0.5.9        R6_2.6.1              
## [70] evaluate_1.0.5         lattice_0.22-9         backports_1.5.1       
## [73] memoise_2.0.1          broom_1.0.13           bslib_0.12.0          
## [76] rstantools_2.7.1       Rcpp_1.1.2             coda_0.19-4.1         
## [79] xfun_0.62              kSamples_1.2-12        zoo_1.9-1             
## [82] pkgconfig_2.0.3