Is there a python equivalent of R stats' alias function?

Viewed 158

Question:

Hi all,

I'm working through some multiple linear regression in Python, and have to demonstrate multicollinearity. I'm more used to working in R, and wondered if there was a function/way of replicating R stats' alias function. What I'm after is an output showing how the confounding variables are related.

R

R's stats::alias() gives an output where

rail_trail_head <- mosaicData::RailTrail %>% head(10)

alias(volume ~ ., rail_trail_head)

Out:

Model :
volume ~ hightemp + lowtemp + avgtemp + spring + summer + fall + 
    cloudcover + precip + weekday + dayType

Complete :
         (Intercept) hightemp lowtemp spring cloudcover precip weekday1
avgtemp    0         1/2      1/2       0      0          0      0     
summer     1           0        0      -1      0          0      0     
fall       0           0        0       0      0          0      0     
dayType1   0           0        0       0      0          0     -1  

For example, this shows clearly that avgtemp can be calculated from hightemp and lowtemp.

Python

The closest I've found to this in python is from statsmodels:

import pandas as pd
from statsmodels.stats.outliers_influence import variance_inflation_factor

# data
rail_trail_head = pd.DataFrame({
    'hightemp' : [83, 73, 74, 95, 44, 69, 66, 66, 80, 79],
    'lowtemp' : [50, 49, 52, 61, 52, 54, 39, 38, 55, 45],
    'avgtemp' : [66.5, 61, 63, 78, 48, 61.5, 52.5, 52, 67.5, 62],
    'spring' : [0, 0, 1, 0, 1, 1, 1, 1, 0, 0],
    'summer' : [1, 1, 0, 1, 0, 0, 0, 0, 1, 1],
    'fall' : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    'cloudcover' : [7.59999990463257,
                          6.30000019073486,7.5,2.59999990463257,10,6.59999990463257,
                          2.40000009536743,0,3.79999995231628,
                          4.09999990463257],
    'precip' : [0,0.28999999165535,
                          0.319999992847443,0,0.140000000596046,
                          0.0199999995529652,0,0,0,0],
    'volume' : [501, 419, 397, 385, 200, 375, 417, 629, 533, 547],
    'weekday' : [True,True,True,False,
                          True,True,True,False,False,True],
    'dayType' : ["weekday","weekday",
                          "weekday","weekend","weekday","weekday","weekday",
                          "weekend","weekend","weekday"]
})

X = pd.get_dummies(
    rail_trail_head
    .drop(columns='volume')
    .assign(weekday=rail_trail_head.weekday.astype('int'))
)

# VIF dataframe
vif_data = pd.DataFrame()
vif_data["feature"] = X.columns
  
# calculating VIF for each feature
vif_data["VIF"] = [variance_inflation_factor(X.values, i)
                          for i in range(len(X.columns))]
  
print(vif_data)

Out

            feature       VIF
0          hightemp       inf
1           lowtemp       inf
2           avgtemp       inf
3            spring       inf
4            summer       inf
5              fall       NaN
6        cloudcover  9.999401
7            precip  1.446440
8           weekday       inf
9   dayType_weekday       inf
10  dayType_weekend       inf

And while this is informative, it doesn't tell me how the variables are correlated, or the relationships between them.

Is there a python equivalent of the alias() function from R stats.

Mosaic Data for R:

# if you don't want to install.packages('mosaicData'):
rail_trail_head <- data.frame(
  stringsAsFactors = FALSE,
          hightemp = c(83, 73, 74, 95, 44, 69, 66, 66, 80, 79),
           lowtemp = c(50, 49, 52, 61, 52, 54, 39, 38, 55, 45),
           avgtemp = c(66.5, 61, 63, 78, 48, 61.5, 52.5, 52, 67.5, 62),
            spring = c(0, 0, 1, 0, 1, 1, 1, 1, 0, 0),
            summer = c(1, 1, 0, 1, 0, 0, 0, 0, 1, 1),
              fall = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
           cloudcover = c(7.59999990463257,
                          6.30000019073486,7.5,2.59999990463257,10,6.59999990463257,
                          2.40000009536743,0,3.79999995231628,
                          4.09999990463257),
               precip = c(0,0.28999999165535,
                          0.319999992847443,0,0.140000000596046,
                          0.0199999995529652,0,0,0,0),
            volume = c(501, 419, 397, 385, 200, 375, 417, 629, 533, 547),
              weekday = c(TRUE,TRUE,TRUE,FALSE,
                          TRUE,TRUE,TRUE,FALSE,FALSE,TRUE),
              dayType = c("weekday","weekday",
                          "weekday","weekend","weekday","weekday","weekday",
                          "weekend","weekend","weekday")
   )

Edit

I realise I don't need the complete formula for how the correlated variables are related, just which are correlated with each other. So an extra column with a 'group' would do. This would indicate - the variables in this group are collinear.

In the railtrail example all I'd need is an output similar to:

group feature VIF
0 hightemp inf
0 lowtemp inf
0 avgtemp inf
1 spring inf
1 summer inf
1 fall NaN
2 cloudcover 9.999401
3 precip 1.446440
4 weekday inf
4 dayType_weekday inf
4 dayType_weekend inf
0 Answers
Related