I'm hoping to better understand the predicted output of the dependent variable using a multiple linear regression. Specifically, I'm getting negative predicted outputs when altering specific independent variables.
I'll display an example df
below and explain the independent variables
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.cross_validation import train_test_split
from statsmodels.stats.outliers_influence import variance_inflation_factor
from statsmodels.tools.tools import add_constant
d = ({
'Date' : ['01/01/18','01/01/18','01/01/18','01/01/18','02/01/18','02/01/18','02/01/18','02/01/18'],
'Country' : ['US','US','US','MX','US','US','MX','MX'],
'Occurrences' : [1,5,3,4,2,5,10,2],
'Turnover' : [100000,40000,500000,8000,10000,300000,80000,1000],
'Medium' : ['S1','S2','S1','S2','S1','S1','S1','S2'],
'Day' : ['Saturday','Saturday','Saturday','Saturday','Sunday','Sunday','Sunday','Sunday'],
})
df = pd.DataFrame(data=d)
# One hot Encoding
one_hot = pd.get_dummies(df[['Country','Medium','Day']], drop_first=True)
df = df.join(one_hot)
df = df[['Country_US','Medium_S2','Day_Sunday','Occurrences','Turnover']]
Model:
# Extract features and labels
features = df.iloc[:,0:-1].values
labels = df.iloc[:,-1].values
# Cross validation
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size = 0.3, random_state = 0)
# Create Linear Model
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# CoEfficient and Intercept
#print(regressor.coef_)
#print(regressor.intercept_)
# Predict the Output
y_Pred = regressor.predict(X_test)
print(regressor.predict([0, 1, 0, 1]))
# Determine multicollinearity
X = add_constant(df)
VIF = pd.Series([variance_inflation_factor(X.values, i)
for i in range(X.shape[1])],
index=X.columns)
print(VIF)
const 19.914550
Country_US 2.029087
Medium_S2 2.165254
Day_Sunday 1.495758
Occurrences 1.364778
Turnover 1.706089
So the variables I've plugged in are:
Country_Group : 0
Medium_S2 : 1
Day_Sunday = 0
Occurrences : 1
I understand Linear regression
does not respect the bounds of 0. But is there a method I can ensure the output is a positive number without affecting the validity of the model?
The minimum output that can be generated is zero. It cannot be a negative number.
Output:
[-95500.]