# Plot of banner ad and purchase likelihood
library(ggplot2)

myData <- aggregate(consumers$Purchase,
                    by = list(Firm_banner = consumers$Firm_banner),
                    FUN = function(x) c(mean = mean(x), sd = sd(x),
                                        n = length(x)))

myData <- do.call(data.frame, myData)

myData$se <- myData$x.sd / sqrt(myData$x.n)
colnames(myData) <- c("Firm_banner", "mean", "sd", "n", "se")
myData$names <- c(paste(myData$Firm_banner, "Firm_banner"))

p <- ggplot(data = myData, aes(x = factor(Firm_banner), y = mean))
p + geom_bar(stat = "identity",
             position = position_dodge(0.9), fill="steelblue") +
  geom_errorbar(aes(ymax = mean + 2*se,
                    ymin = mean - 2*se), position = position_dodge(0.9),
                width = 0.25) +
  labs(x = "Firm_banner", y = "Conversion rate") +
  ggtitle("Conversion rate by firm_banner") +
  scale_y_continuous(labels = function(x) paste0(x*100, "%")) +
  geom_text(position = position_dodge(width= .9), aes(y = mean, label = paste(format(mean*100, digits = 2, nsmall = 2), "%", sep = "")), size = 4, vjust = 5)


# Plot of flyer and purchase likelihood
myData <- aggregate(consumers$Purchase,
                    by = list(Flyer_region = consumers$Flyer_region),
                    FUN = function(x) c(mean = mean(x), sd = sd(x),
                                        n = length(x)))

myData <- do.call(data.frame, myData)

myData$se <- myData$x.sd / sqrt(myData$x.n)
colnames(myData) <- c("Flyer_region", "mean", "sd", "n", "se")
myData$names <- c(paste(myData$Flyer_region, "Flyer_region"))

p <- ggplot(data = myData, aes(x = factor(Flyer_region), y = mean))
p + geom_bar(stat = "identity",
             position = position_dodge(0.9), fill="steelblue") +
  geom_errorbar(aes(ymax = mean + 2*se,
                    ymin = mean - 2*se), position = position_dodge(0.9),
                width = 0.25) +
  labs(x = "Flyer_region", y = "Conversion rate") +
  ggtitle("Conversion rate by Flyer_region") +
  scale_y_continuous(labels = function(x) paste0(x*100, "%")) +
  geom_text(position = position_dodge(width= .9), aes(y = mean, label = paste(format(mean*100, digits = 2, nsmall = 2), "%", sep = "")), size = 4, vjust = 5)


# Plot of banner ad + flyer and purchase likelihood
myData <- aggregate(consumers$Purchase,
                    by = list(Firm_banner = consumers$Firm_banner, Flyer_region = consumers$Flyer_region),
                    FUN = function(x) c(mean = mean(x), sd = sd(x),
                                        n = length(x)))

myData <- do.call(data.frame, myData)

myData$se <- myData$x.sd / sqrt(myData$x.n)
colnames(myData) <- c("Firm_banner", "Flyer_region", "mean", "sd", "n", "se")
myData$names <- c(paste(myData$Firm_banner, "Firm_banner /",
                        myData$Flyer_region, " Flyer_region"))

p <- ggplot(data = myData, aes(x = factor(Firm_banner), y = mean,
                               fill = factor(Flyer_region)))
p + geom_bar(stat = "identity",
             position = position_dodge(0.9)) +
  geom_errorbar(aes(ymax = mean + 2*se,
                    ymin = mean - 2*se), position = position_dodge(0.9),
                width = 0.25) +
  labs(x = "Firm_banner", y = "Conversion rate") +
  ggtitle("Conversion rate by firm_banner and flyer_region") +
  scale_fill_discrete(name = "Flyer region") +
  scale_y_continuous(labels = function(x) paste0(x*100, "%")) +
  geom_text(position = position_dodge(width= .9), aes(y = mean, label = paste(format(mean*100, digits = 2, nsmall = 2), "%", sep = "")), size = 4, vjust = 5)


