0
$\begingroup$

Apologize in advance for there being not a lot of wisdom here..

I am using stats models in Python to create an OLS type model. The data is a years worth of hourly electrical demand (response/dependent variable) of a building, and I am experimenting with creating a model with different explanatory variables that represent time-of-week, and outdoor temperature. Basically the code below represent a dataframe that I constructed (not shown) that if the date time index represents the particular month/day/hour it will either be a 1 or 0. I have also excluded some independent variables where the P value is higher than 0.05, like Saturday and hours 0-5, & 22.

kW_model6 = ols('Demand ~ OAT + OA_Enth + january + february + march + april + may + june + july + august + september + october + november + monday + tuesday + wednesday + thursday + friday + hour_6 + hour_7 + hour_8 + hour_9 + hour_10 + hour_11 + hour_12 + hour_13 + hour_14 + hour_15 + hour_16 + hour_17 + hour_18 + hour_19 + hour_20 + hour_21', data=df).fit() print(kW_model6.summary())

Output:

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 Demand   R-squared:                       0.593
Model:                            OLS   Adj. R-squared:                  0.592
Method:                 Least Squares   F-statistic:                     528.3
Date:                Thu, 12 Mar 2020   Prob (F-statistic):               0.00
Time:                        09:25:10   Log-Likelihood:                -37908.
No. Observations:                8735   AIC:                         7.587e+04
Df Residuals:                    8710   BIC:                         7.604e+04
Df Model:                          24                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept    -34.0281      1.230    -27.665      0.000     -36.439     -31.617
OAT           -0.4942      0.023    -21.185      0.000      -0.540      -0.448
OA_Enth        4.3904      0.134     32.677      0.000       4.127       4.654
january        0.3929      0.068      5.763      0.000       0.259       0.527
february       0.3929      0.068      5.763      0.000       0.259       0.527
march          0.3929      0.068      5.763      0.000       0.259       0.527
april          0.3929      0.068      5.763      0.000       0.259       0.527
may            0.3929      0.068      5.763      0.000       0.259       0.527
june           0.3929      0.068      5.763      0.000       0.259       0.527
july           0.3929      0.068      5.763      0.000       0.259       0.527
august         0.3929      0.068      5.763      0.000       0.259       0.527
september      0.3929      0.068      5.763      0.000       0.259       0.527
october        0.3929      0.068      5.763      0.000       0.259       0.527
november       0.3929      0.068      5.763      0.000       0.259       0.527
monday        24.5560      0.645     38.099      0.000      23.293      25.819
tuesday       26.5541      0.644     41.208      0.000      25.291      27.817
wednesday     25.3667      0.645     39.345      0.000      24.103      26.630
thursday      24.4529      0.644     37.948      0.000      23.190      25.716
friday        19.9213      0.645     30.904      0.000      18.658      21.185
hour_6        13.4158      1.033     12.984      0.000      11.390      15.441
hour_7        28.1592      1.033     27.254      0.000      26.134      30.184
hour_8        38.5271      1.034     37.270      0.000      36.501      40.553
hour_9        37.4324      1.035     36.173      0.000      35.404      39.461
hour_10       40.8495      1.036     39.415      0.000      38.818      42.881
hour_11       44.0491      1.038     42.424      0.000      42.014      46.084
hour_12       48.1224      1.040     46.261      0.000      46.083      50.162
hour_13       37.6936      1.042     36.185      0.000      35.652      39.736
hour_14       31.2085      1.042     29.938      0.000      29.165      33.252
hour_15       26.5551      1.042     25.479      0.000      24.512      28.598
hour_16       11.0039      1.041     10.572      0.000       8.964      13.044
hour_17        8.4223      1.039      8.103      0.000       6.385      10.460
hour_18        7.3961      1.038      7.127      0.000       5.362       9.430
hour_19        6.3822      1.036      6.161      0.000       4.351       8.413
hour_20        4.1404      1.035      4.002      0.000       2.112       6.169
hour_21        3.3824      1.034      3.272      0.001       1.356       5.409
==============================================================================
Omnibus:                      518.296   Durbin-Watson:                   0.224
Prob(Omnibus):                  0.000   Jarque-Bera (JB):             1344.664
Skew:                           0.338   Prob(JB):                    1.02e-292
Kurtosis:                       4.800   Cond. No.                     2.26e+18
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The smallest eigenvalue is 5.27e-30. This might indicate that there are
strong multicollinearity problems or that the design matrix is singular.

