Einfache Lineare Regression (Pythton)¶
These¶
Wir definieren zwei Variablen:
x ist die unabhängige Variable y1 ist die abhängige.
Wir vermuten , dass y1 durch x “erklärt” wird:
Benötigte Pakete (falls noch nicht installiert).¶
# !pip install numpy
# !pip install matplotlib
# !pip install statsmodels
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
Erstes Beispiel¶
Eine unabhängige Variable.
x=[1,2,3,4,5,6,7,8,9,10]
Erste abhängige Variable (fehlerbehaftet)!
y1=[1,1,2,5,6,6,7,10,9,11]
Ein Ausgabe als Plot.
plt.scatter(x,y1,color="red")
plt.title("Messreihe I") # stating the title of the graph
plt.xlabel("x-Werte") # adding the name of x-axis
plt.ylabel("y-Werte") # adding the name of y-axis
plt.show() # specifies end of graph

# Trendlinie haben wir noch nicht. Nur die Sachverhalte der Daten wird dargestellt.
Zweites Beispiel¶
y2=[1,5,2,5,8,3,7,12,4,10]
Ein Ausgabe als Plot.
plt.scatter(x,y2,color="blue")
plt.xlabel("x")
plt.ylabel("y2")
Text(0, 0.5, 'y2')

Sieht nicht so gut aus!
#Vorbereitender Schritt: Die Trendlinie darf einen-Intercept haben, muss es aber nicht.
x = sm.add_constant(x)
model_1 = sm.OLS(y1,x).fit()
print(model_1.summary())
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.950
Model: OLS Adj. R-squared: 0.944
Method: Least Squares F-statistic: 151.7
Date: Fri, 14 Jan 2022 Prob (F-statistic): 1.76e-06
Time: 09:41:30 Log-Likelihood: -11.544
No. Observations: 10 AIC: 27.09
Df Residuals: 8 BIC: 27.69
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const -0.6000 0.586 -1.024 0.336 -1.952 0.752
x1 1.1636 0.094 12.317 0.000 0.946 1.381
==============================================================================
Omnibus: 2.292 Durbin-Watson: 2.532
Prob(Omnibus): 0.318 Jarque-Bera (JB): 1.010
Skew: 0.350 Prob(JB): 0.604
Kurtosis: 1.610 Cond. No. 13.7
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
Zuerst schaue ich nach der F-statistic. Je größer desto besser. Hier: 151.7 ist ok
Dann darunter nach Prob(F-statistic) = 1.76e-06
Dieser Wert ist viel kleiner als der Testwert 0.05. D.h. mit 5% Irrtumswahrscheinlichkeit ist die Nullhypothese (es gibt k e i n e n Zusammenhang von x und y) zu verwerfen.
Das Modell darf also angewendet werden.
Wie gut werden die Varianzen der y - Werte erklärt.
Das erhalten wir aus “R-squared” = 0.975.
Ein Superwert. Man kann mit diesem Modell weiterrechnen!!
…und wie ist das Model 1 zu beschreiben:
y1(model) = const + coef(x1) , also: y1(model) = -0.6 + 1.1636*x
Nun zur Bewertung von y2:
model_2 = sm.OLS(y2,x).fit()
print(model_2.summary())
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.422
Model: OLS Adj. R-squared: 0.350
Method: Least Squares F-statistic: 5.850
Date: Fri, 14 Jan 2022 Prob (F-statistic): 0.0419
Time: 09:13:40 Log-Likelihood: -23.529
No. Observations: 10 AIC: 51.06
Df Residuals: 8 BIC: 51.66
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 1.5333 1.943 0.789 0.453 -2.948 6.015
x1 0.7576 0.313 2.419 0.042 0.035 1.480
==============================================================================
Omnibus: 0.070 Durbin-Watson: 3.097
Prob(Omnibus): 0.966 Jarque-Bera (JB): 0.297
Skew: -0.040 Prob(JB): 0.862
Kurtosis: 2.159 Cond. No. 13.7
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
Also dieselbe Reihenfolge:
F-statistic: 5.85 also Faktor 30 kleiner als vorher.
Heisst: wir werden nicht Gutes zu erwarten haben.
Prob : 0.0419 : Nahe an 0.05.
Heisst, Auf dem strengen Level 5% ist wahrscheinlich die Nullhypothese n i c h t zu verwerfen.
Heisst auch: wahrscheinlich k e i n Zusammenhang
R-squared: 0.422 Kläglich!
will man trotz all’ dieser Warnungen unbedingt und gegen alle Statistikerherzen ein Modell2 bauen, dann sähe es so aus:
y2(model) = 1.5333 + 0.7576 * x