# Libraries used
library(readxl)
library(kableExtra)
library(magrittr)
library(ggplot2)
library(dplyr)
INTRODUCTION
Data analysis has become an important tool for decision-making in various fields, from business to academia and beyond. The ability to extract meaningful insights from data is a skill that has transformed how organizations operate and make informed choices. In this report, I will be importing the InchBio dataset into R and analysing the fisheries in the dataset. I will perform analysis to obtain summary data, the mean, median, and quantile range. I will also visualize the probability of species in the dataset with the use of a pie chart and bar plot. I will be extracting from the dataset the first and last five values using head and tail functions, respectively, and presenting these values in table form for a clearer summary and view. In this project, I will be making use of R to arrive at a different set of codes to perform descriptive statistics. By the end of this document, readers will gain a deeper understanding of the value of data analysis and how it can empower individuals and organizations to make data-driven decisions.
TASK1 A
# Data sets used
#Import inchBio
library(readxl)
inchBio <- read_excel("inchBio.xlsx")
#Obtain summary
summary_stats <- summary(inchBio)
#Create table
summary_table <- kable(summary_stats, format = "html") %>%
kable_styling(full_width = FALSE, position = "center", font_size = 14)
#Display table
summary_table
| netID | fishID | species | length | weight | scale | |
|---|---|---|---|---|---|---|
| Min. : 4.00 | Min. : 7.0 | Length:505 | Min. : 30.47 | Min. : 0.9027 | Mode :logical | |
| 1st Qu.: 12.00 | 1st Qu.:169.0 | Class :character | 1st Qu.: 63.90 | 1st Qu.: 3.0711 | FALSE:194 | |
| Median :101.00 | Median :569.0 | Mode :character | Median :153.04 | Median : 60.2374 | TRUE :311 | |
| Mean : 78.68 | Mean :487.5 | NA | Mean :160.52 | Mean : 129.3169 | NA | |
| 3rd Qu.:113.00 | 3rd Qu.:762.0 | NA | 3rd Qu.:230.11 | 3rd Qu.: 193.4106 | NA | |
| Max. :206.00 | Max. :915.0 | NA | Max. :432.58 | Max. :1071.8813 | NA |
Observation
Here I loaded the kableExtra library, this provides additional styling options, I obtain summary statistics for the inchBio dataset using the summary() function, created a well-styled table using kable() and apply styling using kable_styling(). The full_width = FALSE argument ensures that the table doesn’t span the full page width, and position = “center” centers the table on the page. the font font_size and other styling options can be adjusted at will.
Lastly I displayed the summary table.
TASK1 B
Use code glimpse
#glimpse function
glimpse(inchBio)
## Rows: 505
## Columns: 6
## $ netID <dbl> 5, 16, 16, 21, 24, 24, 101, 101, 101, 101, 102, 102, 102, 102,…
## $ fishID <dbl> 137, 208, 209, 218, 268, 269, 532, 534, 535, 537, 626, 627, 62…
## $ species <chr> "Black Crappie", "Black Crappie", "Black Crappie", "Black Crap…
## $ length <dbl> 268.75365, 298.37579, 275.35851, 154.17039, 332.43289, 309.698…
## $ weight <dbl> 276.278409, 380.552210, 260.684962, 47.293176, 580.883012, 441…
## $ scale <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TR…
Observation This function present the summary of dataset structure in a concise and informative way, it helps to quickly understand dataset structure.
TASK1 C
compare the information obtained on 1A and 1B
In the data above the outlier for fishid is 7 Q1=169 boxplot, netid outlier=4 Q1=12, weight is 0.9027 Q1= 3.0711 and length is 30.47 Q1=63.90, the data summarized and present in 1B was used to arrive at mean, median and quantile range in 1A
#Inline r codes
#To calculate the number of rows and column
num_columns <- ncol(inchBio)
num_rows <- nrow(inchBio)
#showing values
#The dataset has `r 505` rows and `r 6` columns.
#ncol(inchBio) is used to calculate the number of columns and nrow(inchBio) the number of rows in inchBio dataset.
TASK3
#Perform data analysis with few variables (columns)
selected_data <- inchBio %>%
select(species, length, weight)
#first 5 and last 5
first_5 <- head(selected_data, 5)
last_5 <- tail(selected_data, 5)
combined_data <- rbind(first_5, last_5)
table <- combined_data %>%
kable(format = "html", caption = "Selected Data: First 5 and Last 5 Observations") %>%
kable_styling(full_width = FALSE, position = "center", font_size = 14)
table
| species | length | weight |
|---|---|---|
| Black Crappie | 268.75365 | 276.278409 |
| Black Crappie | 298.37579 | 380.552210 |
| Black Crappie | 275.35851 | 260.684962 |
| Black Crappie | 154.17039 | 47.293176 |
| Black Crappie | 332.43289 | 580.883012 |
| Yellow Perch | 223.25979 | 114.197830 |
| Yellow Perch | 86.36791 | 5.865749 |
| Yellow Perch | 93.06347 | 8.160477 |
| Yellow Perch | 82.16209 | 5.820468 |
| Yellow Perch | 72.45059 | 3.038898 |
# I used the head and tail function to obtain the first 5 and last 5 observations of the selected data, then I combined these observations into the combined_data data frame with rbind.
#Finally, I created a well-formatted table using kable and kable_styling to present the selected data. The table display the first 5 and last 5
TASK4
#Basic descritptive statistics for lenght and weight
mean_length <- mean(inchBio$length)
median_length <- median(inchBio$length)
sd_length <- sd(inchBio$length)
quantiles_length <- quantile(inchBio$length, probs = c(0.25, 0.75))
min_length <- min(inchBio$length)
mean_weight <- mean(inchBio$weight)
median_weight <- median(inchBio$weight)
sd_weight <- sd(inchBio$weight)
quantiles_weight <- quantile(inchBio$weight, probs = c(0.25, 0.75))
min_weight <- min(inchBio$weight)
length_values <- c(mean_length, median_length, sd_length, quantiles_length, min_length)
weight_values <- c(mean_weight, median_weight, sd_weight, quantiles_weight, min_weight)
column_names <- c("Mean", "Median", "Standard Deviation", "25th Quantile", "75th Quantile", "Minimum")
row_names <- c("Length", "Weight")
statistics_matrix <- matrix(c(length_values, weight_values), nrow = 2, byrow = TRUE)
colnames(statistics_matrix) <- column_names
rownames(statistics_matrix) <- row_names
# Table using kable and kable_styling
library(kableExtra)
table <- statistics_matrix %>%
kable(format = "html", caption = "Descriptive Statistics for Length and Weight") %>%
kable_styling(full_width = FALSE, position = "center", font_size = 14)
table
| Mean | Median | Standard Deviation | 25th Quantile | 75th Quantile | Minimum | |
|---|---|---|---|---|---|---|
| Length | 160.5242 | 153.04343 | 100.2914 | 63.897124 | 230.1136 | 30.4685480 |
| Weight | 129.3169 | 60.23738 | 167.5727 | 3.071062 | 193.4106 | 0.9027038 |
Observation
Here I calculated various descriptive statistics for the variables length and weight, including mean, median, standard deviation, quantiles (25th and 75th), and minimum values, I created separate objects for each of these values for both length and weight, I created vectors for column names (column_names) and row names (row_names) for the table, created a matrix (statistics_matrix) to organize the values, used the kable and kable_styling functions from the library kableExtra to create a well-formatted table that presents the descriptive statistics for length and weight.
TASK 5
species_names <- unique(inchBio$species)
num_species <- length(species_names)
mean_length_matrix <- matrix(0, nrow = num_species, ncol = 1)
mean_weight_matrix <- matrix(0, nrow = num_species, ncol = 1)
rownames(mean_length_matrix) <- species_names
rownames(mean_weight_matrix) <- species_names
table <- cbind(mean_length_matrix,mean_weight_matrix)
colnames(table) <- c("Mean Length","Mean Weight")
table %>%
kable(format = "html", caption = "Mean Length and Mean Weight for Each Species") %>%
kable_styling(full_width = FALSE, position = "center", font_size = 14)
| Mean Length | Mean Weight | |
|---|---|---|
| Black Crappie | 0 | 0 |
| Bluegill | 0 | 0 |
| Bluntnose Minnow | 0 | 0 |
| Iowa Darter | 0 | 0 |
| Largemouth Bass | 0 | 0 |
| Pumpkinseed | 0 | 0 |
| Yellow Perch | 0 | 0 |
TASK 6
species_table <- table(inchBio$species) %>%
as.data.frame() %>%
dplyr::rename(Frequency = Freq)
species_table <- species_table %>%
mutate(Cumulative_Frequency = cumsum(Frequency))
total_obs <- sum(species_table$Frequency)
species_table <- species_table %>%
mutate(Probability = round(Frequency / total_obs, 2),
Cumulative_Probability = round(Cumulative_Frequency / total_obs, 2))
table <- species_table %>%
kable(format = "html",
caption = "Frequency, Cumulative Frequency, Probability, and Cumulative Probability for Species") %>%
kable_styling(full_width = FALSE, position = "center", font_size = 14)
table
| Var1 | Frequency | Cumulative_Frequency | Probability | Cumulative_Probability |
|---|---|---|---|---|
| Black Crappie | 25 | 25 | 0.05 | 0.05 |
| Bluegill | 208 | 233 | 0.41 | 0.46 |
| Bluntnose Minnow | 100 | 333 | 0.20 | 0.66 |
| Iowa Darter | 31 | 364 | 0.06 | 0.72 |
| Largemouth Bass | 90 | 454 | 0.18 | 0.90 |
| Pumpkinseed | 13 | 467 | 0.03 | 0.92 |
| Yellow Perch | 38 | 505 | 0.08 | 1.00 |
pie_chart <- ggplot(species_table, aes(x = "", y = Probability, fill = Var1)) +
geom_bar(stat = "identity") +
coord_polar("y", start = 0) +
labs(title = "Probability of Each Species", fill = "Species") +
theme_minimal() +
theme(legend.position = "right") +
geom_text(aes(label = paste0(Var1, ": ", scales::percent(Probability))), position = position_stack(vjust = 0.5)) +
scale_fill_brewer(palette = "Set3")
bar_plot <- ggplot(species_table, aes(x = Var1, y = Cumulative_Probability, fill = Var1)) +
geom_bar(stat = "identity") +
labs(title = "Cumulative Probability of Each Species", x = "Species", y = "Cumulative Probability") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
geom_text(aes(label = scales::percent(Cumulative_Probability)), vjust = -0.3)
bar_plot
pie_chart
Data analysis is a powerful tool that empowers individuals and organizations to extract valuable data analysis is a powerful tool that empowers individuals and organisations to extract valuable insights from data. Descriptive and inferential statistics are crucial in understanding data distributions and making inferences. With this, one can navigate the complex landscape of data with confidence. As seen in the project above, I have used different library packages to create codes that can extract statistical values from the IncBio dataset. In this project, I have used code summary, the glimpse function, Inline R codes, weight and length, and a filter function to extract a variable and obtain information about the whole inchBio data set.
The analysis above shows that the probability of Bluegill stands at 41% occurrence of the total species, while Yellow perch has the highest cumulative probability of occurrence in a batch ID in all the species followed by pumpkinseed,largemouth bass,Iowa Darter,Bluntnose,Bluegill and black crappie in a descending order
Wickham, H. (2019). Advanced R. CRC Press https://adv-r.hadley.nz.
Prof Dee Chiluiza RPubs: (2021) https://rpubs.com/Dee_Chiluiza/home
Tufte, E. R. (2001). “The Visual Display of Quantitative Information.
Triola, M. F. (2017). Elementary Statistics. Pearson.
Antonette Dela Cruz (2023), Cumulative Distribution & Probability https://study.com/academy/lesson/cumulative-distribution-function-formula-ex amples.html
John A. Rice (2007) Mathematical Statistics and Data Analysis, Brooks/Cole
An R Markdown (*.Rmd) file containing the R codes has been attached to this report. The name of this file is Adegoke_project3__R.Rmd