commit
7fd27a0f57
13 changed files with 812 additions and 0 deletions
@ -0,0 +1,4 @@ |
|||
.Rproj.user |
|||
.Rhistory |
|||
.RData |
|||
.Ruserdata |
|||
@ -0,0 +1,130 @@ |
|||
--- |
|||
title: "Analysis" |
|||
format: html |
|||
editor: visual |
|||
--- |
|||
|
|||
# Setup |
|||
|
|||
```{r} |
|||
library(dplyr) |
|||
library(cluster) |
|||
library(tidyr) |
|||
library(highcharter) |
|||
library(ggplot2) |
|||
``` |
|||
|
|||
|
|||
# Data |
|||
|
|||
```{r} |
|||
dat<-read.csv("Data.csv", colClasses = c("character","factor", "factor", |
|||
"numeric","numeric","numeric", |
|||
"numeric","numeric","numeric")) |
|||
dat[is.na(dat)]<-0 |
|||
``` |
|||
|
|||
|
|||
```{r} |
|||
# Select the columns for clustering (including "Role" and "Location") |
|||
selected_cols <- c("Self.Purchase1", "Self.Purchase2", "Self.Purchase3", "Other.Purchase1", "Other.Purchase2", "Other.Purchase3", "Role", "Location") |
|||
|
|||
# Subset the data frame to include only the selected columns |
|||
df_subset <- dat[selected_cols] |
|||
|
|||
# Convert the "Role" and "Location" columns to factors (if they are not already) |
|||
df_subset$Role <- as.factor(df_subset$Role) |
|||
df_subset$Location <- as.factor(df_subset$Location) |
|||
|
|||
# Perform one-hot encoding for "Role" and "Location" |
|||
df_encoded <- model.matrix(~Role + Location - 1, data = df_subset) # -1 removes intercept terms |
|||
|
|||
numeric_cols <- dat[, c("Self.Purchase1", "Self.Purchase2", "Self.Purchase3", "Other.Purchase1", "Other.Purchase2", "Other.Purchase3")] |
|||
scaled_data <- scale(numeric_cols) |
|||
final_data <- cbind(scaled_data, df_encoded) |
|||
|
|||
wss <- numeric(length = 10) |
|||
for (i in 1:10) { |
|||
kmeans_result <- kmeans(final_data, centers = i, nstart = 10) |
|||
wss[i] <- sum(kmeans_result$tot.withinss) |
|||
} |
|||
|
|||
plot(1:10, wss, type = "b", xlab = "Number of Clusters", ylab = "Within-cluster Sum of Squares") |
|||
``` |
|||
|
|||
|
|||
```{r} |
|||
optimal_k <- 4 |
|||
|
|||
kmeans_result <- kmeans(final_data, centers = optimal_k, nstart = 10) |
|||
|
|||
dat$Cluster <- kmeans_result$cluster |
|||
|
|||
``` |
|||
|
|||
|
|||
```{r} |
|||
summary(dat[dat$Cluster==1,]) |
|||
filter.curr<-dat |> |
|||
filter(Location=="Curacao") |> |
|||
summarize(yr.1=sum(Self.Purchase1)+sum(Other.Purchase1), |
|||
yr.2=sum(Self.Purchase2)+sum(Other.Purchase2), |
|||
yr.3=sum(Self.Purchase3)+sum(Other.Purchase3) |
|||
) |> |
|||
pivot_longer(c(yr.1,yr.2,yr.3)) |> |
|||
rename(Year=name, Revenue=value) |
|||
|
|||
hchart(filter.curr, |
|||
"column", |
|||
hcaes(x=Year,y=Revenue)) |> |
|||
hc_title(text="Revenue from self purchase") |
|||
|
|||
unique(dat$Role) |
|||
|
|||
write.csv(dat,"DataSegmented.csv",row.names = FALSE) |
|||
|
|||
dat |> |
|||
summarise(current.year=sum(Self.Purchase3), |
|||
last.year=sum(Self.Purchase2)) |
|||
library(ggplot2) |
|||
|
|||
# Create a scatter plot to visualize the clusters |
|||
summ.dat<-dat |> |
|||
group_by(Cluster) |> |
|||
mutate( |
|||
self=sum(Self.Purchase1)+sum(Self.Purchase2)+sum(Self.Purchase3), |
|||
other=sum(Other.Purchase1)+sum(Other.Purchase2)+sum(Other.Purchase3) |
|||
) |
|||
hchart(summ.dat, type = "scatter", hcaes(x = self, y = other, color = factor(Cluster))) %>% |
|||
hc_plotOptions( |
|||
scatter = list(jitter = list(x = 10000000, y = 10000000)) |
|||
) %>% |
|||
hc_title(text = "K-means Clustering Visualization") %>% |
|||
hc_xAxis(title = list(text = "Self")) %>% |
|||
hc_yAxis(title = list(text = "Other")) |
|||
``` |
|||
|
|||
|
|||
|
|||
```{r} |
|||
dat |> |
|||
group_by(Cluster) |> |
|||
summarise( |
|||
self=sum(Self.Purchase1)+sum(Self.Purchase2)+sum(Self.Purchase3), |
|||
other=sum(Other.Purchase1)+sum(Other.Purchase2)+sum(Other.Purchase3) |
|||
) |> |
|||
pivot_longer(c(self,other)) |> |
|||
hchart("column", hcaes(x=Cluster, y=value, group=name), stacking="normal") |> |
|||
hc_colors(c("#0073C2FF", "#EFC000FF")) |> |
|||
hc_title(text="Revenue by segment") |> |
|||
hc_xAxis(title=list(text="Segment")) |> |
|||
hc_yAxis(title=list(text="Revenue")) |
|||
``` |
|||
|
|||
|
|||
```{r} |
|||
|
|||
``` |
|||
|
|||
|
|||
|
|||
@ -0,0 +1,13 @@ |
|||
Version: 1.0 |
|||
|
|||
RestoreWorkspace: Default |
|||
SaveWorkspace: Default |
|||
AlwaysSaveHistory: Default |
|||
|
|||
EnableCodeIndexing: Yes |
|||
UseSpacesForTab: Yes |
|||
NumSpacesForTab: 2 |
|||
Encoding: UTF-8 |
|||
|
|||
RnwWeave: Sweave |
|||
LaTeX: pdfLaTeX |
|||
@ -0,0 +1,2 @@ |
|||
|
|||
dat<-read.csv("DataSegmented.csv") |
|||
@ -0,0 +1,66 @@ |
|||
|
|||
# Function to summarize & reshape data |
|||
|
|||
summarizer <- function(dat, location, role, self=TRUE){ |
|||
if(self==TRUE){ |
|||
filter.curr<- |
|||
dat |> |
|||
filter(Location %in% location) |> |
|||
filter(Role %in% role) |> |
|||
summarize(year.1=sum(Self.Purchase1), |
|||
year.2=sum(Self.Purchase2), |
|||
year.3=sum(Self.Purchase3) |
|||
) |> |
|||
pivot_longer(c(year.1,year.2,year.3)) |> |
|||
rename(Year=name, Revenue=value) |
|||
} |
|||
|
|||
if(self==FALSE){ |
|||
filter.curr<- |
|||
dat |> |
|||
filter(Location %in% location) |> |
|||
filter(Role %in% role) |> |
|||
summarize( |
|||
year.1=sum(Other.Purchase1), |
|||
year.2=sum(Other.Purchase2), |
|||
year.3=sum(Other.Purchase3) |
|||
) |> |
|||
pivot_longer(c(year.1,year.2,year.3)) |> |
|||
rename(Year=name, Revenue=value) |
|||
} |
|||
|
|||
return(filter.curr) |
|||
} |
|||
|
|||
# Snapshot |
|||
|
|||
snapshot<- function(dat, location, role, item="self"){ |
|||
if(item=="self"){ |
|||
filter.curr<- |
|||
dat |> |
|||
filter(Location %in% location) |> |
|||
filter(Role %in% role) |> |
|||
summarise(current.year=sum(Self.Purchase3), |
|||
last.year=sum(Self.Purchase2)) |
|||
} |
|||
|
|||
if(item=="other"){ |
|||
filter.curr<- |
|||
dat |> |
|||
filter(Location %in% location) |> |
|||
filter(Role %in% role) |> |
|||
summarise(current.year=sum(Other.Purchase3), |
|||
last.year=sum(Other.Purchase2)) |
|||
} |
|||
|
|||
if(item=="total"){ |
|||
filter.curr<- |
|||
dat |> |
|||
filter(Location %in% location) |> |
|||
filter(Role %in% role) |> |
|||
summarise(current.year=sum(Other.Purchase3)+sum(Self.Purchase3), |
|||
last.year=sum(Other.Purchase2)+sum(Self.Purchase2)) |
|||
} |
|||
|
|||
return(filter.curr) |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
# Module to create a stacked bar chart in highcharter |
|||
|
|||
barplotUI<-function(id){ |
|||
ns<-NS(id) |
|||
tagList( |
|||
h4("Cluster"), |
|||
highchartOutput(ns("barplot")) |
|||
) |
|||
} |
|||
|
|||
|
|||
barplotServer<-function(id, data){ |
|||
moduleServer(id, function(input, output, session){ |
|||
output$barplot<-renderHighchart({ |
|||
data|> |
|||
group_by(Cluster)|> |
|||
summarise(n=n())|> |
|||
hchart("bar", hcaes(x=Cluster, y=n))|> |
|||
hc_plotOptions(bar=list(stacking="normal")) |
|||
}) |
|||
}) |
|||
} |
|||
|
|||
@ -0,0 +1,18 @@ |
|||
|
|||
# SHiny module to show data frame using DT |
|||
datatableUI<- function(id){ |
|||
ns <- NS(id) |
|||
tagList( |
|||
h4("Full Data"), |
|||
DT::dataTableOutput(ns("datatable")) |
|||
) |
|||
|
|||
} |
|||
|
|||
datatableServer<- function(id, df){ |
|||
moduleServer(id, function(input, output, session){ |
|||
output$datatable <- DT::renderDataTable({ |
|||
DT::datatable(df) |
|||
}) |
|||
}) |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
# Module to plot histogram using highcharter |
|||
|
|||
histoUI<-function(id){ |
|||
ns<-NS(id) |
|||
tagList( |
|||
selectizeInput(ns("selreve"), "Select", choices = c("self","other"), selected=c("self")), |
|||
highchartOutput(ns("histoplot")) |
|||
) |
|||
} |
|||
|
|||
# Moduleserver |
|||
|
|||
|
|||
#Server |
|||
histoServer<-function(id, df, segment){ |
|||
|
|||
moduleServer(id, function(input, output, session){ |
|||
|
|||
output$histoplot<-renderHighchart({ |
|||
summ.dat<- df |> |
|||
filter(Cluster==segment) |> |
|||
mutate( |
|||
self=Self.Purchase1+Self.Purchase2+Self.Purchase3, |
|||
other=Other.Purchase1+Other.Purchase2+Other.Purchase3 |
|||
) |
|||
|
|||
if(input$selreve=="self"){ |
|||
revenue<-summ.dat$self |
|||
} |
|||
if(input$selreve=="other"){ |
|||
revenue<-summ.dat$other |
|||
} |
|||
#create histogram |
|||
hchart(revenue) |> |
|||
hc_title(text = "Revenue Distribution") |> |
|||
hc_xAxis(title = list(text = "Revenue")) |> |
|||
hc_yAxis(title = list(text = "Count")) |
|||
}) |
|||
}) |
|||
|
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
infocardUI<- function(id){ |
|||
ns<- NS(id) |
|||
uiOutput(ns("infocard")) |
|||
} |
|||
|
|||
infocardServer<- function(id, Value, Title="", Stat=NULL, Description="", Icon, Icon_bg="default", BGC){ |
|||
moduleServer( |
|||
id, |
|||
function(input, output, session){ |
|||
|
|||
output$infocard<- renderUI({ |
|||
req(Value,Stat) |
|||
argonInfoCard( |
|||
value=Value, |
|||
title = Title, |
|||
stat = Stat, |
|||
stat_icon = NULL, |
|||
description = Description, |
|||
icon=Icon, |
|||
icon_background = Icon_bg, |
|||
hover_lift = TRUE, |
|||
shadow = FALSE, |
|||
background_color = BGC, |
|||
gradient = TRUE, |
|||
width = 12 |
|||
) |
|||
}) |
|||
} |
|||
) |
|||
} |
|||
@ -0,0 +1,168 @@ |
|||
|
|||
revplotUI<- function(id,filtervalues){ |
|||
ns<- NS(id) |
|||
tagList( |
|||
fluidRow( |
|||
# Add to select inputs side by side |
|||
column(width = 3, |
|||
selectizeInput(inputId = ns("geo"), label="Select Geography", choices= c("Aruba","Bonair","Poland", |
|||
"Curacao", "Netherlands"), |
|||
selected=c("Aruba","Bonair","Poland", |
|||
"Curacao", "Netherlands"), multiple=T)), |
|||
column(width = 3, |
|||
selectizeInput(inputId = ns("role"), label="Select Role", choices= c("Customer","Customer - Influencer","Poland", |
|||
"Financer", "Influencer"), |
|||
c("Customer","Customer - Influencer","Poland", |
|||
"Financer", "Influencer"), multiple=T)), |
|||
column(width = 6, |
|||
shinycssloaders::withSpinner(valueBoxOutput(ns("selfinfo"))), |
|||
shinycssloaders::withSpinner(valueBoxOutput(ns("othinfo"))), |
|||
shinycssloaders::withSpinner(valueBoxOutput(ns("totinfo"))), |
|||
) |
|||
), |
|||
fluidRow( |
|||
column(width = 4, |
|||
shinycssloaders::withSpinner(highchartOutput(ns("revselfbar")) , image = "https://media.giphy.com/media/RLsct1jsRVsVFupW7a/giphy.gif", image.height ="200px",hide.ui = T) |
|||
), |
|||
column(width = 4, |
|||
shinycssloaders::withSpinner(highchartOutput(ns("revothbar")) , image = "https://media.giphy.com/media/RLsct1jsRVsVFupW7a/giphy.gif", image.height ="200px",hide.ui = T) |
|||
), |
|||
column(width = 4, |
|||
shinycssloaders::withSpinner(highchartOutput(ns("revtotbar")) , image = "https://media.giphy.com/media/RLsct1jsRVsVFupW7a/giphy.gif", image.height ="200px",hide.ui = T) |
|||
) |
|||
) |
|||
) |
|||
} |
|||
|
|||
revplotServer<- function(id, df, xtitle="", ytitle=""){ |
|||
moduleServer( |
|||
id, |
|||
function(input, output, session){ |
|||
|
|||
sel.geo<-reactive({ |
|||
print(input$geo) |
|||
input$geo |
|||
}) |
|||
sel.role<-reactive({ |
|||
print(input$role) |
|||
input$role |
|||
}) |
|||
summarized.df <- reactive(summarizer(df, sel.geo(), sel.role(), self=TRUE)) |
|||
summarized.df.oth <- reactive(summarizer(df, sel.geo(), sel.role(), self=FALSE)) |
|||
output$revselfbar<- renderHighchart({ |
|||
print(summarized.df()) |
|||
hchart(summarized.df(), |
|||
"column", |
|||
hcaes(x=Year,y=Revenue)) |> |
|||
hc_title(text="Revenue from self purchase") |> |
|||
# hc_yAxis(title = list(text = ytitle)) |> |
|||
#hc_plotOptions(column = list(stacking = "normal")) |> |
|||
hc_tooltip(shared = TRUE, valueDecimals = 0) |> |
|||
#hc_brush(enabled = TRUE) |> |
|||
hc_credits(enabled = FALSE) |> |
|||
hc_exporting(enabled = TRUE) |
|||
|
|||
} |
|||
) |
|||
|
|||
output$revothbar<- renderHighchart({ |
|||
print(summarized.df.oth()) |
|||
hchart(summarized.df.oth(), |
|||
"column", |
|||
hcaes(x=Year,y=Revenue)) |> |
|||
hc_title(text="Revenue from purchase by influencer") |> |
|||
# hc_yAxis(title = list(text = ytitle)) |> |
|||
#hc_plotOptions(column = list(stacking = "normal")) |> |
|||
hc_tooltip(shared = TRUE, valueDecimals = 0) |> |
|||
#hc_brush(enabled = TRUE) |> |
|||
hc_credits(enabled = FALSE) |> |
|||
hc_exporting(enabled = TRUE) |
|||
|
|||
} |
|||
) |
|||
|
|||
output$revtotbar<- renderHighchart({ |
|||
summarized.df.tot<-df |> |
|||
filter(Location %in% sel.geo()) |> |
|||
filter(Role %in% sel.role()) |> |
|||
summarize(yr.1=sum(Self.Purchase1)+sum(Other.Purchase1), |
|||
yr.2=sum(Self.Purchase2)+sum(Other.Purchase2), |
|||
yr.3=sum(Self.Purchase3)+sum(Other.Purchase3) |
|||
) |> |
|||
pivot_longer(c(yr.1,yr.2,yr.3)) |> |
|||
rename(Year=name, Revenue=value) |
|||
hchart(summarized.df.tot, |
|||
"column", |
|||
hcaes(x=Year,y=Revenue)) |> |
|||
hc_title(text="Total Revenue") |> |
|||
# hc_yAxis(title = list(text = ytitle)) |> |
|||
#hc_plotOptions(column = list(stacking = "normal")) |> |
|||
hc_tooltip(shared = TRUE, valueDecimals = 0) |> |
|||
#hc_brush(enabled = TRUE) |> |
|||
hc_credits(enabled = FALSE) |> |
|||
hc_exporting(enabled = TRUE) |
|||
|
|||
} |
|||
) |
|||
|
|||
|
|||
selfinfo.df<- reactive({ |
|||
snapshot(df, sel.geo(), sel.role(), item="self") |
|||
}) |
|||
|
|||
otherinfo.df<- reactive({ |
|||
snapshot(df, sel.geo(), sel.role(), item="other") |
|||
}) |
|||
|
|||
totalinfo.df<- reactive({ |
|||
snapshot(df, sel.geo(), sel.role(), item="total") |
|||
}) |
|||
|
|||
output$selfinfo<- renderValueBox({ |
|||
delta<-round((selfinfo.df()$current.year-selfinfo.df()$last.year)*100/selfinfo.df()$last.year) |
|||
if(delta<0){ |
|||
icon<-icon("arrow-down") |
|||
} else { |
|||
icon<-icon("arrow-up") |
|||
} |
|||
valueBox( |
|||
value = paste0(delta," %"), |
|||
subtitle = "Self", |
|||
icon = icon, |
|||
color = ifelse(delta>0, "green", "red") |
|||
) |
|||
}) |
|||
|
|||
output$othinfo<- renderValueBox({ |
|||
delta<-round((otherinfo.df()$current.year-otherinfo.df()$last.year)*100/otherinfo.df()$last.year) |
|||
if(delta<0){ |
|||
icon<-icon("arrow-down") |
|||
} else { |
|||
icon<-icon("arrow-up") |
|||
} |
|||
valueBox( |
|||
value = paste0(delta," %"), |
|||
subtitle = "Other", |
|||
icon = icon, |
|||
color = ifelse(delta>0, "green", "red") |
|||
) |
|||
}) |
|||
|
|||
output$totinfo<- renderValueBox({ |
|||
delta<-round((totalinfo.df()$current.year-totalinfo.df()$last.year)*100/totalinfo.df()$last.year) |
|||
if(delta<0){ |
|||
icon<-icon("arrow-down") |
|||
} else { |
|||
icon<-icon("arrow-up") |
|||
} |
|||
valueBox( |
|||
value = paste0(delta," %"), |
|||
subtitle = "Total", |
|||
icon = icon, |
|||
color = ifelse(delta>0, "green", "red") |
|||
) |
|||
}) |
|||
|
|||
} |
|||
) |
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
# Shiny Module to show scatter plot using highcharter |
|||
|
|||
scatterUI<-function(id){ |
|||
ns<-NS(id) |
|||
tagList( |
|||
highchartOutput(ns("scatterPlot")) |
|||
) |
|||
} |
|||
|
|||
# Server |
|||
|
|||
scatterServer<-function(id,df){ |
|||
moduleServer(id,function(input,output,session){ |
|||
output$scatterPlot<-renderHighchart({ |
|||
summ.dat<-df |> |
|||
group_by(Cluster) |> |
|||
mutate( |
|||
self=sum(Self.Purchase1)+sum(Self.Purchase2)+sum(Self.Purchase3), |
|||
other=sum(Other.Purchase1)+sum(Other.Purchase2)+sum(Other.Purchase3) |
|||
) |
|||
hchart(summ.dat, type = "scatter", hcaes(x = self, y = other, color = factor(Cluster))) |> |
|||
hc_plotOptions( |
|||
scatter = list(jitter = list(x = 10000000, y = 10000000)) |
|||
) |> |
|||
hc_title(text = "K-means Clustering Visualization") |> |
|||
hc_xAxis(title = list(text = "Self")) |> |
|||
hc_yAxis(title = list(text = "Other")) |> |
|||
# add Cluster as hover info |
|||
hc_tooltip(pointFormat = "Cluster: {point.Cluster}") |
|||
|
|||
|
|||
}) |
|||
} |
|||
) |
|||
} |
|||
Loading…
Reference in new issue