-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.r
161 lines (126 loc) · 7.33 KB
/
model.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
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
# require(gRain);
library(bnlearn)
library(Rgraphviz)
# Change this to yours!
root_path = 'D:\\Documents\\Github\\BN-2017'
# root_path = '~/Documents/RU/BN/BN-2017'
data_path = file.path(root_path, 'data')
source(file.path(root_path, 'testable_implications_v3d.r'))
source(file.path(root_path, 'helper.r'))
# defining the network arcs from the picture
# v3d
defined_net_string = "[major][genre|major][budget|major:genre:us][us|major][cast_popularity|budget:us][community_count|us:genre:budget:cast_popularity][community_vote|community_count:critics_vote][critics_count|us:genre:budget][critics_vote|critics_count:budget][roi|cast_popularity:community_vote:critics_vote]"
defined_net = model2network(defined_net_string)
#graphviz.plot(defined_net)
t <- read.csv(file.path(data_path, 'train.csv'))
t <- t[c('major',
'genre',
'budget',
'us',
'cast_popularity',
'community_vote',
'community_count',
'critics_vote',
'critics_count',
'roi')]
# 'revenue_binned',
# 'popularity_binned')]
names(t) = c('major','genre','budget','us','cast_popularity','community_vote', 'community_count', 'critics_vote', 'critics_count', 'roi')
fitted <- bn.fit(defined_net, data=t)
implications <- getImplications()
count <- 1
citest.names <- c()
citest.rmsea <- c()
N <- dim(t)[1]
for (i in implications)
{
print(i)
test <- ci.test(x = i[1] , y = i[2], z = c(i[-1:-2]), data = t, test="x2")
# note that some numbers will be NaN, this is, according to some webstie overfit of the data
# so we can safely ignore it
citest.rmsea[count] <- max(sqrt( ((test$statistic/test$parameter) - 1 )/(N - 1)),0)
citest.names[count] = test$data.name
count <- count+1
}
# df <- data.frame(names=citest.names, p.value=citest.p.value)
df <- data.frame(names=citest.names, RMSEA=citest.rmsea)
# todo plot this with points and maybe prettier labels
op <- par(mar=c(11,4,4,2)) # the 10 allows the names.arg below the barplot
plot(df, las=2, xlab='')
rm(op)
# run the following code: to calculate the rmseas with higher dan 0.05
df = df[df$RMSEA > 0.05 & !is.na(df$RMSEA),]
# - predict a movie's profitability
# - see https://sujitpal.blogspot.nl/2013/07/bayesian-network-inference-with-r-and.html
# - see http://www.bnlearn.com/documentation/man/cpquery.html for inference given evidence (or not)
# - see http://www.bnlearn.com/documentation/man/rbn.html for generating data from the networ
#What makes for a highly profitable movie?
# Pr(roi=high | genre=action) vs. Pr(roi=high | genre=light)
cpquery(fitted, (roi=='high'), (genre=='action'))
cpquery(fitted, (roi=='high'), (genre=='dark'))
cpquery(fitted, (roi=='high'), (genre=='light'))
cpquery(fitted, (roi=='high'), (genre=='other'))
#-----------------------------------------------------------------------------------------------------------
#What are the odds of making a high profit for a non-major company? What if we want to go for a niche movie?
#e.g. Pr(roi=high | major=no) vs. Pr(roi=~high | major=no)
cpquery(fitted, (roi=='high'), (major=='yes'))
cpquery(fitted, (roi!='high'), (major=='yes'))
cpquery(fitted, (roi=='high'), (major=='no'))
cpquery(fitted, (roi!='high'), (major=='no'))
#e.g. Pr(roi=high | major=no, genre=other) vs. Pr(roi=~high | major=no, genre=other)
cpquery(fitted, (roi=='high' & major=='no'), (genre=='action'))
cpquery(fitted, (roi=='high' & major=='no'), (genre=='dark'))
cpquery(fitted, (roi=='high' & major=='no'), (genre=='light'))
cpquery(fitted, (roi=='high' & major=='no'), (genre=='other'))
cpquery(fitted, (roi!='high'& major=='no'), (genre=='action'))
# the same for major, for comparison
cpquery(fitted, (roi=='high' & major=='yes'), (genre=='action'))
cpquery(fitted, (roi=='high' & major=='yes'), (genre=='dark'))
cpquery(fitted, (roi=='high' & major=='yes'), (genre=='light'))
cpquery(fitted, (roi=='high' & major=='yes'), (genre=='other'))
cpquery(fitted, (roi!='high'& major=='yes'), (genre=='action'))
#-----------------------------------------------------------------------------------------------------------
#Does a highly popular cast get us higher votes?
#e.g. Pr(critics_vote=great & community_vote=great | cast=high)
cpquery(fitted, (critics_vote=='great' & community_vote=='great'), (cast_popularity=='high'))
cpquery(fitted, (critics_vote=='great' | community_vote=='great'), (cast_popularity=='high'))
cpquery(fitted, (critics_vote=='great'), (cast_popularity=='high'))
cpquery(fitted, (community_vote=='great'), (cast_popularity=='high'))
cpquery(fitted, (critics_vote=='great'), (cast_popularity!='high'))
cpquery(fitted, (community_vote=='great'), (cast_popularity!='high'))
#e.g. Pr(critics_vote=great & community_vote=great | cast=~high)
cpquery(fitted, (critics_vote=='great' & community_vote=='great'), (cast_popularity!='high'))
cpquery(fitted, (critics_vote=='great' | community_vote=='great'), (cast_popularity!='high'))
cpquery(fitted, (critics_vote=='great' & community_vote=='great'), (cast_popularity=='high'))
cpquery(fitted, (critics_vote=='great' | community_vote=='great'), (cast_popularity=='high'))
#-----------------------------------------------------------------------------------------------------------
#How can we prevent our movie from being a flop?
#e.g. Pr(roi=~flop | genre=? & cast_popularity=?)
cpquery(fitted, (roi!='flop'), (genre=='action' & cast_popularity=='high'))
cpquery(fitted, (roi!='flop'), (genre=='dark' & cast_popularity=='high'))
cpquery(fitted, (roi!='flop'), (genre=='light' & cast_popularity=='high'))
cpquery(fitted, (roi!='flop'), (genre=='other' & cast_popularity=='high'))
cpquery(fitted, (roi!='flop'), (genre=='action' & cast_popularity!='high'))
cpquery(fitted, (roi!='flop'), (genre=='dark' & cast_popularity!='high'))
cpquery(fitted, (roi!='flop'), (genre=='light' & cast_popularity!='high'))
cpquery(fitted, (roi!='flop'), (genre=='other' & cast_popularity!='high'))
cpquery(fitted, (roi!='flop'), (genre=='action' ))
cpquery(fitted, (roi!='flop'), (genre=='dark' ))
cpquery(fitted, (roi!='flop'), (genre=='light' ))
cpquery(fitted, (roi!='flop'), (genre=='other' ))
#-----------------------------------------------------------------------------------------------------------
#Are highly popular actors worth it if we want a "great" review?
#e.g. Pr(critics_vote=great | cast=avg) vs. Pr(critics_vote=great | cast=high)
cpquery(fitted, (critics_vote=='great'), (cast_popularity=='high'))
cpquery(fitted, (critics_vote=='great'), (cast_popularity=='avg'))
cpquery(fitted, (critics_vote=='great'), (cast_popularity=='low'))
cpquery(fitted, (community_vote=='great'), (cast_popularity=='high'))
cpquery(fitted, (community_vote=='great'), (cast_popularity=='avg'))
cpquery(fitted, (community_vote=='great'), (cast_popularity=='low'))
cpquery(fitted, (community_vote=='great'& critics_vote=='great'), (cast_popularity=='high'))
cpquery(fitted, (community_vote=='great'& critics_vote=='great'), (cast_popularity=='avg'))
cpquery(fitted, (community_vote=='great'& critics_vote=='great'), (cast_popularity=='low'))
cpquery(fitted, (community_vote=='great' | critics_vote=='great'), (cast_popularity=='high'))
cpquery(fitted, (community_vote=='great' | critics_vote=='great'), (cast_popularity=='avg'))
cpquery(fitted, (community_vote=='great' | critics_vote=='great'), (cast_popularity=='low'))
#-----------------------------------------------------------------------------------------------------------