StatsModels not aligned Error
Asked Answered
T

0

6

I am getting an error when I try to run a multivariable linear regression in Statsmodels. Everything works fine when I hardcode just one X column in the XData variable.

Can someone please give me some advice as what I'm missing here? I would greatly appreciate it.

Error:

ValueError: shapes (747,2) and (747,2) not aligned: 2 (dim 1) != 747 (dim 0)

Code:

import pandas as pd
import statsmodels.api as sm
import itertools

data = pd.read_csv("deaconFoodData.csv")

for i in range(2,10,1):
    xCombinations = itertools.combinations(["Food Exp","HH Size","HH Inc","Highest Ed Head","Age Head","Shopping Time","Kid <6","Kid 6-18","Eating Healthy"], i)
    print(str(i) + " variables")
    for combination in xCombinations:

        comb = list(combination)

        print(comb)
        xData = data[["Food Exp", "HH Size"]] # data[comb]
        yData = data["Shopping LH"]
        yData = sm.add_constant(yData, prepend=False)
        print(yData)
        # Fit and summarize OLS model
        mod = sm.OLS(xData, yData)
        res = mod.fit()
        print(res.rsquared)

GitHub Link: https://github.com/deacons2016/DeaconFood

Therapy answered 23/11, 2015 at 3:34 Comment(7)
OLS regression can deal with multiple predictors (what you are calling yData), but not multiple outcomes (your xData). Regression with multiple outcomes is usually referred to as "multivariate regression".Tactile
I'm not sure I understand. Ydata only has one column,which is the likelihood column. Xdata is where my independent variables are.Therapy
Oh, in that case I think you are just getting the order of the arguments mixed up, it should be sm.OLS(yData, xData), and you should be add_constanting() your xData, not your yData.Tactile
@Tactile you're right. Thanks!!!Therapy
Can anyone suggest what to use instead? I am facing similar issues? ValueError: shapes (260,0) and (260,0) not aligned: 0 (dim 1) != 260 (dim 0)Kilowatthour
The OLS part runs, its the print(res.summary()) that failsKilowatthour
Did you get an answer @ctrl-alt-deletRikkiriksdag

© 2022 - 2024 — McMap. All rights reserved.