# Load package to make an output table of all models
library(sjPlot)
library(sjmisc)
library(sjlabelled)

# Logistic regression model of banner ad treatment + flyer and purchase likelihood
model1 <- glm(Purchase ~ Firm_banner + Flyer_region, data=consumers, family=binomial)
model2 <- glm(Purchase ~ Firm_banner*Flyer_region, data=consumers, family=binomial)

tab_model(model1, model2, transform = NULL, collapse.ci = TRUE, p.style = "stars")




# Create some new variables
consumers$Banner_exposures <- consumers$Banner_no_click + consumers$Banner_click
consumers$Banner_seen <- ifelse(consumers$Banner_exposures==0,0,1)

# Plot of banner ad group + banner ad seen and purchase likelihood
myData <- aggregate(consumers$Purchase,
                    by = list(Banner_seen = consumers$Banner_seen, Firm_banner = consumers$Firm_banner),
                    FUN = function(x) c(mean = mean(x), sd = sd(x),
                                        n = length(x)))

myData <- do.call(data.frame, myData)

myData$se <- myData$x.sd / sqrt(myData$x.n)
colnames(myData) <- c("Banner_seen", "Firm_banner", "mean", "sd", "n", "se")
myData$names <- c(paste(myData$Banner_seen, "Banner_seen /",
                        myData$Firm_banner, " Firm_banner"))

p <- ggplot(data = myData, aes(x = factor(Banner_seen), y = mean,
                               fill = factor(Firm_banner)))
p + geom_bar(stat = "identity",
             position = position_dodge(0.9)) +
  geom_errorbar(aes(ymax = mean + 2*se,
                    ymin = mean - 2*se), position = position_dodge(0.9),
                width = 0.25) +
  labs(x = "Banner_seen", y = "Conversion rate") +
  ggtitle("Conversion rate by Banner_seen and Firm_banner") +
  scale_fill_discrete(name = "Firm_banner") +
  scale_y_continuous(labels = function(x) paste0(x*100, "%")) +
  geom_text(position = position_dodge(width= .9), aes(y = mean, label = paste(format(mean*100, digits = 2, nsmall = 2), "%", sep = "")), size = 4, vjust = 5)


# Logistic regression model of banner ad treatment + banner ad seen and purchase likelihood
model3 <- glm(Purchase ~ Firm_banner + Banner_seen + Firm_banner*Banner_seen, data=consumers, family=binomial)

tab_model(model1, model2, model3, transform = NULL, collapse.ci = TRUE, p.style = "stars")



# Plot of banner ad treatment + banner ad seen for new/potential customers
myData <- consumers[consumers$Existing_customer==0,]

myData <- aggregate(myData$Purchase,
                    by = list(Banner_seen = myData$Banner_seen, Firm_banner = myData$Firm_banner),
                    FUN = function(x) c(mean = mean(x), sd = sd(x),
                                        n = length(x)))

myData <- do.call(data.frame, myData)

myData$se <- myData$x.sd / sqrt(myData$x.n)
colnames(myData) <- c("Banner_seen", "Firm_banner", "mean", "sd", "n", "se")
myData$names <- c(paste(myData$Banner_seen, "Banner_seen /",
                        myData$Firm_banner, " Firm_banner"))

p <- ggplot(data = myData, aes(x = factor(Banner_seen), y = mean,
                               fill = factor(Firm_banner)))
p + geom_bar(stat = "identity",
             position = position_dodge(0.9)) +
  geom_errorbar(aes(ymax = mean + 2*se,
                    ymin = mean - 2*se), position = position_dodge(0.9),
                width = 0.25) +
  labs(x = "Banner_seen", y = "Conversion rate") +
  ggtitle("Conversion rate by Banner_seen and Firm_banner for potential/new customers") +
  scale_fill_discrete(name = "Firm_banner") +
  scale_y_continuous(labels = function(x) paste0(x*100, "%")) +
  geom_text(position = position_dodge(width= .9), aes(y = mean, label = paste(format(mean*100, digits = 2, nsmall = 2), "%", sep = "")), size = 4, vjust = 2)


