library(lme4)
library(lmerTest)
library(agricolae)
library(tidyverse)
##Analysis of Augmented Block Design Across Two Seasons Using Linear mixed Models##

##Importing Augmented Design Data into R for Analysis 
data <- AUGMENTED_DESIGN_DATA_FOR_ANALYSIS
print(data)
##Create a new column that categorizes entries into two groups: one for 
##New Genotypes and another for Checks.Therefore, it has 2levels and its df=1

data$NewCheck<- 0
data$NewCheck[data$Genotype== "C1"]<- 1
data$NewCheck[data$Genotype== "C2"]<- 1
data$NewCheck[data$Genotype== "C3"]<- 1

##Make another Column with 3Checks and 1Group containing Newgenotype
data$entryc <- 0
data$entryc [data$Genotype=="C1"]<- "C1"
data$entryc [data$Genotype=="C2"]<- "C2"
data$entryc [data$Genotype=="C3"]<- "C3"

##Change explanatory variable to factors
str(data)
# Convert columns to factors
data$S         <- as.factor(data$S) # Seasons 1 and 2
data$Genotype  <- as.factor(data$Genotype) # Newentry
data$blk       <- as.factor(data$blk)    # Blocks (10)
data$Newcheck <- as.factor(data$NewCheck)
data$entryc    <- as.factor(data$entryc)
view(data)
#Check layout of Genotypes across blocks
xtabs(~ Genotype + blk, data)

#Application  Linear mixed Model to Analyze Augmented Block Design Data

# Fit the linear model(A= Length of the Branch (cm) )
model_A <- lmer(A ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_A)
print(anovaOut)

modelSum <- summary(model_A, correlation=TRUE)
print(modelSum)

# Fit the linear model(B= Plant height (cm) )
model_B <- lmer(B ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_B)
print(anovaOut)

modelSum <- summary(model_B, correlation=TRUE)
print(modelSum)


# Fit the linear model(C= No .of branches plant¯¹ )

model_C <- lmer(C ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_C)
print(anovaOut)

modelSum <- summary(model_C, correlation=TRUE)
print(modelSum)


# Fit the linear model (D=No. of clusters)
model_D <- lmer(D ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_D)
print(anovaOut)

modelSum <- summary(model_D, correlation=TRUE)
print(modelSum)
# View the model summary
anova(model_D)

# Fit the linear model(E= No of pods plant ¯¹ )
model_E <- lmer(E ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_E)
print(anovaOut)

modelSum <- summary(model_E, correlation=TRUE)
print(modelSum)

# Fit the linear model(F= No of seed pod¯¹ )
model_F <- lmer(F ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_F)
print(anovaOut)

modelSum <- summary(model_F, correlation=TRUE)
print(modelSum)


# Fit the linear model(G=Pod weight (g) )
model_G <- lmer(G ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_G)
print(anovaOut)

modelSum <- summary(model_G, correlation=TRUE)
print(modelSum)


# Fit the linear model(H= Seed weight (g) )
model_H <- lmer(H ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_H)
print(anovaOut)

modelSum <- summary(model_H, correlation=TRUE)
print(modelSum)


# Fit the linear model(I=  Weight of 1000 seeds (g) )
model_I <- lmer(I ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_I)
print(anovaOut)

modelSum <- summary(model_I, correlation=TRUE)
print(modelSum)

# Fit the linear model(J= Pod length (cm) )
model_J <- lmer(J ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_J)
print(anovaOut)

modelSum <- summary(model_J, correlation=TRUE)
print(modelSum)


# Fit the linear model(K= days to flowering )

model_K <- lmer(K ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_K)
print(anovaOut)

modelSum <- summary(model_K, correlation=TRUE)
print(modelSum)
# View the model summary
anova(model_K)

# Fit the linear model(L= days to maturity )
model_L <- lmer(L ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_L)
print(anovaOut)

modelSum <- summary(model_L, correlation=TRUE)
print(modelSum)

# Fit the linear model(M= Shelling Percentage)
model_M <- lmer(M ~ entryc + Newcheck + S + (1|blk) +(1|Genotype:NewCheck) + S:Genotype,data = data)

# View the model summary
anovaOut <- anova(model_M)
print(anovaOut)

modelSum <- summary(model_M, correlation=TRUE)
print(modelSum)


