require(mosaic)
## Loading required package: mosaic
## Registered S3 method overwritten by 'mosaic':
##   method                           from   
##   fortify.SpatialPolygonsDataFrame ggplot2
## 
## The 'mosaic' package masks several functions from core packages in order to add 
## additional features.  The original behavior of these functions should not be affected by this.
## 
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
## 
##     count, do, tally
## The following object is masked from 'package:Matrix':
## 
##     mean
## The following object is masked from 'package:ggplot2':
## 
##     stat
## The following objects are masked from 'package:stats':
## 
##     binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
##     quantile, sd, t.test, var
## The following objects are masked from 'package:base':
## 
##     max, mean, min, prod, range, sample, sum
require(tidytable)
## Loading required package: tidytable
## 
## Attaching package: 'tidytable'
## The following object is masked from 'package:stats':
## 
##     dt
## The following object is masked from 'package:base':
## 
##     %in%
require(dplyr)

library(dplyr)

library(readxl)

avocado_GIS <- read_excel("D:/MBA/MKT 6000/avocado_GIS.xlsx")
avocado.totalprice <- mutate(avocado_GIS, total.price = average_price*total_volume)

str(avocado.totalprice)
## tibble [12,628 × 9] (S3: tbl_df/tbl/data.frame)
##  $ date         : POSIXct[1:12628], format: "2017-12-03" "2017-12-03" ...
##  $ average_price: num [1:12628] 1.39 1.44 1.07 1.62 1.43 1.58 1.14 1.77 1.4 1.88 ...
##  $ total_volume : num [1:12628] 139970 3577 504933 10609 658939 ...
##  $ type         : chr [1:12628] "conventional" "organic" "conventional" "organic" ...
##  $ year         : num [1:12628] 2017 2017 2017 2017 2017 ...
##  $ city         : chr [1:12628] "Albany" "Albany" "Atlanta" "Atlanta" ...
##  $ state_code   : chr [1:12628] "NY" "NY" "GA" "GA" ...
##  $ Mileage      : num [1:12628] 2832 2832 2199 2199 2679 ...
##  $ total.price  : num [1:12628] 194558 5151 540278 17187 942283 ...
# Finding the city has largest sales of conventional avocados in the total dollar amount in 2017


avocado_conventional <- filter(avocado.totalprice, type == "conventional", year == "2017")
head(avocado_conventional)
## # A tibble: 6 × 9
##   date                averag…¹ total…² type   year city  state…³ Mileage total…⁴
##   <dttm>                 <dbl>   <dbl> <chr> <dbl> <chr> <chr>     <dbl>   <dbl>
## 1 2017-12-03 00:00:00     1.39  139970 conv…  2017 Alba… NY         2832 194558.
## 2 2017-12-03 00:00:00     1.07  504933 conv…  2017 Atla… GA         2199 540278.
## 3 2017-12-03 00:00:00     1.43  658939 conv…  2017 Balt… MD         2679 942283.
## 4 2017-12-03 00:00:00     1.14   86646 conv…  2017 Boise ID          827  98776.
## 5 2017-12-03 00:00:00     1.4   488588 conv…  2017 Bost… MA         2998 684023.
## 6 2017-12-03 00:00:00     1.13  153282 conv…  2017 Buff… NY         2552 173209.
## # … with abbreviated variable names ¹​average_price, ²​total_volume, ³​state_code,
## #   ⁴​total.price
avocado_conventional.sort <- arrange(avocado_conventional, desc(total.price))
View(avocado_conventional.sort)

# Finding the city has largest sales of organic avocados in the total dollar amount in 2017


avocado_organic <- filter(avocado.totalprice, type == "organic", year == "2017")

avocado_organic.sort <- arrange(avocado_organic, desc(total.price))
View(avocado_organic.sort)

# Finding the city has largest sales of conventional avocados in the total dollar amount in 2020

avocado_conventional.2 <- filter(avocado.totalprice, type == "conventional", year == "2020")

avocado_conventional.sort2 <- arrange(avocado_conventional.2, desc(total.price))

View(avocado_conventional.sort2)

# Finding the city has largest sales of organic avocados in the total dollar amount in 2020

avocado_organic.2 <- filter(avocado.totalprice, type == "organic", year == "2020")

avocado_organic.sort2 <- arrange(avocado_organic.2, desc(total.price))
View(avocado_organic.sort2)


#Price Elasticity with average_price

plot(total_volume ~ average_price,avocado_GIS) 
regr <- lm(total_volume ~ average_price,avocado_GIS )
abline(regr, col='red')

coefficients(regr)
##   (Intercept) average_price 
##     1179544.8     -628686.6
Beta <- regr$coefficients[["average_price"]]
P <- mean(avocado_GIS$average_price)
Q <- mean(avocado_GIS$total_volume)
elasticity <-Beta*P/Q
elasticity
## [1] -2.626474
#Price Elasticity with Total Price


plot(total_volume ~ total.price, avocado.totalprice)
regr1 <- lm(total_volume ~ total.price, avocado.totalprice)
abline(regr1, col='red')

coefficients(regr1)
##  (Intercept)  total.price 
## 2859.6201501    0.8838094
Beta1 <- regr1$coefficients[["total.price"]]
P1 <- mean(avocado.totalprice$total.price)
Q1 <- mean(avocado.totalprice$total_volume)
elasticity1 <-Beta1*P1/Q1
elasticity1
## [1] 0.9912082
ggplot(avocado.totalprice, aes(x = average_price, fill = type)) + 
  geom_histogram(bins = 30, col = "red") + 
  scale_fill_manual(values = c("blue", "green")) +
  ggtitle("Frequency of Average Price - Oragnic vs. Conventional")