-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_pred.R
100 lines (59 loc) · 2.54 KB
/
day_pred.R
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
#' Function to run predictions on daily level
#' @param df data frame must contain date, landingContentGroup2, country, deviceCategory, operatingSystem, ses, rev, rps, dpred, avg
#' @param current_date date representing "current date" can be used to backtest by setting date in the past
#' @return data frame containg new dpred predictions
predictor_day <- function(df, current_date) {
df_test <- df%>%
filter(date<=current_date)%>%
filter(date>current_date-3)
df_check <- df%>%
filter(date == current_date+1, !is.na(dpred))%>%
select(landingContentGroup2, country, deviceCategory, operatingSystem, dpred)
df_amt<-df_test%>%
filter(ses >= 100)%>%
group_by(landingContentGroup2, country, deviceCategory, operatingSystem)%>%
summarise(count = n(), ses = sum(ses, na.rm = TRUE))
df_amt <- merge(df_amt, df_check,
by = c("landingContentGroup2", "country", "deviceCategory", "operatingSystem"), all = TRUE)%>%
arrange(desc(ses))%>%
mutate(date = current_date+1)
for(x in 1:nrow(df_amt)){
name1<-df_amt[x,]
if(!is.na(name1$dpred)){
next
}
df_1<-df%>%
filter(landingContentGroup2 == name1$landingContentGroup2 , country == name1$country ,
deviceCategory == name1$deviceCategory, operatingSystem == name1$operatingSystem)%>%
filter(date > current_date-12, date <= current_date)%>%
pad(start_val = current_date-11, end_val = current_date, interval = "day" )%>%
arrange(desc(date))
#begin pred
weights <- rep(.6,12)^seq(1:12)
df_exp <- data.frame(df_1, weights)%>%
mutate(rps_adj = rps*weights)%>%
filter(!is.na(rps_adj))
dval <- sum(df_exp$rps_adj)/sum(df_exp$weights)
# dval <- mean(df_1$rps, na.rm = TRUE)
#
# df_3d <- df_1%>%
# filter(date > current_date-3)%>%
# filter(date <= current_date)
#
# d_avg <- mean(df_3d$rps, na.rm = TRUE)
#
# dval <- (d_avg-dval) * .3 + dval
print(dval)
if(dval < 0){
dval <- 0
}
df_amt<-df_amt%>%
mutate(dpred = case_when(landingContentGroup2 == name1$landingContentGroup2 & country == name1$country &
deviceCategory == name1$deviceCategory & operatingSystem == name1$operatingSystem~dval,
TRUE ~ dpred))
#end pred
}
df_amt<-df_amt%>%
select(date, landingContentGroup2, deviceCategory, operatingSystem, country, dpred)
return(df_amt)
}