# Plot of banner ad treatment + banner ad seen for existing customers
myData <- consumers[consumers$Existing_customer==1,]

myData <- aggregate(myData$Purchase,
                    by = list(Banner_seen = myData$Banner_seen, Firm_banner = myData$Firm_banner),
                    FUN = function(x) c(mean = mean(x), sd = sd(x),
                                        n = length(x)))

myData <- do.call(data.frame, myData)

myData$se <- myData$x.sd / sqrt(myData$x.n)
colnames(myData) <- c("Banner_seen", "Firm_banner", "mean", "sd", "n", "se")
myData$names <- c(paste(myData$Banner_seen, "Banner_seen /",
                        myData$Firm_banner, " Firm_banner"))

p <- ggplot(data = myData, aes(x = factor(Banner_seen), y = mean,
                               fill = factor(Firm_banner)))
p + geom_bar(stat = "identity",
             position = position_dodge(0.9)) +
  geom_errorbar(aes(ymax = mean + 2*se,
                    ymin = mean - 2*se), position = position_dodge(0.9),
                width = 0.25) +
  labs(x = "Banner_seen", y = "Conversion rate") +
  ggtitle("Conversion rate by Banner_seen and Firm_banner for existing customers") +
  scale_fill_discrete(name = "Firm_banner") +
  scale_y_continuous(labels = function(x) paste0(x*100, "%")) +
  geom_text(position = position_dodge(width= .9), aes(y = mean, label = paste(format(mean*100, digits = 2, nsmall = 2), "%", sep = "")), size = 4, vjust = 5)


# Logistic regression model of banner ad treatment + banner ad seen and purchase likelihood
model4 <- glm(Purchase ~ Firm_banner*Banner_seen*Existing_customer, data=consumers, family=binomial)

tab_model(model1, model2, model3, model4, transform = NULL, collapse.ci = TRUE, p.style = "stars")



# Plot of banner ad treatment + number of exposures and purchase likelihood
myData <- consumers

myData$Banner_exposures <- ifelse(myData$Banner_exposures > 6, 6, myData$Banner_exposures)
myData$Firm_banner <- as.factor(myData$Firm_banner)

myData <- aggregate(myData$Purchase,
                    by = list(Firm_banner = myData$Firm_banner, Banner_exposures = myData$Banner_exposures),
                    FUN = function(x) c(mean = mean(x), sd = sd(x),
                                        n = length(x)))

myData <- do.call(data.frame, myData)

myData$se <- myData$x.sd / sqrt(myData$x.n)
colnames(myData) <- c("Firm_banner", "Banner_exposures", "mean", "sd", "n", "se")
myData$names <- c(paste(myData$Firm_banner, "Firm_banner /",
                        myData$Banner_exposures, " Banner_exposures"))

p <- ggplot(data = myData, aes(x = Banner_exposures, y = mean,
                               group = as.factor(Firm_banner), color = Firm_banner))
p + geom_line() +
  geom_point() +
  geom_errorbar(aes(ymin=mean - 2*se, ymax=mean + 2*se), width=.2,
                position=position_dodge(0.05)) +
  labs(x = "Number of banner exposures", y = "Conversion rate") +
  scale_y_continuous(labels = function(x) paste0(x*100, "%")) +
  ggtitle("Conversion rate by number of ad exposures") +
  geom_text(position = position_dodge(width= .9), aes(y = mean, label = paste(format(mean*100, digits = 2, nsmall = 2), "%", sep = "")), size = 4, vjust = -1)


# Logistic regression model of banner ad treatment + number of exposures and purchase likelihood
model5 <- glm(Purchase ~ Firm_banner*log(Banner_exposures+1), data=consumers, family=binomial)

tab_model(model1, model2, model3, model4, model5, transform = NULL, collapse.ci = TRUE, p.style = "stars")
