[Python] Why are my VIF scores so low, but in my OLS Regression Results, I get a warning: the...

Discussão em 'Python' iniciado por Stack, Abril 22, 2025.

  1. Stack

    Stack Membro Participativo

    mport pandas as pd import numpy as np import matplotlib.pyplot as plt import statsmodels.api as sm import seaborn as sns from statsmodels.stats.outliers_influence import variance_inflation_factor from sklearn.model_selection import train_test_split

    def remove_dot(data): dot_not_accepted = ['brthord', 'meduc', 'feduc'] for column in dot_not_accepted: data[column] = data[column].replace('.', np.nan) data = data.dropna() return data

    data_file_path = 'data.xls'

    Load your data
    data = pd.read_excel(data_file_path)

    Process the data using the remove_dot function
    data = remove_dot(data)

    Create the regression model
    data['Total_Education'] = data['feduc'] + data['meduc'] X = data[['hours', 'IQ', 'KWW', 'educ', 'exper', 'tenure', 'age', 'sibs','brthord', 'feduc', 'meduc']] y = data['wage'] X = sm.add_constant(X) # Adds a constant to the model

    Split the data into training and test sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

    Fit the model with training data
    model = sm.OLS(y_train, X_train).fit()

    Calculate VIF for each variable
    vif_data = pd.DataFrame() vif_data["Variables"] = X_train.columns vif_data["VIF"] = [variance_inflation_factor(X_train.values, i) for i in range(X_train.shape[1])] print(vif_data)

    Print the summary of the model
    print(model.summary())


    Variables VIF 0 const 249.241998 1 hours 1.043743 2 IQ 1.564278 3 KWW 1.732137 4 educ 2.061102 5 exper 1.942750 6 tenure 1.183890 7 age 1.800178 8 sibs 1.515675 9 brthord 1.465356 10 feduc 1.622698 11 meduc 1.620933 Notes: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified. [2] The condition number is large, 1.98e+03. This might indicate that there are strong multicollinearity or other numerical problems.
    ​

    Is it an error in my code? Or is this normal behaviour.

    I tried to find a fault in my code, because I find my case very suspicious. I have to find the best model for the data. I thought it was a nice idea, that I would combine or delete variables based on VIF score. But now is everything according to VIF not multicollinear.

    Continue reading...

Compartilhe esta Página