The second pair I wanted to examine was the number of book ratings and the average book ratings. This could provide insight on if a reader is more likely to review a book they enjoyed, a book they disliked, or perhaps there is not much of a relationship at all. To begin, I also wanted to calculate a ratio between the two features and create a scatterplot.
book_sales$Ratings_to_Avg_Ratio <- book_sales$Book_ratings_count / book_sales$Book_average_rating
ggplot(book_sales, aes(x = Book_ratings_count, y = Book_average_rating, color = Ratings_to_Avg_Ratio)) +
geom_point() +
scale_color_gradient(low = "blue", high = "red") +
labs(x = "Book Ratings Count", y = "Book Average Rating", color = "Ratings to Avg Ratio") +
ggtitle("Relationship between Book Ratings Count and Average Ratingt")
The scatterplot for the book average rating and book ratings count does not appear to indicate a strong relationship between the two variables. Although there are few books with over 100,000 ratings that have an average book rating of less than 3.5, I am very hesitant to call this much of a relationship.
#defining bootstrap with confidence interval
bootstrap_correlation <- function(data, n_iter = 1000, confidence_level = 0.95) {
correlation_coefficients <- numeric(n_iter)
#simulate sampling n_iter times
for (i in 1:n_iter) {
sampled_data <- data[sample(1:nrow(data), replace = TRUE), ]
correlation_coefficients[i] <- cor(sampled_data$Book_ratings_count, sampled_data$Book_average_rating)
}
mean_val <- mean(correlation_coefficients)
lower_ci <- quantile(correlation_coefficients, (1 - confidence_level) / 2)
upper_ci <- quantile(correlation_coefficients, 1 - (1 - confidence_level) / 2)
return(list(Mean = mean_val, Lower_CI = lower_ci, Upper_CI = upper_ci,
Bootstrapped_Values = correlation_coefficients))
}
bootstrap_result <- bootstrap_correlation(book_sales)
cat("Correlation Coefficient Mean:", bootstrap_result$Mean, "\n")
## Correlation Coefficient Mean: 0.09156592
cat("95% Confidence Interval:", bootstrap_result$Lower_CI, "-", bootstrap_result$Upper_CI, "\n")
## 95% Confidence Interval: 0.03062 - 0.1498668
The 95% confidence interval suggest that there is a large amount of uncertainty of there being much of a relationship between the Book Average Rating and Book Rating Count. Though there seems to be a very slight positive relationship, it does not seem to be significant given the data. I believe that this matches how I felt about the visualization - there is no strong indication of relation.
Finally, I wanted to examine how the units sold and gross sales procured have changed through the years. To begin, I will calculate the units sold and gross for each year.
#calculating units sold and gross sales for each year
total_sales_per_year <- book_sales |>
filter(`Publishing Year` > 1900) |> #filtering this to show only more recent data
group_by(`Publishing Year`) |>
summarise(Total_Units_Sold = sum(`units sold`),
Total_Gross_Sales = sum(`gross sales`))
ggplot(total_sales_per_year, aes(x = `Publishing Year`)) +
geom_line(aes(y = Total_Units_Sold), color = "blue", size = 1.5) +
geom_line(aes(y = Total_Gross_Sales), color = "red", size = 1.5) +
scale_y_continuous(sec.axis = sec_axis(~., name = "Total Gross Sales")) +
labs(title = "Total Units Sold and Gross Sales Over the Years",
x = "Publishing Year",
y = "Total Units Sold",
color = "Variable") +
scale_color_manual(values = c("blue", "red")) +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
The visualization shows that as the years go on, there is continuous
growth with both units sold and total gross sales, however, the total
units sold has increased a lot more than the gross sales have.
#converting publishing year to numeric
total_sales_per_year$`Publishing Year` <- as.numeric(as.character(total_sales_per_year$`Publishing Year`))
#defining bootstrap function
bootstrap_correlation <- function(data, x_var, y_var, n_iter = 1000, confidence_level = 0.95) {
correlation_coefficients <- numeric(n_iter)
#simulate sampling n_iter times
for (i in 1:n_iter) {
sampled_data <- data[sample(1:nrow(data), replace = TRUE), ]
correlation_coefficients[i] <- cor(sampled_data[[x_var]], sampled_data[[y_var]])
}
mean_val <- mean(correlation_coefficients)
lower_ci <- quantile(correlation_coefficients, (1 - confidence_level) / 2)
upper_ci <- quantile(correlation_coefficients, 1 - (1 - confidence_level) / 2)
return(list(Mean = mean_val, Lower_CI = lower_ci, Upper_CI = upper_ci,
Bootstrapped_Values = correlation_coefficients))
}
bootstrap_units_sold_result <- bootstrap_correlation(total_sales_per_year, "Publishing Year", "Total_Units_Sold")
bootstrap_gross_sales_result <- bootstrap_correlation(total_sales_per_year, "Publishing Year", "Total_Gross_Sales")
cat("Correlation Coefficient (Total Units Sold) Mean:", bootstrap_units_sold_result$Mean, "\n")
## Correlation Coefficient (Total Units Sold) Mean: 0.6622259
cat("95% Confidence Interval:", bootstrap_units_sold_result$Lower_CI, "-", bootstrap_units_sold_result$Upper_CI, "\n\n")
## 95% Confidence Interval: 0.5986542 - 0.7208618
cat("Correlation Coefficient (Total Gross Sales) Mean:", bootstrap_gross_sales_result$Mean, "\n")
## Correlation Coefficient (Total Gross Sales) Mean: 0.6363773
cat("95% Confidence Interval:", bootstrap_gross_sales_result$Lower_CI, "-", bootstrap_gross_sales_result$Upper_CI, "\n")
## 95% Confidence Interval: 0.5498656 - 0.7148071
The correlation coefficient and confidence interval is suggesting that there is a fairly strong relationship between both units sold and gross sales with publishing year. As new books continue to be published, it is likely that they will see a steady rise in units of books sold and how much each book will gross. This certainly reflects what the visualation indicated.