/
regression.py
executable file
·50 lines (31 loc) · 1.26 KB
/
regression.py
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
#!/usr/bin/env python
import sys
from ols import ols
from utils import get_event_data, event_data_to_matrix
def main(events):
data = get_event_data(events)
if len(data) == 0:
sys.exit("ERROR: Unable to retrieve data from Mixpanel")
matrix = event_data_to_matrix(data, events)
response_name = events.pop(0)
response_data = matrix[:,0]
predictors_data = matrix[:,1:]
predictors_names = events
model = ols(response_data, predictors_data, response_name, predictors_names)
model.summary()
## Generate Equation
# Gather list of coefficients so we can build our formula
coeff_dict = dict(zip(model.x_varnm, model.b))
equation = "%s = %.5f" % (response_name, coeff_dict['const']) # Start with constant coefficient
for name in predictors_names:
val = coeff_dict[name]
equation += " + %.5f(%s)" % (val, name)
print "Regression equation for response variable '%s'" % response_name
print
print equation
if __name__ == "__main__":
if len(sys.argv) < 3:
sys.exit("ERROR: Please enter at least 1 predictor and 1 response event " \
+ "to analyse. Like this: './regression.py predictor_event " \
+ "response_event1 response_event2'")
main(sys.argv[1::])