2
$\begingroup$

I trying to run a mutivariate multiple regression using Statsmodels. Not sure if this is the best tool for this type of regression, so please do tell me if there's another package to which I should belooking. Below is a toy replication of the process (run in Colab).

import statsmodels.api as sm
import pandas as pd

sampleY = {'Maths': [90, 80, 70, 60, 50, 40, 30, 20], 'English': [77, 66, 55, 44, 33, 66, 44, 11]}
sampleX = {'Work': [76, 75, 72, 66, 66, 63, 62, 61] ,'Brains': [33, 66, 22, 44, 11, 66, 55, 11]}

Y = pd.DataFrame(sampleY,columns= ['Maths', 'English'])
X = pd.DataFrame(sampleX,columns= ['Work', 'Brains'])

Xc = sm.add_constant(X)

linear_regression = sm._MultivariateOLS(Y,Xc)
#linear_regression = sm.OLS(Y,Xc) - this one works, but of course only with a single DV, i.e. Y = pd.DataFrame(sampleY,columns= ['Maths'])
fitted_model = linear_regression.fit()

fitted_model.summary()

However which way I try to ensure that statsmodels is fully loaded - git clone, importing the one module specifically, etc. I get

AttributeError: module 'statsmodels.api' has no attribute '_MultivariateOLS'

If I run an OLS (i.e. multiple regression, not multivariate), instead, all works fine. Stumped.

$\endgroup$
  • $\begingroup$ You are not doing something wrong, just what you look for is not there. Please see my answer for a few more details. $\endgroup$ – usεr11852 Aug 16 '19 at 9:25
2
$\begingroup$

At the time of writing this (Aug-2019) there is no MultivariateOLS in actual terms. That's why the _ infront of the call; it signifies that it is mostly a placeholder and should not be directly called by a user. Right now, only MultivariateTestResults is operational as it acts as the back-end for MANOVA.

I am unaware of commonly used multivariate linear regression implementation in Python. That said, these lecture notes on Multivariate Linear Regression by N. Helwig show all the required Lin. Algebra to do it and offer a good walk-through in R; I think they will be very helpful to get one started.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ I add this link for everybody interested in calling R from Python after installing R on the same pc, just in case you wish to use some R libraries within a code written in Python. kdnuggets.com/2015/12/using-python-r-together.html $\endgroup$ – Fr1 Aug 16 '19 at 10:59
1
$\begingroup$

Multivariate OLS is closely related to canonical correlation analysis, which Statsmodels has:

https://www.statsmodels.org/devel/generated/statsmodels.multivariate.cancorr.CanCorr.html

Also, if your multivariate data are actually balanced repeated measures of the same thing, it might be better to use a form of repeated measure regression, like GEE, mixed linear models , or QIF, all of which Statsmodels has.

| cite | improve this answer | |
$\endgroup$

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