### R-code used for the regression analysis between ecological factors and haplotype diversity.

## Loading the excel file
library(readxl)
Ecological_data <- read_excel("Ecological-data.xlsx")
View(Ecological_data)

# Define the min-max normalization function
min_max_normalize <- function(x) {
  (x - min(x, na.rm = TRUE)) / (max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
}

# Apply normalization to the variables
normalized_data <- Ecological_data
variables <- c("Temperature", "Precipitation", "pH", "Organic_Carbon", "Nitrogen", "Altitude")
normalized_data[variables] <- lapply(normalized_data[variables], min_max_normalize)

# View normalized data
head(normalized_data)

#Generalized Linear Model
glm_model <- glm(Haplotype ~ pH + Organic_Carbon + Nitrogen + Altitude + Precipitation + Temperature, 
                 data = normalized_data, 
                 family = gaussian(link = "identity")) # For continuous response
summary(glm_model)

#Multinomial regression
# Ensure Haplotype is treated as a categorical variable
normalized_data$Haplotype <- as.factor(normalized_data$Haplotype)

# Load the nnet package
library(nnet)

# Fit the multinomial logistic regression
multinom_model <- multinom(Haplotype ~ Altitude + Nitrogen + pH + Organic_Carbon + Precipitation + Temperature, 
                           data = normalized_data)

# Summary of the model
summary(multinom_model)

# Check coefficients
coef(multinom_model)

# Predicted probabilities
predicted_probs <- predict(multinom_model, type = "probs")
head(predicted_probs)

# Predict the most probable haplotype for each observation
predicted_haplotypes <- predict(multinom_model, type = "class")
predicted_haplotypes

# Add the predicted haplotypes as a new column to the normalized_data
normalized_data$Predicted_Haplotype <- predicted_haplotypes

# View the updated dataset
head(normalized_data)
normalized_data

#Multinomial regression
# Ensure Haplotype is treated as a categorical variable
normalized_data$Haplotype <- as.factor(normalized_data$Haplotype)

# Fit the multinomial logistic regression
multinom_model <- multinom(Haplotype ~ Altitude + Nitrogen + pH + Organic_Carbon + Precipitation + Temperature, 
                           data = normalized_data)


# Summary of the model
summary(multinom_model)

# Check coefficients
coef(multinom_model)


# Extract coefficients and standard errors
coef_summary <- summary(multinom_model)
z_scores <- coef_summary$coefficients / coef_summary$standard.errors
p_values <- 2 * (1 - pnorm(abs(z_scores)))

# Print p-values for each predictor
p_values

# Predict haplotype probabilities for the dataset
predicted_probs <- predict(multinom_model, type = "probs")

# View the predicted probabilities
head(predicted_probs)

# Predict the most probable haplotype for each observation
predicted_haplotypes <- predict(multinom_model, type = "class")
predicted_haplotypes
normalized_data$Haplotype

# Add the predicted haplotypes as a new column to the normalized_data
normalized_data$Predicted_Haplotype <- predicted_haplotypes

# View the updated dataset
head(normalized_data)

# Create confusion matrix
conf_matrix <- table(normalized_data$Haplotype, normalized_data$Predicted_Haplotype)

conf_matrix

# Calculate false positives for each class
false_positives <- apply(conf_matrix, 2, sum) - diag(conf_matrix)

# Calculate False Positives (FP) and False Negatives (FN)
false_positives <- apply(conf_matrix, 2, sum) - diag(conf_matrix) # Column sums minus diagonal
false_negatives <- apply(conf_matrix, 1, sum) - diag(conf_matrix) # Row sums minus diagonal


# Calculate total instances
total_instances <- sum(conf_matrix)

# Calculate correctly classified instances (sum of diagonal)
correct_classifications <- sum(diag(conf_matrix))

# Calculate misclassified instances
misclassifications <- total_instances - correct_classifications

# Calculate error rate
error_rate <- misclassifications / total_instances * 100

# Display results
cat("Error Rate:", error_rate, "%\n")



####### Data set used for the regression analysis (saved as Ecological_data) ########
Haplotype	Altitude	Nitrogen	pH	Organic_Carbon	Precipitation	Temperature
1	339	0.47	7.9000001	3.8	677	9.2836666
1	327.610199	0.72	7.6999998	5.5999999	881	12.3388329
1	339.085022	0.48	7.4000001	3.9000001	888	12.4635
1	343.175995	0.49	7.5999999	2.4000001	888	12.4635
1	344.488983	0.49	7.5999999	2.4000001	888	12.4635
1	339.085022	0.48	7.4000001	3.9000001	888	12.4635
1	289	0.64	6.9000001	2.9000001	864	9.0635004
1	297.317505	0.72	7	4	844	11.2863331
1	350.852325	0.77	7	3.3	844	11.2863331
1	302.368408	0.81	7.0999999	6.4000001	846	11.2508335
2	276.935699	0.55	7.0999999	4.5999999	844	11.2863331
3	326.325165	0.53	7.4000001	4	888	12.4635
4	340.897156	0.77	7.0999999	3.7	844	11.2863331
5	334.293243	0.68	7.6999998	5.5	881	12.3388329
4	304.954987	0.72	7	4	844	11.2863331
4	217.985931	0.51	5.9000001	2.8	1096	15.0279999
4	324.371063	0.68	7.6999998	5.5	881	12.3388329
6	282.732361	0.73	5.4000001	5	1397	15.3113337
7	178	0.37	6.3000002	2.4000001	926	11.2441664
8	306	0.54	7.5	3.5	847	10.7969999
6	247.241837	1.1	6.6999998	4.5	1010	12.7808332
4	343.009888	0.77	7.0999999	3.7	844	11.2863331
4	333.429779	0.48	7	4	844	11.2863331
4	333.040588	0.68	7.6999998	5.5	881	12.3388329
4	343.631042	0.49	7.5999999	2.4000001	888	12.4635
4	326.748749	0.53	7.4000001	4	888	12.4635
9	329.177521	0.77	7	4	844	11.2863331
9	337.295624	0.68	7.6999998	5.5	881	12.3388329
9	155.194336	0.28	5.5999999	2.5	1071	18.4066658
9	333.429779	0.48	7	4	844	11.2863331
9	344.506134	1.1	7.0999999	2.9000001	844	11.2863331
6	336.666351	0.77	7.0999999	3.7	844	11.2863331
6	340.970001	0.77	7.0999999	3.7	844	11.2863331
6	324.404785	0.77	7.0999999	3.7	844	11.2863331
6	333.873077	0.53	7.4000001	4	888	12.4635
6	331.075287	0.53	7.4000001	4	888	12.4635
6	328.801117	0.53	7.4000001	4	888	12.4635
6	328.368042	0.53	7.4000001	4	888	12.4635
10	337.545532	0.53	7.4000001	4	888	12.4635
6	259	0.75	6.8000002	3	876	9.4336662
11	285.154602	0.72	7	4	844	11.2863331
6	335.676727	0.77	7.0999999	3.7	844	11.2863331
12	292	0.42	6.9000001	3.5	851	9.0900002
13	292	0.42	6.9000001	3.5	851	9.0900002
6	274.05957	0.66	6.5	2.8	1010	12.7808332


