Repository navigation
Expand file tree
/
Copy pathAssignment_IV_Sastre_Paola(1).R
More file actions
244 lines (190 loc) · 7.1 KB
/
Copy pathAssignment_IV_Sastre_Paola(1).R
File metadata and controls
244 lines (190 loc) · 7.1 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#*************************************************************
#### Advanced Econometrics - Assignment 4 #####
## Paola Sastre Dordelly
#*************************************************************
####MARKETING CAMPAING OPTIMIZATION####
### 0. HOUSEKEEPING ####
#***********************
## 0.1 Clear session ####
rm(list = ls()) # clear workspace
if(!is.null(dev.list())) dev.off() # Clear previous plots
cat("\014") # clear command window
## 0.2 Read the files ####
setwd("~/Desktop/Econometría:Assignments")
data1<-read.csv("marketing_data.csv")
data2<-read.csv("pricing_data.csv")
sales<-data1$s
google_exp<-data.frame(data1$g)
youtube_exp<-data.frame(data1$y)
# 1.1 Plot the available data on sales with the google search expenditures
# and again the sales but this time with the youtube expenditures.
# Estimating the parameters of the nonlinear dynamic time-varying parameter model
# yields to the following maximum likelihood parameter estimates.
mu <-0.617
epsilon1 <- 2.88
epsilon2 <- 9.51
delta1 <- 0.27
delta2 <- 0.60
alpha1 <- 0.203
alpha2 <- 0.065
beta1 <- 0.949
beta2 <- 0.95
adstock_google<-data.frame(
google_exp,
sales
)
adstock_youtube<-data.frame(
youtube_exp,
sales
)
T<-length(google_exp$data1.g)
## Plot observed data and forecast
google_plot <- ggplot() +
geom_point(data = adstock_google, aes(x = sales, y = google_exp), color = "black")+
labs(title="Google Expenditures") +xlab("") + ylab("")
google_plot+ scale_x_discrete(labels=c('s-2','s-1','s','s+1','s+2','s+3','s+4','s+5','s+6','s+7','s+8','s+9','s+10'))
google_plot
## Plot observed data and forecast
youtube_plot <- ggplot() +
geom_point(data = adstock_youtube, aes(x = sales, y = youtube_exp), color = "red")
youtube_plot+labs(title="Youtube Expenditures") +xlab("sales") + ylab("Euros spent")
# 2. Plot the filtered adstock generated by marketing on Google search
# and Youtube.
# 2.1 adstock google
T<-2443
adstock_google <- matrix(c(0:0), T, 1)
adstock_google [1] <- google_exp[1,1];
for (t in 1:T){
adstock_google[t+1] = beta1*adstock_google[t]+alpha1*google_exp[t+1,1]
}
adstock_google_df<-as.data.frame(adstock_google)
# 2.1.1.adstock google plot
gads_plot <- ggplot() +
geom_line(data = adstock_google_df, aes(x = (1:2444), y = adstock_google), color = "blue")
gads_plot+labs(title="Adstock generated by marketing Google") +xlab("") + ylab("")
# 2.2 adstock youtube
adstock_youtube <- matrix(c(0:0), T, 1)
adstock_youtube [1] <- youtube_exp[1,1];
for (t in 1:T){
adstock_youtube[t+1] = beta2*adstock_youtube[t]+alpha2*youtube_exp[t+1,1]
}
adstock_youtube_df<-as.data.frame(adstock_youtube)
# 2.2.1 adstock youtube plot
yads_plot <- ggplot() +
geom_line(data = adstock_youtube_df, aes(x = (1:2444), y = adstock_youtube), color = "red")
yads_plot+labs(title="Adstock generated by marketing youtube") +xlab("") + ylab("")
# 2.3 both plots
gyads_plot <- ggplot() +
geom_line(data = adstock_google_df, aes(x = (1:2444), y = adstock_google), color = "blue")+
geom_line(data = adstock_youtube_df, aes(x = (1:2444), y = adstock_youtube), color = "red")
gyads_plot+labs(title="Adstock generated by Marketing: Google and Youtube ") +xlab("Observed hours") + ylab("sales")
#filtered adstock?
N<-10
# 3. Use the fitted model to plot impulse response functions for both
# the adstock and sales volume generated by spending 100 euros with marketing
# on Youtube on a given hour
## 3.1 Parameter Values
s = 10 # Set shock time s
x0 = 0 # Set origin value
e = 100 # Set shock size e
h = 40 # Set steps ahead
## 3.2 Generate Innovations
eps <- matrix(0, s+h, N)
for (t in 1:(s+h)){
eps[t,] = 0.1*rnorm(N)
} # Generate N values of epsilon_T+1
## 3.3 Generate IRF
IRF <- matrix(0, s+h+1, N)
IRF[1:s-1,] = x0*matrix(1, s-1, N)
IRF[s,] = (x0+e)*matrix(1, 1 , N)
for (t in (s+1):(s+h)) {
IRF[t] <- mu+(epsilon1*adstock_google[t]^delta1)+(epsilon2*adstock_youtube[t]^delta2)
}
# 3.4 Calculate point forecasts (conditional mean) and confidence bounds (5 and
# 95 percentiles) recursively
IRF_hat = matrix(0, (s+h), 1)
upper_bound = matrix(0, (s+h),1)
lower_bound= matrix(0, (s+h),1)
#x_hat[1] = x_T
#upper_bound[1]= x_T
#lower_bound[1]= x_T
for (t in 1:(s+h)) {
IRF_hat[t] = mean(IRF[t,])
upper_bound[t] = quantile(IRF_hat[t,],.90)
lower_bound[t] = quantile(IRF_hat[t,],.10)
}
# 3.5 Plot Data
IRF_hatt <- data.frame(
h_ax = 1:(h+s),
IRF_hat = IRF_hat
)
lower_b <- data.frame(
h_ax = 1:(h+s),
low_b = lower_bound
)
upper_b <- data.frame(
h_ax = 1:(h+s),
upp_b = upper_bound
)
p1 = ggplot() +
geom_line(data = lower_b, aes(x = h_ax, y = low_b), color = "red") +
geom_line(data = upper_b,aes(x = h_ax, y = upp_b),color = "red") +
geom_line(data = IRF_hatt, aes(x = h_ax, y = IRF_hat), color = "black")
p1 + scale_x_discrete(labels=c('s-2','s-1','s','s+1','s+2','s+3','s+4','s+5','s+6','s+7','s+8','s+9','s+10'))
####DYNAMIC PRICING####
# ***********************
# A retailer of electronic products has called upon you to help design a data-driven procedure
# for optimizing the prices for their products. The activity of this company consists
# essentially of buying consumer electronics (e.g. cell phones, laptops, etc.) from large manufacturers
# (Apple, Samsung, etc.) and selling these products directly to consumers both
# online and on their shops
# 1. Plot the available data on sales, prices, acquisition costs and marketing
# expenditures.
M<-length(data2$s)
sales_plot <- ggplot() +
geom_line(data = data2, aes(x = 1:M, y = data2$s), color = "black")+
labs(title="De-seasonnalized sales") +xlab("Time") + ylab("")
sales_plot
price_plot <- ggplot() +
geom_line(data = data2, aes(x = 1:M, y = data2$p), color = "red")+
labs(title="Selling price") +xlab("Time") + ylab("")
price_plot
marketing_plot<- ggplot() +
geom_line(data = data2, aes(x = 1:M, y = data2$m), color = "dark blue")+
labs(title="Marketing expenditures") +xlab("Time") + ylab("")
marketing_plot
cost_plot<- ggplot() +
geom_line(data = data2, aes(x = 1:M, y = data2$c), color = "orange")+
labs(title="Acquisition Costs") +xlab("Time") + ylab("")
cost_plot
names(data2)
sales<-data2$s
price<-data2$p
cost<-data2$c
marketing<-data2$m
# 2. Run a simple regression of sales st on prices pt, and report the estimates
# 2.1 Simple OLS regression
Model1<-lm(sales~price)
Model1
stargazer(Model1, type = "text")
# 3. Concerned about simultaneity, you have decided to use costs as an
# instrument variable. Run a regression of prices pt on costs ct,
# and use predicted price hat(p_t) to find the structural-causal relation between sales and
# prices,
# Consider the parameter estimates for both the instrumental regression and the
# simple regression in (1). What is the causal impact of an increase in price pt on
# expected sales st?
# 3.1 2SLS with package
# independent ~ endogenous variable | instrumental variable
ivreg<-ivreg(sales~price|cost)
summary(ivreg)
# check the 2SLS because of negative R2
# 2SLS
Model2<-lm(price~cost)
summary(Model2)
p_hat<-fitted(Model2)
Model3<-lm(sales~p_hat)
summary(Model3)
# 4. Compare the R2 obtained for the regression in (1) with that obtained
# in (2). Which model is best?
# Answered above.