2017-03-10 2 views
0

Wissen Sie, wie ich den ersten Teil in den Rest der Funktion passen kann, um eine Regressionsanalyse zu erhalten.Python-Funktion für multivariate Regression mit Daten aus csv

import numpy as np 
import matplotlib.pyplot as plt 
from numpy import * 
from matplotlib.pyplot import * #for the graph 

data = np.genfromtxt('bookingdata.csv', delimiter = ',') 

tvst = data[1:,][:,1] 
cntime = data[1:,][:,2] 
brate = data[1:,][:,3] 
ppvwst = data[1:,][:,4] 



import statsmodels.api as sm 


def reg_m(tvst, cntime,brate,ppvwst): 
ones = np.ones(len(cntime[0]) 
X = sm.add_constant(np.column_stack((x[0], ones))) 
for ele in x[1:]: 
X = sm.add_constant(np.column_stack((ele, X))) 
results = sm.OLS(y, X).fit() 
return results 

und alle Variablen wurden als Variablen tvst, ctime usw. geladen. Auch alle Variablen sind Zahlen.

das Endziel ist nun eine multivariate Regressions wie

      OLS Regression Results        
============================================================================== 
Dep. Variable:      y R-squared:      0.535 
Model:       OLS Adj. R-squared:     0.461 
Method:     Least Squares F-statistic:      7.281 
Date:    Tue, 19 Feb 2013 Prob (F-statistic):   0.00191 
Time:      21:51:28 Log-Likelihood:    -26.025 
No. Observations:     23 AIC:        60.05 
Df Residuals:      19 BIC:        64.59 
Df Model:       3    
============================================================================== 
       coef std err   t  P>|t|  [95.0% Conf. Int.] 
------------------------------------------------------------------------------ 
x1    0.2424  0.139  1.739  0.098  -0.049  0.534 
x2    0.2360  0.149  1.587  0.129  -0.075  0.547 
x3   -0.0618  0.145  -0.427  0.674  -0.365  0.241 
const   1.5704  0.633  2.481  0.023   0.245  2.895 


Omnibus:      6.904 Durbin-Watson:     1.905 
Prob(Omnibus):     0.032 Jarque-Bera (JB):    4.708 
Skew:       -0.849 Prob(JB):      0.0950 
Kurtosis:      4.426 Cond. No.       38.6 
+0

Was scheint nicht zu arbeiten? – DyZ

+0

Ich habe meine CSV-Datei in Python geladen, als Variablen tvst (abhängig) und Centime, Braten, ppvwst (unabhängig). Nun möchte ich einen multivariaten Regressionstest an dem Modell durchführen, mit einer solchen Ausgabe wie der oben gezeigten. Ich bin völlig verloren, wie ich das erreichen kann. –

+0

'add_constant' macht dasselbe wie die Spalte von Einsen. Beide sind redundant. – user333700

Antwort

0

Erster zu bekommen, wenn y (ENDOG) ist nur eine Variable, dann wird diese multiple Regression genannt. Multivariate Regression bezieht sich normalerweise auf den Fall, wenn wir mehrere y gleichzeitig haben, d. H. Y ist multivariat.

add_constant macht das gleiche wie das Hinzufügen der Spalte ones. Beide sind redundant.

Also, die multiple Regression ist nur

X = sm.add_constant(np.column_stack((cntime, brate, ppvwst))) 
results = sm.OLS(y, X).fit() 

oder da wir bereits eine column_stack tun, die eine zusätzliche Kopie der Daten vermeidet:

X = np.column_stack((ones, cntime, brate, ppvwst))) 
results = sm.OLS(y, X).fit() 
Verwandte Themen