# Packages list
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(visdat)
library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
library(readr)

1.

Correlation is a statistical measure that describes the size and direction of a relationship between two or more variables. The coefficient’s numerical values ranges from -1 to 1. If the correlation is negative, the two variables move in opposite direction. If it is positive, the two variables move in the same direction. If the correlation is 0, there is no relationship between the variables.

2.

Covariance is an indicator of the extend to which two variables are dependent on each other. The value of covariance lies between \(-\infty\) and \(+\infty\) . If it is positive, the two variables move in the same direction. If the correlation is 0, there is no relationship between the variables. The difference between correlation and covariance is that covariance is not unit-free and a change in scale affects it but it doesn’t affect correlation.

3.

stores <- read.csv("stores.csv")
sales <- read.csv("sales.csv")
products <- read.csv("products.csv")
inventory <- read.csv("inventory.csv")
vis_dat(stores)

vis_dat(sales , warn_large_data = FALSE)

vis_dat(products)

vis_dat(stores)

# This data set expands the product characteristics in the total sales data set 
sp <- 
merge(x = sales, 
      y = products, 
      by = c("Product_ID"))

sp$Product_Cost <- as.numeric(gsub('[$,]', '', sp$Product_Cost))
sp$Product_Price <- as.numeric(gsub('[$,]', '', sp$Product_Price))
sp$Product_ID <- as.character(sp$Product_ID)
sp$Store_ID <- as.character(sp$Store_ID)
sp$Sale_ID <- as.character(sp$Sale_ID)

4.

stargazer(sp, 
          type = "text",
          title= "Summary Statistics")
## 
## Summary Statistics
## ==================================================
## Statistic        N     Mean  St. Dev.  Min   Max  
## --------------------------------------------------
## Units         829,262 1.315   0.831     1     30  
## Product_Cost  829,262 9.976   7.818   1.990 34.990
## Product_Price 829,262 13.772  8.665   2.990 39.990
## --------------------------------------------------

5.

cov(sp$Units , sp$Product_Price)
## [1] -0.6934432
cor(sp$Units , sp$Product_Price)
## [1] -0.09634025

The negative covariance and variance indicate the type of relationship between this two variable. As unit increases, the price decreases. This aligns with the basic assumptions in economics for demand, and increase in price decreases the quantity demanded which can be seen here.