-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysis.Rmd
More file actions
271 lines (231 loc) · 6.83 KB
/
Copy pathAnalysis.Rmd
File metadata and controls
271 lines (231 loc) · 6.83 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
---
title: "BeginAnalysis"
author: "Eamon"
date: "2024-12-18"
output: html_document
---
# Initialisation
## Set Up
### Load Libraries
```{r}
library(tidyverse)
library(corrplot) # For correlation plots
library(FactoMineR) # For PCA
library(factoextra) # For PCA plots
library(vegan) # For CCA
library(ggplot2)
library(rsample) # For sampling of data (split, analysis,..)
library(rpart)
library(rpart.plot)
library(randomForest)
```
###
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Clear environment
```{r}
rm(list = ls())
```
### Specify the FilePath and Read CSV
```{r}
path <- "/Users/eamon/Desktop/MathToolsProject/R_Project/MathematicalTools/lizard.csv"
# Read a CSV file
data <- read.csv(path)
# View the first few rows of the data
data
```
## Cleaning
### Change Variables Types
```{r}
# Check the type of each column
sapply(data, class)
# Convert SD.Female.adult.weight..g. to numeric
data$SD.Female.adult.weight..g. <- as.numeric(data$SD.Female.adult.weight..g.)
# Convert categorical columns to factors
data$Species <- as.factor(data$Species)
data$Genus <- as.factor(data$Genus)
data$Family<- as.factor(data$Family)
data$Mode.of.reproduction <- as.factor(data$Mode.of.reproduction)
data$Foraging.Mode <- as.factor(data$Foraging.Mode)
data$Distribution <- as.factor(data$Distribution)
data$Prefered.Habitat.Type <- as.factor(data$Prefered.Habitat.Type)
```
### Check for and Remove Duplicates
```{r}
# Check for Duplicates
duplicates <- data[duplicated(data), ]
print("Duplicate Rows:")
print(duplicates)
# Remove duplicates
data <- data[!duplicated(data), ]
```
### Calculate Percentage of Each Variable Which Are NA
```{r}
# Calculate percentage of NA values for each column
na_percentage <- colSums(is.na(data)) / nrow(data) * 100
# Convert NA percentages to a data frame
na_df <- data.frame(
Column = names(na_percentage),
NA_Percentage = na_percentage
)
# Create bar plot
ggplot(na_df, aes(x = Column, y = NA_Percentage)) +
geom_bar(stat = "identity", fill = "skyblue") +
labs(
title = "Percentage of NA Values per Column",
x = "Columns",
y = "Percentage (%)"
) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1) # Rotate x-axis labels
)
```
### Remove Unneeded Variables
Here we removed the variables that we didn't plan to use in the PCA.
This was done before filtering out NAs to avoid unneccessary data loss.
All rows which are missing RCM (NA) are also missing mean female adult weight (which makes sense as they're related).
```{r}
data_filtered_PCA <- data %>%
select(-Sample.Size.Female.adult.weight, -Sample.size.Clutch.Size., -Sample.size.Mean.F.SVL.adults, -SD.Female.adult.weight..g., -SD.F.SVL.adults..mm.)
```
## NAs
### Check Amount of Data Loss If All NAs Removed
```{r}
data_clean <- na.omit(data_filtered_PCA)
nrow(data)
# Too many rows lost if all NAs removed, but for now can't be avoided
nrow(data_clean)
```
# PCA
### Select Only Variables of Interest
```{r}
data_selected <- data_clean %>%
select_if(is.numeric) %>%
select(-Latitude, -Longitude)
```
### Scale the Data
```{r}
data_scaled <- data_selected %>%
#Only numeric columns are selected
mutate_all(.funs = scale)
head(data_scaled)
```
### Visualise with a Correlation
```{r}
### CorrPlot
data_scaled %>%
cor(use="pairwise.complete.obs") %>% # Calculate the empirical correlation matrix
corrplot() # Then graph this matrix
```
### Run the PCA
```{r}
result_pca <- PCA(data_scaled,
scale.unit = TRUE, # Option to center and scale data (useless here)
ncp = 18, # Number of components to keep (here, all)
graph = FALSE)
```
### Result of PCA
```{r}
names(result_pca) # It's a list
result_pca
```
### Percentage of Information Retained
```{r}
fviz_eig(result_pca, choice = "variance")
```
### Visualise the PCA
```{r}
# Representation in the first principal plane
fviz_pca_var(result_pca,
axes = c(1, 2)) # Number of axes to represent
fviz_pca_var(result_pca,
axes = c(1, 3)) # Number of axes to represent
fviz_pca_var(result_pca,
axes = c(2, 3)) # Number of axes to represent
fviz_pca_var(result_pca,
axes = c(1, 4))
```
# Plot Individuals
```{r}
# Representation in the first principal plane
fviz_pca_biplot(result_pca,
axes = c(1, 2))
fviz_pca_biplot(result_pca,
axes = c(1, 3))
fviz_pca_biplot(result_pca,
axes = c(2, 3))
fviz_pca_biplot(result_pca,
axes = c(3, 4))
```
### First Principle Plane with Various Colorations
```{r}
# Representation in the first principal plane
fviz_pca_ind(result_pca,
axes = c(1, 2),
col.ind = data_clean$RCM)
# Representation in the first principal plane
fviz_pca_ind(result_pca,
axes = c(1, 2),
col.ind = data_clean$Mean.Clutch.Size)
fviz_pca_ind(result_pca,
axes = c(1, 2),
col.ind = data_clean$Mean.F.SVL.adults..mm.)
fviz_pca_ind(result_pca,
axes = c(1, 2),
col.ind = data_clean$F.SVL.at.maturity..mm.)
```
###
```{r}
# Representation in the first principal plane
fviz_pca_biplot(result_pca,
axes = c(1, 2),
col.ind = data_clean$Clutch.Frequency) # Number of axes to represent
fviz_pca_biplot(result_pca,
axes = c(1, 3),
col.ind = data_clean$Clutch.Frequency # Number of axes to represent
fviz_pca_biplot(result_pca,
axes = c(2, 3),
col.ind = data_clean$Clutch.Frequency) # Number of axes to represent
fviz_pca_biplot(result_pca,
axes = c(3, 4),
col.ind = data_clean$Clutch.Frequency) # Number of axes to represent
```
```{r}
head(data_clean)
```
# Unsupervised Learning
##Split Data
###Filter Data Variables
```{r}
data_filtered <- data_clean %>%
select(-Species, -Family, -Genus, -Population, -Source)
#select(is.numeric, Clutch.Frequency, -SD.Female.adult.weight..g., -Latitude, -Longitude)
str(data_filtered)
```
### Initial Split
```{r}
split_data <- data_filtered %>%
initial_split(prop = 0.8, strata = "Prefered.Habitat.Type")
# Train and Test Data
train_data <- analysis(split_data)
test_data <- assessment(split_data)
```
# Random Forests
### Create the RF
```{r}
forest <- randomForest(Clutch.Frequency ~ ., # Formula for prediction
data = train_data, # Data for training
ntree = 5000, # Number of trees
maxnodes = 10, # Number of maximum leaves for each tree
mtry = 4, # Number of variables for each tree
importance = TRUE) # Computation of importance
```
### Test the Accuracy
```{r}
# Fast way of computing accuracy with the pipe operator %>%
predict(forest, newdata = select(test_data, - Clutch.Frequency)) %>%
table(prediction = ., truth = test_data$Clutch.Frequency) %>%
{sum(diag(.)) / sum(.)}
```