Calculate Degrees of Freedom in R
Here we are discuss the main methods to calculate Degrees of Freedom in R Programming Language.
Calculate Degrees of Freedom using T-Test
Conduct an independent two-sample t-test to compare the means of two groups.
# Generate sample data
data1 <- rnorm(30, mean = 10, sd = 5)
data2 <- rnorm(30, mean = 12, sd = 5)
# Perform an independent two-sample t-test
t_test_result <- t.test(data1, data2)
# Print results
print(t_test_result)
# Extract degrees of freedom
df <- t_test_result$parameter
print(paste("Degrees of Freedom:", df))
Output:
Welch Two Sample t-test
data: data1 and data2
t = -0.41773, df = 57.125, p-value = 0.6777
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.288934 2.153537
sample estimates:
mean of x mean of y
10.38647 10.95417
[1] "Degrees of Freedom: 57.1245090390818"
The t-test result indicates that the t-value is -0.41773 with 57.124 degrees of freedom and a p-value of 0.6777. The degrees of freedom are not an integer due to the Welch’s correction used for unequal variances.
Calculate Degrees of Freedom using ANOVA
Perform a one-way ANOVA to compare means across three different groups.
# Generate sample data
set.seed(456) # For reproducibility
group <- factor(rep(1:3, each = 10))
response <- c(rnorm(10, mean = 10), rnorm(10, mean = 12), rnorm(10, mean = 14))
# Perform ANOVA
anova_result <- aov(response ~ group)
# Print summary
summary(anova_result)
# Extract degrees of freedom
df_between <- summary(anova_result)[[1]]["group", "Df"]
df_within <- summary(anova_result)[[1]]["Residuals", "Df"]
print(paste("Between Groups DF:", df_between))
print(paste("Within Groups DF:", df_within))
Output:
Df Sum Sq Mean Sq F value Pr(>F)
group 2 76.60 38.30 33.74 4.53e-08 ***
Residuals 27 30.65 1.14
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
[1] "Between Groups DF: 2"
[1] "Within Groups DF: 27"
The ANOVA table shows the degrees of freedom between groups (2) and within groups (27). The F value is significant, indicating differences among group means.
Calculate Degrees of Freedom using Chi-Square Test
Conduct a chi-square test of independence on a 2×2 contingency table.
# Generate sample data
observed <- matrix(c(10, 20, 30, 40), nrow = 2)
# Perform chi-square test
chi_square_result <- chisq.test(observed)
# Print results
print(chi_square_result)
# Extract degrees of freedom
df <- chi_square_result$parameter
print(paste("Degrees of Freedom:", df))
Output:
Pearson's Chi-squared test with Yates' continuity correction
data: observed
X-squared = 0.44643, df = 1, p-value = 0.504
[1] "Degrees of Freedom: 1"
The chi-square test result shows the chi-square statistic is 0.44643 with 1 degree of freedom and a p-value of 0.504, indicating no significant association between the variables.
Degrees of Freedom in R
In statistical tests, degrees of freedom are used to determine the distribution of test statistics and divert the analysis of hypothesis testing, confidence intervals, etc.