-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFINAL_R-Script_DataAnalysis.R
More file actions
200 lines (156 loc) · 8.65 KB
/
Copy pathFINAL_R-Script_DataAnalysis.R
File metadata and controls
200 lines (156 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
library(readxl)
library(ggplot2)
library(dplyr)
# LOADING THE DATASET
dataset <- read_excel("dataset_specific_region.xlsx", sheet = "Mimaropa")
# BASIC SUMMARY STATISTICS
summary(dataset)
# LINEAR MODEL
result <- lm(Extinction_Risk_Rate ~ Average_Temperature_Increase + Deforestation_Rate ,
data = dataset)
# BASIC SUMMARY STATISTICS
summary(result)
# CREATES A LINE PLOT TO VISUALIZE DATA
linePlot <- ggplot(dataset, aes(x = Year)) +
geom_line(aes(y = Deforestation_Rate, color = "Deforestation Rate")) +
geom_line(aes(y = Average_Temperature_Increase, color = "Average Temp Increase")) +
geom_line(aes(y = Reforestation_Rate, color = "Reforestation Rate")) +
geom_line(aes(y = Extinction_Risk_Rate, color = "Extinction Risk Rate")) +
labs(title = "Environmental Data Trends Over Time",
x = "Year",
y = "Values") +
scale_color_manual(values = c("Deforestation Rate" = "red",
"Average Temp Increase" = "blue",
"Reforestation Rate" = "green",
"Extinction Risk Rate" = "purple")) +
theme_minimal()
# DISPLAYS THE PLOT
print(linePlot)
# -----------------------------------------------------------------------------#
# ESTABLISHING THE KNOWLEDGE BASE / FACTS
SpeciesData <- read_excel("dataset_specific_region.xlsx", sheet = "SpeciesData")
ForestData <- read_excel("dataset_specific_region.xlsx", sheet = "ForestData")
OrganizationData <- read_excel("dataset_specific_region.xlsx", sheet = "OrganizationData")
IUCNRedList <- read_excel("dataset_specific_region.xlsx", sheet = "IUCNRedList")
ForestIssues <- read_excel("dataset_specific_region.xlsx", sheet = "ForestIssues")
IssueData <- read_excel("dataset_specific_region.xlsx", sheet = "IssueData")
# ESTABLISHING THE RELATIONSHIPS
SpeciesCategory <- SpeciesData %>%
left_join(IUCNRedList, by = c("Category" = "Abbreviation")) %>%
select(Species, IUCN_Red_List_Category = FullForm) %>% as.data.frame()
ForestIssueData <- ForestIssues %>%
left_join(IssueData, by = c("Issue" = "Issue")) %>% as.data.frame()
# -----------------------------------------------------------------------------#
# RELATIONSHIPS
categorizedAs <- function(species, category){
any(SpeciesCategory$Species == species & SpeciesCategory$IUCN_Red_List_Category == category)
}
locatedIn <- function(forest, location){
any(ForestData$Forest == forest & ForestData$Location == location)
}
canBeFoundIn <- function(species, location){
any(ForestData$Species == species & ForestData$Location == location)
}
inhabits <- function(forest, species){
any(ForestData$Forest == forest & ForestData$Species == species)
}
headquarteredIn <- function(organization, location){
any(OrganizationData$Organization == organization & OrganizationData$Location == location)
}
affects <- function(issue, forest){
any(ForestIssueData$Issue == issue & ForestIssueData$Forest == forest)
}
adverselyAffects <- function(issue, species){
data <- ForestIssueData[1:2] %>% left_join(ForestData[2:3], by = c("Forest" = "Forest"), relationship = "many-to-many") %>%
select(Issue, Species) %>% distinct() %>% as.data.frame()
any(data$Issue == issue & data$Species == species)
}
# -----------------------------------------------------------------------------#
# PREDICTIVE FUNCTIONS
predictImpact <- function(issue = "NA"){
if(issue %in% ForestIssueData$Issue){
forestImpact <- ForestIssueData[-c(1, 4)] %>% filter(Issue == issue) %>% distinct %>% select(Impact) %>% as.data.frame()
cat("\n\nIt has been predicted that", issue, "will bring forth the impact:\n\n", unique(forestImpact$Impact))
predictAffectedForestInLocation(issue)
} else if (issue == "NA") {cat("\nPlease enter an issue!\n")} else {cat("\nIssue not found in the database!\n")}
}
predictAffectedForestInLocation <- function(issue = "NA"){
if(issue %in% ForestIssueData$Issue){
affectedForestInLocation <- ForestIssueData %>% filter(Issue == issue) %>%
left_join(ForestData[1:2], by = c("Forest" = "Forest")) %>%
distinct() %>% select(Affected_Forest = Forest, Location, Practice) %>% as.data.frame()
cat("\n\nIt has been predicted that", issue, "will adversely affect the following Forest in location:\n\n")
print(affectedForestInLocation %>% select(-Practice))
cat("\n\nTo fight", issue, "It is suggested to perform the conservation practice: \n\n", unique(affectedForestInLocation$Practice))
} else if (issue == "NA") {cat("\nPlease enter an issue!\n")} else {cat("\nIssue not found in the database!\n")}
}
predictAllAffectedForestInLocation <- function(location = "NA"){
if (location %in% unique(ForestData$Location)){
affectedForests <- ForestData %>% filter(Location == location) %>% select(Forest) %>% distinct() %>%as.data.frame()
cat("\nIf an environmental issue arises in", location, "it has been predicted that the following forests will be put to risk:\n\n")
print(affectedForests)
} else if (location == "NA") {cat("\nPlease enter a location!\n")} else {cat("\nLocation not found in the database!\n")}
}
predictAffectedSpecies <- function(issue = "NA"){
if(issue %in% ForestIssueData$Issue){
affectedSpecies <- ForestIssueData %>% filter(Issue == issue) %>%
left_join(ForestData, by = c("Forest" = "Forest")) %>%
select(Species, Location, Forest) %>% distinct() %>% as.data.frame()
cat("\n\nIt has been predicted that", issue, "will adversely affect the following Species:\n\n")
print(affectedSpecies)
} else if (issue == "NA") {cat("\nPlease enter an issue!\n")} else {cat("\nIssue not found in the database!\n")}
}
predictAffectedSpeciesPerForestLocation <- function(forest = "NA", location = "NA"){
if(locatedIn(forest, location)){
affectedSpecies <- ForestData %>% filter(Forest == forest, Location == location) %>% select(Species) %>% as.data.frame()
cat("\n\nIf an environmental crisis arises in", forest, "in", location,
", it has been predicted that the following species below will be at risk:\n\n")
print(affectedSpecies)
} else if ((forest == "NA") | (location == "NA")) {cat("\nPlease enter a forest and an issue!\n")}
else {cat("\n", forest, "is not located in", location, "!\n")}
}
suggestOrganization <- function(location = "NA"){
if (!location %in% unique(OrganizationData$Location)){cat("\nThe location", location, "isn't in the database!\n")}
else if (location == "NA"){cat("\nPlease enter a location!\n")}
else {print(OrganizationData %>% filter(Location == location) %>% select(-Location) %>% distinct() %>% as.data.frame())}
}
identifyThreatenedSpecies <- function(location = "NA"){
threatenedSpecies <- ForestData %>% left_join(SpeciesData, by = c("Species" = "Species"), relationship = "many-to-many") %>%
filter(Location == location) %>% left_join(IUCNRedList, by = c("Category" = "Abbreviation")) %>%
filter(FullForm %in% c("Vulnerable", "Endangered", "Critically Endangered")) %>%
select(Forest, Species, Category = FullForm) %>% as.data.frame()
if (location == "NA"){cat("\nPlease enter a location!\n")}
if (nrow(threatenedSpecies) == 0){cat("There is no threatened species in", lcoation)}
else if (!location %in% unique(ForestData$Location)){cat("\nThe location", location, "isn't in the database!\n")}
else{
cat("\nWarning! A threatened species has been identified in", location, "!\n\n Prioritize Utmost Conservation!\n\n")
print(threatenedSpecies)
cat("\nIt is suggested to Contact the organization below:\n\n")
suggestOrganization(location)
}
}
# -----------------------------------------------------------------------------#
while (TRUE){
cat("\n\nMain Menu\n",
"1. Predict Impact\n",
"2. Predict Affected Forest In Location\n",
"3. Predict All Affected Forest In Location\n",
"4. Predict Affected Species\n",
"5. Predict Affected Species Per Forest Location\n",
"6. Suggest Organization\n",
"7. Identify Threatened Species\n",
"8. Exit\n\n")
case <- readline("> ")
cat("\n")
switch (
case,
"1" = predictImpact(readline("Input an issue: ")),
"2" = predictAffectedForestInLocation(readline("Input an issue: ")),
"3" = predictAllAffectedForestInLocation(readline("Input a location: ")),
"4" = predictAffectedSpecies(readline("Input an issue: ")),
"5" = predictAffectedSpeciesPerForestLocation(readline("Input a forest: "), readline("Input a location: ")),
"6" = suggestOrganization(readline("Input a location: ")),
"7" = identifyThreatenedSpecies(readline("Input a location: ")),
"8" = break
)
}