69 lines
2.1 KiB
R
69 lines
2.1 KiB
R
##
|
|
# Libary imports
|
|
##
|
|
|
|
library(readODS)
|
|
library(tidyverse)
|
|
library(dplyr)
|
|
|
|
##
|
|
# parse the input data
|
|
##
|
|
|
|
# read a data frame from the ods document
|
|
df <- read_ods("ironwood_data_cleaned.ods", sheet = 1)
|
|
|
|
##
|
|
# 1. asses the tree health
|
|
# - create an overview of the populations health
|
|
##
|
|
|
|
# First, let's create a vector of condition names corresponding to the health index numbers
|
|
condition_names <- c("Healthy", "Light damage", "Medium damage", "Severe damage", "At point of death")
|
|
# Define colors for each condition
|
|
condition_colors <- c("green", "yellow", "orange", "red", "black")
|
|
# Calculate the percentage of trees in each health condition
|
|
percentage <- prop.table(table(df$Tree_Health_Index)) * 100
|
|
# Now, let's create the bar plot
|
|
barplot(percentage,
|
|
names.arg = condition_names,
|
|
main = "Distribution of Tree Health Index",
|
|
xlab = "Health Index",
|
|
ylab = "Percentage of Trees",
|
|
ylim = c(0, max(percentage) + 10),
|
|
col = condition_colors,
|
|
border = "black")
|
|
# Adding a legend
|
|
legend("topright", legend = condition_names, fill = condition_colors)
|
|
# Adding a grid
|
|
#grid(nx = NULL, ny = NULL, col = "lightgray", lty = "dotted")
|
|
# Adding a box around the plot
|
|
box()
|
|
# Add labels with the percentage of trees in each bar
|
|
text(x = barplot(percentage, plot = FALSE), y = percentage, labels = paste0(round(percentage, 1), "%"), pos = 3)
|
|
|
|
##
|
|
# 2. perform a shapiro-wilk normality test
|
|
# - the goal is to see if the health is normally distributed
|
|
##
|
|
|
|
# Perform Shapiro-Wilk test
|
|
shapiro_test <- shapiro.test(df$Tree_Health_Index)
|
|
# Print the test results
|
|
print(shapiro_test)
|
|
# Check the p-value
|
|
p_value <- shapiro_test$p.value
|
|
# Interpret the results
|
|
if (p_value < 0.05) {
|
|
print("The data is not normally distributed (reject the null hypothesis)")
|
|
} else {
|
|
print("The data is normally distributed (fail to reject the null hypothesis)")
|
|
}
|
|
|
|
##
|
|
# ToDo Tasks:
|
|
##
|
|
|
|
# 3. calculate the average DBH and try to correlate it with the health index
|
|
# 4. plot health indices of each site on a map and try to find patterns
|
|
# 5. try to fit health and location data in one plot |