Confidence interval (CI) formula for a two-sample t-test
df= structure(list(Treatment = c("N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N1", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0", "N0"),Block = c("I", "I", "I", "I", "I", "I", "I", "I", "I", "II", "II", "II", "II", "II", "II", "II", "II", "III", "III", "III", "III", "III", "III", "III", "III", "III", "III", "IV", "IV", "IV", "IV", "IV", "IV", "IV", "I", "I", "I", "I", "I", "I", "I", "II", "II", "II", "II", "II", "II", "III", "III", "III", "III", "III", "III", "IV", "IV", "IV", "IV", "IV"), Yield = c(866.7, 1377.3, 694.5, 654, 1198.4, 816.4, 1299.9, 850.1, 1294.4, 1113.5, 1169.2, 1016.1, 997.1, 816.6, 1385.4, 1403.8, 897.7, 1146.8, 892.9, 972.4, 1483.5, 1000, 1234.1, 897.2, 1329.3, 844.7, 1342.8, 1522.4, 1275.6, 990.1, 1165.3, 1011.9, 913.1, 1675.5, 885.8, 1014.8, 921.8, 1196.4, 923, 914.6, 759.7, 1065.8, 1282.6, 948.8, 1505.3, 1309.7, 1361.8, 1057.7, 1181.3, 1244.4, 1156, 1120.3, 961.6, 1059, 1528.7, 1297.7, 1132.5, 1009.9)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -58L))
When performing a t-test, a confidence interval can be obtained.
t.test(Yield ~ Treatment, data= df, var.equal= TRUE)
Two Sample t-test
data: Yield by Treatment
t = 0.22632, df = 56, p-value = 0.8218
alternative hypothesis: true difference in means between group N0 and group N1 is not equal to 0
95 percent confidence interval:
-109.3403 137.1933
sample estimates:
mean in group N0 mean in group N1
1118.300 1104.374
Below, I describe how to calculate the confidence interval manually, step by step.
1) Difference in means

N0=mean(subset(df, Treatment=="N0")$Yield)
N1=mean(subset(df, Treatment=="N1")$Yield)
difference= N0 - N1
cat("Yield at N0:", round(N0), "g\n")
cat("Yield at N1:", round(N1), "g\n")
cat("Yield difference:", round(difference), "g\n")
Yield at N0: 1118 g
Yield at N1: 1104 g
Yield difference: 14 g
2) Pooled variance

Var_N0= var(subset(df, Treatment=="N0")$Yield)
Var_N1= var(subset(df, Treatment=="N1")$Yield)
n_N0= length(subset(df, Treatment=="N0")$Yield)
n_N1= length(subset(df, Treatment=="N1")$Yield)
cat("Variance (N0):", round(Var_N0), "\n")
cat("Variance (N1):", round(Var_N1), "\n")
cat("Sample No. (N0):", round(n_N0), "\n")
cat("Sample No. (N1):", round(n_N1), "\n")
Variance (N0): 38587
Variance (N1): 63505
Sample No. (N0): 24
Sample No. (N1): 34
# Pooled variance
pooled_variance= ((n_N0 - 1) * Var_N0 + (n_N1 - 1) * Var_N1) / (n_N0 + n_N1 - 2)
cat("Pooled variance:", round(pooled_variance), "\n")
Pooled variance: 53271
3) Standard error

# Standard error of the difference
se= sqrt(pooled_variance * (1/n_N0 + 1/n_N1))
cat("Standard error:", round(se), "\n")
Standard error: 62
4) Degrees of freedom

# Degrees of freedom
df_pooled= n_N0 + n_N1 - 2
cat("df_pooled:", round(df_pooled), "\n")
df_pooled: 56
5) Confidence interval

# Two-tailed critical t-value
t_crit= qt(1 - 0.05/2, df= df_pooled)
cat("Critical t:", round(t_crit, 3), "\n")
Critical t: 2.003
and let’s calculate the confidence interval
CI_Lower = difference - t_crit * se
CI_Upper = difference + t_crit * se
cat("CI:", CI_Lower, CI_Upper, "\n")
CI: -109.3403 137.1933

We aim to develop open-source code for agronomy ([email protected])
© 2022 – 2025 https://agronomy4future.com – All Rights Reserved.
Last Updated: 12/12/2025