The questionS I have is what does the [2] warning represent of strong multicollinearity problems or that the design matrix is singular?

Also is it possible to know the overall standard error of the model? The output shows standard error for each independent variable, but I would like to know overall model standard error if possible... This stackoverflow post someone asked this same question but the answer print results.bse will only output the standard error for each independent variable. Any tips greatly appreciated

EDIT UPDATED MODELS

Model 1, more simple:

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 Demand   R-squared:                       0.494
Model:                            OLS   Adj. R-squared:                  0.494
Method:                 Least Squares   F-statistic:                     852.6
Date:                Fri, 20 Mar 2020   Prob (F-statistic):               0.00
Time:                        10:09:38   Log-Likelihood:                -38854.
No. Observations:                8735   AIC:                         7.773e+04
Df Residuals:                    8724   BIC:                         7.781e+04
Df Model:                          10                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     25.6167      0.835     30.680      0.000      23.980      27.253
OAT            0.2426      0.018     13.383      0.000       0.207       0.278
january        3.8752      0.880      4.402      0.000       2.150       5.601
february       7.1255      0.917      7.772      0.000       5.328       8.923
april         -9.2747      0.849    -10.929      0.000     -10.938      -7.611
may           -7.8215      0.873     -8.956      0.000      -9.533      -6.110
june          -7.4618      0.966     -7.728      0.000      -9.355      -5.569
july          12.8750      1.042     12.355      0.000      10.832      14.918
august        -4.5391      0.970     -4.679      0.000      -6.441      -2.638
weekend      -24.2935      0.491    -49.501      0.000     -25.255     -23.331
daytime       29.5826      0.462     64.010      0.000      28.677      30.488
==============================================================================
Omnibus:                     1088.881   Durbin-Watson:                   0.351
Prob(Omnibus):                  0.000   Jarque-Bera (JB):             1940.075
Skew:                           0.828   Prob(JB):                         0.00
Kurtosis:                       4.608   Cond. No.                         357.
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

Model 2, more variables:

OLS Regression Results                            
==============================================================================
Dep. Variable:                 Demand   R-squared:                       0.583
Model:                            OLS   Adj. R-squared:                  0.582
Method:                 Least Squares   F-statistic:                     451.2
Date:                Fri, 20 Mar 2020   Prob (F-statistic):               0.00
Time:                        10:09:40   Log-Likelihood:                -38009.
No. Observations:                8735   AIC:                         7.607e+04
Df Residuals:                    8707   BIC:                         7.627e+04
Df Model:                          27                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept      4.3297      0.705      6.142      0.000       2.948       5.712
OAT            0.0873      0.014      6.037      0.000       0.059       0.116
april         -8.5919      0.760    -11.311      0.000     -10.081      -7.103
may           -5.4854      0.786     -6.982      0.000      -7.025      -3.945
june          -3.4248      0.865     -3.959      0.000      -5.120      -1.729
july          18.1550      0.926     19.604      0.000      16.340      19.970
september      5.7152      0.849      6.732      0.000       4.051       7.379
monday        23.8989      0.652     36.630      0.000      22.620      25.178
tuesday       26.5753      0.652     40.735      0.000      25.296      27.854
wednesday     25.6968      0.653     39.378      0.000      24.418      26.976
thursday      24.6435      0.652     37.782      0.000      23.365      25.922
friday        20.4213      0.652     31.307      0.000      19.143      21.700
hour_6        13.6696      1.046     13.074      0.000      11.620      15.719
hour_7        28.5675      1.045     27.327      0.000      26.518      30.617
hour_8        38.9586      1.047     37.227      0.000      36.907      41.010
hour_9        38.2897      1.048     36.520      0.000      36.235      40.345
hour_10       42.1590      1.051     40.104      0.000      40.098      44.220
hour_11       45.7804      1.055     43.411      0.000      43.713      47.848
hour_12       50.2482      1.058     47.501      0.000      48.175      52.322
hour_13       40.1126      1.060     37.835      0.000      38.034      42.191
hour_14       33.8396      1.061     31.887      0.000      31.759      35.920
hour_15       29.2043      1.061     27.532      0.000      27.125      31.284
hour_16       13.8810      1.057     13.129      0.000      11.809      15.953
hour_17       11.2406      1.054     10.662      0.000       9.174      13.307
hour_18        9.8193      1.052      9.336      0.000       7.758      11.881
hour_19        8.2318      1.050      7.843      0.000       6.175      10.289
hour_20        5.3948      1.048      5.149      0.000       3.341       7.449
hour_21        4.1299      1.047      3.946      0.000       2.078       6.181
==============================================================================
Omnibus:                      639.976   Durbin-Watson:                   0.221
Prob(Omnibus):                  0.000   Jarque-Bera (JB):             1467.001
Skew:                           0.462   Prob(JB):                         0.00
Kurtosis:                       4.782   Cond. No.                         473.
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
In [11]:

