使用statsmodels线性混合效应模型时遇可哈希性报错求助
Hey there! Let's break down why you're hitting this error and get your linear mixed-effects model running right away.
The Root Cause
The error pops up because statsmodels' mixedlm expects your dependent variable (fc) to be a single scalar value per row, not an array/list. Your fc column holds 1×2346 arrays, which are unhashable objects—this throws off the model's data parsing logic, leading to the "all items must be hashable" message.
Step-by-Step Solution
You need to convert your wide-format data (one row per subject with multiple fc values) into long-format data (one row per individual fc value, keeping subject/group/session metadata intact). Here's how to do it:
Prepare the
fccolumn for expansion
First, make sure any numpy arrays in thefccolumn are converted to lists so pandas can handle them properly:import numpy as np import pandas as pd df['fc'] = df['fc'].apply(lambda x: x.tolist() if isinstance(x, np.ndarray) else x)Explode the
fccolumn into long format
Use pandas'explode()method to split each array into separate rows, while retaining the values from other columns:df_long = df.explode('fc', ignore_index=True)Convert
fcto a numeric type
Ensure the explodedfcvalues are treated as numbers (not strings or generic objects):df_long['fc'] = pd.to_numeric(df_long['fc'])Fit your model with the long-format data
Now use the expanded dataframe to run your model—this time it should work without the hashable error:import statsmodels.formula.api as smf my_model_fit = smf.mixedlm("fc ~ group * session", df_long, groups=df_long["subj"]).fit() print(my_model_fit.random_effects) print(my_model_fit.summary())
Why This Works
Linear mixed-effects models require one observation per row: each row should represent a single measurement (fc value) paired with its corresponding predictor variables (group, session) and grouping identifier (subj). By exploding the fc column, you align your data with this core requirement, letting the model properly estimate fixed and random effects for your dataset.
内容的提问来源于stack exchange,提问作者stasia_l

