-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud-services.Rmd
210 lines (158 loc) · 4.97 KB
/
cloud-services.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
title: "cloud-services"
author: "Dev"
date: '2022-10-07'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Libraries
```{r}
# loading the required packages
library(tidyverse) # for data wrangling and visualization
library(ggpubr) # theme_pubr()
library(broom) # for tidy model output
library(dfidx) # for indexed data frames
library(mlogit) # for multinomial logit
```
## Data
```{r}
# reading the data
cloud <- read.csv("cloud.csv" , stringsAsFactors = T)
```
## Data inspection and wrangling
```{r}
glimpse (cloud)
summary (cloud)
# Converting the attributes and setting the reference levels
cloud$cloud_storage <- relevel(cloud$cloud_storage , ref = '30gb')
cloud$price <- relevel(cloud$price , ref = 'p6')
# removing p from price and setting it as a numeric
cloud$price_n <- cloud %>%
mutate (price = gsub ("p" , "" , price) ,
price = as.numeric(price))
as.data.frame(cloud$price_n)
View (cloud$price_n)
price_n <- cloud$price_n$price
cloud <- cloud [ , 1:9]
cloud$price_n <- price_n
cloud$price_n <- as.numeric (cloud$price_n)
glimpse(cloud)
price_n
mean (price_n)
mean (cloud$price_n)
head (cloud)
table (cloud$cloud_storage)
table (cloud$cloud_services)
```
## Logistic regression
```{r}
# shape data for mlogit
m_data <- dfidx(cloud ,
choice = "choice" ,
idx = list (c("choice_id" , "respondent_id") ,
"alternative_id"))
m_data
# Estimate the model
set.seed (123)
# Build the model using m_data
model1 <- mlogit(choice ~ 0 + cloud_storage + customer_support + cloud_services + price ,
data = m_data)
corr1 <- summary (model1)$CoefTable
write.csv (corr1 , "corr1.csv")
```
## New model
```{r}
set.seed (123)
model2 <- mlogit(choice ~ 0 + cloud_storage + customer_support + cloud_services + price_n ,
data = m_data)
corr2 <- summary (model2)$CoefTable
write.csv (corr2 , "corr2.csv")
hist (cloud$price_n)
```
## Likelihood ratio test
```{r}
# likelihood ratio test
lrtest (model1 , model2)
```
## Prediction and probabilities
```{r}
# print the prediction for the first five choice sets in the data.
head (predict (model2 , m_data , 1))
# predict the choice probabilities for all different alternatives in the data
predicted_prob <- predict(model2 , m_data) %>%
as_tibble()
predicted_prob
# compute the predicted alternatives using the maximum choice probabilities.
predicted_alternatives <-
predicted_prob %>%
rowid_to_column("choiseset_id") %>%
pivot_longer(!choiseset_id , names_to = "choice" , values_to = "prob") %>%
group_by(choiseset_id) %>%
slice(which.max(prob)) %>%
ungroup() %>%
select(choice) %>%
as_vector()
```
## Confusion matrix
```{r}
# extract the selected alternatives from the data
selected_alternatives <- cloud %>%
filter (choice > 0) %>%
select (alternative_id) %>%
as_vector()
# confusion matrix
table (selected_alternatives , predicted_alternatives)
# Accuracy or hit rate
(579+624+614)/3000
```
## Conjoint simulator
```{r}
#build a custom function to predict market share for an arbitrary set of alternatives available in a data set “d”.
predict.share <- function(model2, d) {
temp <- model.matrix(update(model2$formula, 0 ~ .), data = d)[, -1] # generate dummy matrix
u <- temp %*% model2$coef[colnames(temp)] # calculate utilities
probs <- t(exp(u) / sum(exp(u))) # calculate probabilities
colnames(probs) <- paste("alternative", colnames(probs))
return(probs)
}
```
## A hypothetical base market structure
```{r}
# consider a hypothetical base market structure with 4 alternatives in the market.
cloud_storage <- c ("30gb" , "30gb" , "30gb" , "5000gb" , "5000gb")
customer_support <- c ("no" , "no" , "yes" , "yes" , "no")
cloud_services <- c ("email" , "email, video" , "email" , "email" , "email, video, productivity")
price_n <- c (6,12,12,18,18)
d_base <- cbind (cloud_storage , customer_support , cloud_services , price_n)
d_base <- d_base %>% as_tibble()
d_base$cloud_storage <- as.factor(d_base$cloud_storage)
d_base$customer_support <- as.factor(d_base$customer_support)
d_base$cloud_services <- as.factor(d_base$cloud_services)
d_base$price_n <- as.numeric(d_base$price_n)
# run the custom function and attach it's output
d_base <- cbind(d_base , as.vector(predict.share(model2 , d_base)))
d_base
cloud
# change the column name
colnames(d_base) [5] <- "predicted_share"
d_base
# make a copy of the d_base
d_new <- d_base
d_new [5 , 3] <- "email, video"
d_new
str(d_new)
d_new$predicted_share <- as.vector(predict.share(model2 , d_new))
d_new
```
# Calculating willingness to pay towards customer support
```{r}
- coef(model2) ['customer_supportyes']
# brand equity - dollar value of an upgrade from 30Gb to 2000 GB
- coef (model2) ['cloud_storage2000gb'] / coef (model2) ['price_n']
# brand equity - dollar value of an upgrade from 2000GB to 5000GB
- (coef (model2) ['cloud_storage5000gb'] - coef (model2) ['cloud_storage2000gb']) / coef (model2) ['price_n']
coef(model2)
```
----