Advertisement
friedmusic

A13

Mar 5th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. ---
  2. title: "A13 by Noah Perry & Andrew Hall"
  3. output: html_notebook
  4. ---
  5. Use the provide SQL code for a basket of goods analysis to extract the frequency with which particular pairs of products occur in the same basket.
  6. Insert the SQL code using the ```{sql, connection = con, output.var = dataframe} feature of R markdown
  7. Create a heat map using geom_tile().
  8. Include code that can facilitate necessary adjustments to make the heat map useful.
  9.  
  10.  
  11. ```{r}
  12. library(tidyverse)
  13. library(kableExtra)
  14. library(DBI)
  15. library(scales)
  16. #online DB connect
  17. conn<-dbConnect(RMySQL::MySQL(),"richardtwatson.com",dbname="ClassicModels",user="student",password="student")
  18. ```
  19.  
  20. ```{r}
  21. normalize <- function(x){
  22. return((x-min(x)) / (max(x)-min(x)))
  23. }
  24. ```
  25.  
  26. ```{sql connection=conn, output.var = 'basket'}
  27. SELECT (SELECT productName FROM Products WHERE Products.productCode = OD1.productCode) AS `Product 1`,
  28. (SELECT productName FROM Products WHERE Products.productCode = OD2.productCode) AS `Product 2`,
  29. count(*) as Frequency
  30. FROM OrderDetails AS OD1 JOIN OrderDetails AS OD2 ON OD1.orderNumber = OD2.orderNumber
  31. WHERE OD1.productCode > OD2.productCode
  32. GROUP BY `Product 1`, `Product 2` HAVING Frequency > 10
  33. ORDER BY Frequency DESC, `Product 1`, `Product 2`;
  34. ```
  35.  
  36. ```{r cleaning}
  37. basket<-as_tibble(basket)
  38. basket_scaled<-basket %>% rename("product1" = `Product 1`,
  39. "product2" = `Product 2`,
  40. "pairFrequency" = Frequency) %>% arrange(product1)%>%
  41. mutate(pairFrequency = normalize(pairFrequency)) %>% slice(1:15)
  42. ```
  43. ```{r heatMap}
  44. ggplot(basket_scaled, aes(product1, product2 )) +
  45. geom_tile(aes(fill = pairFrequency), color = "white") +
  46. scale_fill_gradient(low = "red", high = "steelblue") +
  47. theme(axis.text.x = element_text(angle=45, vjust=1, hjust=1))
  48. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement