Week 6 Data Dive

Pair 1

Examining the Relationship Between Average Book Rating and Author Rating

For the first pair, I wanted to see if there was a relationship between the author’s rating (novice, intermediate, excellent, or famous) and the book’s average rating. Although there are some instances where an author who has published multiple books has different ratings, I still felt as though this could provide interesting insight on if authors with a higher rating always have books with higher ratings.

To begin, I need to calculate the mean book average rating, grouped by each category of author rating.

#setting label order for visualization
label_order <- c("Novice", "Intermediate", "Excellent", "Famous")

#calculating the mean book average rating for each category of author rating
book_avg_per_category <- book_sales |>
  group_by(Author_Rating) |>
    mutate(Mean_Book_Avg_Rating = mean(Book_average_rating, na.rm = TRUE),
         Author_Rating = factor(Author_Rating, levels = label_order))

boxplot(book_avg_per_category$Book_average_rating ~ book_avg_per_category$Author_Rating, 
        xlab = "Author Rating", ylab = "Book Average Rating",
        main = "Book Average Rating by Author Rating")

#mean book average rating
points(book_avg_per_category$Author_Rating, book_avg_per_category$Mean_Book_Avg_Rating)

Based on the visualizaton, there does appear to be some relationship between the author’s rating and the average book rating for each author rating category. Novice author’s tend to have the lowest-rated books, intermediate authors (which is notably the most widely spread category) tend to have better book ratings than the novices, excellent authors tend to have better book ratings than intermediate authors, and finally, famous authors tend to have the highest book ratings.

#defining bootstrap function and confidence interval
bootstrap <- function(x, func = mean, n_iter = 1000, confidence_level = 0.95) {
  
  func_values <- numeric(n_iter)
  
  #simulate sampling n_iter times
  for (i in 1:n_iter) {
    x_sample <- sample(x, size = length(x), replace = TRUE)
    func_values[i] <- func(x_sample)
  }
  mean_val <- mean(func_values)
  lower_ci <- quantile(func_values, (1 - confidence_level) / 2)
  upper_ci <- quantile(func_values, 1 - (1 - confidence_level) / 2)
  return(list(Mean = mean_val, Lower_CI = lower_ci, Upper_CI = upper_ci, Bootstrapped_Values = func_values))
}

#getting unique categories
unique_author_ratings <- unique(book_avg_per_category$Author_Rating)

#loop through each category
for (author_rating in unique_author_ratings) {
  cat("Author Rating:", author_rating, "\n")
  
  #turn each category into a subset
  subset_data <- book_avg_per_category$Book_average_rating[book_avg_per_category$Author_Rating == author_rating]
  
  #apply bootstrap
  bootstrap_result <- bootstrap(subset_data)
  
  cat("Book Rating Mean:", bootstrap_result$Mean, "\n")
  cat("95% Confidence Interval:", bootstrap_result$Lower_CI, "-", bootstrap_result$Upper_CI, "\n\n")
}
## Author Rating: Novice 
## Book Rating Mean: 3.368713 
## 95% Confidence Interval: 3.331 - 3.403675 
## 
## Author Rating: Intermediate 
## Book Rating Mean: 3.908243 
## 95% Confidence Interval: 3.894328 - 3.922676 
## 
## Author Rating: Famous 
## Book Rating Mean: 4.439797 
## 95% Confidence Interval: 4.379995 - 4.488123 
## 
## Author Rating: Excellent 
## Book Rating Mean: 4.166642 
## 95% Confidence Interval: 4.156434 - 4.176391

The above means and confidence intervals for each category of author ratings certainly suggest that there is a notable difference between the book rating mean and the author’s level of expertise. This greatly aligns with what the visualization indicated.

Pair 2

Examining the Relationship the Number of Book Ratings and the Average Book Rating

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.

Pair 3

Examining the Evolution of Sales Through the Years

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.