Week 10 Data Dive

Performing GLM

When choosing which binary column of data to model with a generalized linear model, I ultimately went with units sold, as I have already spent a lot of focus on trying to understand which factors impact the number of book units that get sold. Since units sold is count data, the use of log as the link function seemed suitable. Although it would impossible to have a negative number of units sold, I did find it important to add 1 to each of the units sold, as a book that has sold 0 units would cause issues when using the log link function.

book_sales$units_sold_adjusted <- book_sales$`units sold` + 1
#performing glm
glm_model <- glm(units_sold_adjusted ~ `Publishing Year` + Book_average_rating 
                 + genre + `sale price`, 
                 family = poisson(link = "log"),
                 data = book_sales)
summary(glm_model)
## 
## Call:
## glm(formula = units_sold_adjusted ~ `Publishing Year` + Book_average_rating + 
##     genre + `sale price`, family = poisson(link = "log"), data = book_sales)
## 
## Coefficients:
##                       Estimate Std. Error  z value Pr(>|z|)    
## (Intercept)          1.035e+01  6.045e-03 1712.632  < 2e-16 ***
## `Publishing Year`   -1.227e-04  1.485e-06  -82.575  < 2e-16 ***
## Book_average_rating -5.313e-02  1.262e-03  -42.091  < 2e-16 ***
## genrefiction        -7.401e-01  1.869e-03 -396.052  < 2e-16 ***
## genrenonfiction     -7.306e-01  1.993e-03 -366.591  < 2e-16 ***
## `sale price`         4.663e-04  8.803e-05    5.297 1.18e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 19427061  on 1068  degrees of freedom
## Residual deviance: 19293254  on 1063  degrees of freedom
##   (1 observation deleted due to missingness)
## AIC: 19303627
## 
## Number of Fisher Scoring iterations: 6

Each coefficient in the GLM output represents the expected changes in the log count of the adjusted units sold (which is units sold + 1). The model did not have the outputs that I would have anticipated in seeing. The log count of the adjusted number of units sold appears to decrease for increases in publishing year, average book rating, and genres. On the other hand, the log count of the adjusted number of units sold seems to increase in response in an increase in sales price.

Although a decrease in units sold for books published in more recent years, compared to books published tens or hundreds of years ago does make sense, I did not expect to see the deceases in relation to the average book rating or genres.

The P-values for each coefficent indicates that there is enough evidence to conclude that there is a significance between all of the chosen explanatory values and the number of book units sold.

Calculating Confidence Interval

#calculating c.i. using confit
ci <- confint(glm_model)
## Waiting for profiling to be done...
#getting the sales price coefficient
coefficient <- coef(glm_model)["`sale price`"]

#extracting the standard error for 'sale price'
standard_error <- summary(glm_model)$coefficients["`sale price`", "Std. Error"]

#get lower and upper bounds of the confidence interval
lower_bound <- coefficient - (1.96 * standard_error)  #1.96 as the critical value
upper_bound <- coefficient + (1.96 * standard_error)

cat("95% Confidence Interval for 'sale price': [", lower_bound, ", ", upper_bound, "]\n")
## 95% Confidence Interval for 'sale price': [ 0.0002937263 ,  0.0006388104 ]

In order to calculate the 95% confidence interval for the “sale price” column, I utilized 1.96 as my critical value. The result of this gave me a lower bound of 0.0002937263 and an upper bound of 0.0006388104. This means that I am 95% confident that for each increase in sales price, the log count of the adjusted units sold will increase by a value within the range of the lower and upper bound. It is reasonable to conclude that there is a relationship between the sales price of a book and the number of units that book may sell, but the sales price does not necessarily have a large effect.