INCLUDE RESIDUAL PLOTS:

plot residuals

resiDf = pd.DataFrame()

predictedValues  = kW_model3.fittedvalues
yData = df.Demand

res = yData - predictedValues

res.plot(marker='.', alpha=1, linestyle='None', figsize=(16, 12), subplots=True)

enter image description here

$\endgroup$
6
  • $\begingroup$ In general, check what attributes are available and look for likely named candidates, e.g. dir(kW_model6). mean squared error results.mse or results.scale contain the variance of the residual. $\endgroup$
    – Josef
    Commented Mar 24, 2020 at 15:36
  • $\begingroup$ On the second more complicated model kW_model3.mse = 159547, and kW_model3.scale = 353. For Variance, doesnt it indicate how far the numbers are spread out from their average value...? I still get confused on how I could use this in looking at my model results... $\endgroup$
    – bbartling
    Commented Mar 24, 2020 at 18:06
  • $\begingroup$ Thanks for the tips here... $\endgroup$
    – bbartling
    Commented Mar 24, 2020 at 18:06
  • $\begingroup$ @Josef, I included a plot of the residuals for the model. Is this where you could view the mse and variance (scale) in the data? $\endgroup$
    – bbartling
    Commented Mar 25, 2020 at 17:13
  • $\begingroup$ I can definitely make out a pattern in the residuals which looks similar to plotting the entire dataset which is interesting too. Not a good model, need more data.. $\endgroup$
    – bbartling
    Commented Mar 25, 2020 at 17:14

1 Answer 1

1
$\begingroup$

The very small eigenvalue and the large condition number mean that your matrix of explanatory variables is singular.
Also, you have more explanatory variables than df_model, so the matrix rank is smaller than the number of columns.

The default algorithm in statsmodels for linear models uses SVD and a generalized inverse, pinv. This implies that not all of your parameters are identified, and the solution is regularized similar to principal component regression where the eigenvectors with singular values close to zero are dropped. Close to zero depends on the definition in numpy.linalg.pinv and is defined through a default threshold for the condition number.

Even though the parameters estimates are not unique (and are "arbitrarily" made unique through pinv), predictions are still well defined.

However, it is better to check what causes the perfect collinearity, e.g. dummy variable trap, and drop variables to make the design matrix nonsingular.

$\endgroup$
2
  • $\begingroup$ Hi @ Josef, I realized I had an error in the original model. I updated the post with 2 more under EDITS, can you comment on these too? Particularly the dummy variable trap. I am attempting to recreate some steps from a White Paper to use OLS models for electrical demand response for a building. So basically the only non-dummy variables would be the building electrical demand (kW) and outside air temperature... $\endgroup$
    – bbartling
    Commented Mar 24, 2020 at 14:16
  • $\begingroup$ Any time you have in response greatly appreciated, there isnt a ton of wisdom here! But more of a learning experience $\endgroup$
    – bbartling
    Commented Mar 24, 2020 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.