data<-read.csv("data_Heritability_ANOVA .csv", header=T)
attach(data)
str(data)
## 'data.frame': 50 obs. of 2 variables:
## $ Sire: chr "S1" "S1" "S1" "S1" ...
## $ P : int 195 183 242 190 200 201 201 211 196 197 ...
head(data)
## Sire P
## 1 S1 195
## 2 S1 183
## 3 S1 242
## 4 S1 190
## 5 S1 200
## 6 S1 201
# Fitting ANOVA model
model <- lm(P ~ Sire, data = data)
# View ANOVA table
summary.aov(model)
## Df Sum Sq Mean Sq F value Pr(>F)
## Sire 4 3482 870.4 1.9 0.127
## Residuals 45 20617 458.2
# Getting mean squares
anova_table <- summary.aov(model)[[1]]
MS_sire <- anova_table["Sire", "Mean Sq"]
MS_residual <- anova_table["Residuals", "Mean Sq"]
# Number of replicates per genotype
ni <- length(data$P) / length(unique(data$Sire))
# Estimate variance components
genetic_var <- (MS_sire - MS_residual) / ni
residual_var <- MS_residual
phenotypic_var <- genetic_var + residual_var
anova_table <- summary.aov(model)[[1]]
# Print the whole ANOVA table to see what you can extract
print(anova_table)
## Df Sum Sq Mean Sq F value Pr(>F)
## Sire 4 3481.7 870.42 1.8998 0.127
## Residuals 45 20617.3 458.16
# Extract row and column names to confirm
print(rownames(anova_table)) # To confirm "Sire" exists
## [1] "Sire " "Residuals "
print(colnames(anova_table)) # To confirm "Mean Sq" exists
## [1] "Df" "Sum Sq" "Mean Sq" "F value" "Pr(>F)"
# If column name is "Mean Sq" (or "Mean Sq."), adjust accordingly
MS_sire <- as.numeric(anova_table["Sire", "Mean Sq"])
MS_residual <- as.numeric(anova_table["Residuals", "Mean Sq"])
# Print them to check
print(MS_sire)
## [1] 870.42
print(MS_residual)
## [1] 458.1622
# Heritability
h2 <- 4*genetic_var / phenotypic_var;h2
## [1] 0.3302104
## Df Sum Sq Mean Sq F value Pr(>F)
## Sire 4 3481.7 870.42 1.8998 0.127
## Residuals 45 20617.3 458.16
Df (Degrees of Freedom) :
Sire: 4 sires (groups) were tested.
Residuals: 45 individual-level observations within
the groups.
Sum Sq (Sum of Squares) :
Sire: 3481.7 – Total variability due to differences
among sires.
Residuals: 20617.3 – Variability within groups (random
noise/error).
Mean Square :
Sire: 870.42 = 3481.7 / 4 – Average variability between
groups.
Residuals: 458.16 = 20617.3 / 45 – Average variability
within groups.
F value :
1.8998 – Ratio of SireMS to ResidualMS;
used to test if sires differ significantly.
Pr(>F) :
0.127 – p-value indicating non-significance (usually compared to 0.05); no strong evidence of sire effect.
h2 <- 4*genetic_var / phenotypic_var
Assuming:
genetic_var = (MS_sire - MS_residual) / k , where k is number of observations per sire.
* phenotypic_var = MS_sire + (k - 1) MS_residual / k or estimated directly.
Final output:
0.3302104