I'm trying to fit an ordered logistic regression on Python statsmodel using statsmodels.miscmodels.ordinal_model.OrderedModel. I'm wrapping my head around this but I can't find a solution.
Long story short here is the main code snippets:
df['Ricontatto'] = pd.Categorical(df['Ricontatto'], ordered=True)
ordine_desiderato = ['sinistra', 'centro-sinistra', 'centro', 'centro-destra',
'destra']
df['Or Pol'] = pd.Categorical(df['Or Pol'], categories=ordine_desiderato,
ordered=True)
df['Extr conf Isr'] = pd.Categorical(df['Extr conf Isr'], ordered=True)
#Model
predittori = ['Mc_Isr', 'Or Pol', 'Extr conf Isr', 'SSP']
responso= ['Ricontatto']
mod_prob = OrderedModel(df[responso],
df[predittori],
distr='probit')
#Fit
results = mod_prob.fit(method='bfgs', full_output=True)
print(results.summary())
The variables: Mc_Isr is continuous, Or Pol is categorical ordered, as Extr conf Isr; SSP is continous.
result: ValueError
ValueError Traceback (most recent call last)
Cell In[27], line 5
2 predittori = ['Mc_Isr', 'Or Pol', 'Extr conf Isr', 'SSP']
3 responso= ['Ricontatto']
----> 5 mod_prob = OrderedModel(df[responso],
6 df[predittori],
7 distr='probit')
9 # Fit del modello
10 results = mod_prob.fit(method='bfgs', full_output=True)
.......
File ~\ANACONDA_LUGLIO_2024\Lib\site-packages\statsmodels\base\data.py:509, in PandasData._convert_endog_exog(self, endog, exog)
507 exog = exog if exog is None else np.asarray(exog)
508 if endog.dtype == object or exog is not None and exog.dtype == object:
--> 509 raise ValueError("Pandas data cast to numpy dtype of object. "
510 "Check input data with np.asarray(data).")
511 return super()._convert_endog_exog(endog, exog)
ValueError: Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).
The predictor and the response come from the same dataset. Equal number of rows, no missing values. The same error happens on a dataset with many more predictors, where after some variables pruning it stops fitting the model. I understand that maybe somewhere there could be an hidden intercept or a costant so the model goes awry See here, but that's more a conjecture than a informed guess. I'm no programmer. Any suggestion? Thanks in advance