Reputation: 33
I am successfully fitting a GLIMMIX with a particular formula on some dummies variables like below, but once fitted, I can't perform an ANOVA on that same model because the L1 norm isn't the right dimensions (I think based on the error prompt). Any guess what could be wrong here?
T_ctr_vec = np.random.rand(15,12)
T_pat_vec = np.random.rand(15,12)
mask_K = np.ones((K,K)) - np.eye(K,K)
mask_K_ind = np.where(mask_K != 0)
n_pat = T_pat_vec.shape[0]
n_ctr = T_ctr_vec.shape[0]
Y = np.concatenate((T_ctr_vec, T_pat_vec), axis=1).T.flatten()
TT = np.tile(mask_K_ind[0], n_ctr + n_pat)
G_pat = np.full(n_pat, np.nan)
G_pat[0:15] = 3
G = np.concatenate((np.ones(n_ctr), G_pat), axis=0)
G = np.repeat(G, len(mask_K_ind[0]))
S = np.tile(np.arange(1, n_ctr+n_pat+1), (1, len(mask_K_ind[0])))
S = S.flatten()
tbl_gmle_ = pd.DataFrame({'TT':TT,'S':S,'G':G}).astype('category')
tbl_gmle_t_Y = pd.DataFrame({'Y':Y})
tbl_gmle_t = tbl_gmle_.join(tbl_gmle_t_Y)
import statsmodels.api as sm
import statsmodels.formula.api as smf
formula = 'Y ~ 1 + TT * G + C(S)'
model = sm.MixedLM.from_formula(formula, data=tbl_gmle_t, groups=tbl_gmle_t["S"])
model.family = sm.families.Gaussian()
result = model.fit()
anova_table = sm.stats.anova_lm(result,typ=2) # Error L1 dimensions???
That last line returns the following error:
Cell In [814], line 2
1 # Perform ANOVA
----> 2 anova_table = sm.stats.anova_lm(result,typ=2)
4 # Print the ANOVA table
5 print(anova_table)
File /opt/homebrew/Caskroom/miniforge/base/envs/working°env/lib/python3.8/site-packages/statsmodels/stats/anova.py:349, in anova_lm(*args, **kwargs)
347 if len(args) == 1:
348 model = args[0]
--> 349 return anova_single(model, **kwargs)
351 if typ not in [1, "I"]:
352 raise ValueError("Multiple models only supported for type I. "
353 "Got type %s" % str(typ))
File /opt/homebrew/Caskroom/miniforge/base/envs/working°env/lib/python3.8/site-packages/statsmodels/stats/anova.py:80, in anova_single(model, **kwargs)
77 return anova1_lm_single(model, endog, exog, nobs, design_info, table,
78 n_rows, test, pr_test, robust)
79 elif typ in [2, "II"]:
---> 80 return anova2_lm_single(model, design_info, n_rows, test, pr_test,
81 robust)
82 elif typ in [3, "III"]:
83 return anova3_lm_single(model, design_info, n_rows, test, pr_test,
84 robust)
File /opt/homebrew/Caskroom/miniforge/base/envs/working°env/lib/python3.8/site-packages/statsmodels/stats/anova.py:201, in anova2_lm_single(model, design_info, n_rows, test, pr_test, robust)
198 L2 = np.eye(model.model.exog.shape[1])[L2]
200 if L2.size:
--> 201 LVL = np.dot(np.dot(L1,robust_cov),L2.T)
202 from scipy import linalg
203 orth_compl,_ = linalg.qr(LVL)
File <__array_function__ internals>:180, in dot(*args, **kwargs)
ValueError: shapes (6,37) and (38,38) not aligned: 37 (dim 1) != 38 (dim 0)
Thanks a lot!
Upvotes: 1
Views: 99