连续出现 ValueError 时如何运行双向方差分析:约束矩阵中必须至少有一行

问题描述 投票:0回答:1

当我尝试运行方差分析时,我收到一条“ValueError:约束矩阵中必须至少有一行”消息。请参阅下面的示例表和我尝试运行的代码。您现在可以忽略“重新计数”栏!

计数 重述 规则 舞台
5 2 1 2
8 4 1 2
5 3 1 2
5 1 1 2
3 0 1 2
0 1 1 2

我目前拥有的代码是:

import pandas as pd
import statsmodels.api as sm
from statsmodels.formula.api import ols

data = pd.read_csv('errShift.csv')

# Check the first few rows of your data to verify the column name change
print(data.head())

# Create a model for the two-factor ANOVA
model = ols('pecount ~ C(rule) * C(stage)', data=data).fit()

# Perform the two-factor ANOVA
anova_table = sm.stats.anova_lm(model, typ=2)

# Print the ANOVA table
print(anova_table)

我希望它能产生方差分析结果,其中 pecount 作为因变量,阶段和规则作为自变量。

matrix error-handling constraints row anova
1个回答
0
投票

更新 - 我使用以下代码让它工作(我注意到的唯一区别是在第 10 行中说“data = data”与说“data = df”,也就是我命名的数据框,这可能是产生的结果错误?):

# Importing libraries
import pandas as pd
import statsmodels.api as sm
from statsmodels.formula.api import ols

# Create a dataframe
df = pd.read_csv('errShift.csv')

# Performing two-way ANOVA
model = ols('pecount ~ C(rule) + C(stage)', data=df).fit()
anova_table = sm.stats.anova_lm(model, type=2)

# Print the result
print(anova_table)
© www.soinside.com 2019 - 2024. All rights reserved.