-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantileregresion5.py
85 lines (57 loc) · 2.04 KB
/
quantileregresion5.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
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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 8 10:37:29 2021
@author: deses
"""
import numpy as np
import statsmodels.formula.api as smf
from sklearn.linear_model import QuantileRegressor
from nonlinearreg import func1
def QLRegression(data):
x=np.log(data[:,0])
y=data[:,1]
#print(x,y)
mod = smf.quantreg(y,x)
res = mod.fit(q=0.5)
print(res.summary())
def QLRegression2(data,quantile, a,b ):
x=func1(data[:,0],a,b)
y=data[:,1]
x = x[:, np.newaxis]
qreg = QuantileRegressor(quantile=quantile,alpha=0,solver='highs').fit(x, y)
slope= qreg.coef_
intercept = qreg.intercept_
backlog=np.linspace(1, max(data[:,0]), 10000)
backlog=backlog[:, np.newaxis]
backlogy=qreg.predict(func1(backlog,a,b))
y_pred = qreg.predict(x)
return slope, intercept
def QLRegression_subplot(data,quantile, a,b):
x=func1(data[:,0],a,b)
y=data[:,1]
x = x[:, np.newaxis]
qreg = QuantileRegressor(quantile=quantile,alpha=0).fit(x, y)
slope= qreg.coef_
intercept = qreg.intercept_
max_days=int(max(data[:,0]))
backlog=np.linspace(1,max_days , max_days)
backlog=backlog[:, np.newaxis]
backlogy=qreg.predict(func1(backlog,a,b)).reshape(-1,1)
y_pred = qreg.predict(x)#
return slope, intercept,x,y, y_pred,backlog, backlogy
def QLRegression_df(data, quantile, a,b,quant_df,rodent='Rat'):
x=func1(data[:,0],a,b)
x2=func1(quant_df['Human'],a,b)
y=data[:,1]
x = x[:, np.newaxis]
x2 = x2[:, np.newaxis]
lower_quant2= np.full((len(quant_df[rodent])), 0.0)
for i in quantile:
qreg = QuantileRegressor(quantile=i,alpha=0.5).fit(x, y)
y_pred = qreg.predict(x2)
print(y.shape)
lower_quant = quant_df[rodent] > y_pred
lower_quant2[lower_quant] = i
#print(lower_quant2)
quant_df['quantil'] = lower_quant2
quant_df.to_excel('file2.xlsx')