I have a dictionary which values are lists (of lists of strings).
Some of these strings are names, used as variables for statistical models (ARIMA-- a time-series model).
There is a variable name for the model, and a variable name for the results (after .fit function is applied).
This resulting "results" variable now should have attributes '.llf', '.aic', '.bic', etc. which come from the statsmodels.tsa.arima.model ARIMA package (from statsmodels.tsa.arima.model import ARIMA).
The strings in the dictionary have a name like "results_ret_ar_1_ma_1" and I want to be able to pull out from the corresponding ARIMA class, the llf, aic, bic attributes. However, the .llf, etc. only apply to an ARIMA class, not to a string. When I concatenate within an 'exec()' function, I still have the problem of the attribute trying to be pulled from a string, not a class.
How do I "cast" or convert the type from string to ARIMA class?
I was hoping to do something analogous to this (Convert string to Python class object?):
`import sys
def str_to_class(classname): return getattr(sys.modules[name], classname)`
I think the reason for the dynamic variable generation in the existing code has to do with the volume of names that would have to semi-manually be written out (copy-paste with the parameters contained in the names substituted across too many instances).
Thank you in advance!
I've tried:
def str_to_class(classname): return getattr(statsmodels.tsa.arima.model.ARIMAResultsWrapper, classname)
and get the error: return getattr(statsmodels.tsa.arima.model.ARIMAResultsWrapper, classname) NameError: name 'statsmodels' is not defined
trying:
import statsmodels.tsa.arima.model.ARIMAResultsWrapper as ARW def str_to_class(classname): return getattr(ARW, classname)
gave the error:
import statsmodels.tsa.arima.model.ARIMAResultsWrapper as ARW ModuleNotFoundError: No module named 'statsmodels.tsa.arima.model.ARIMAResultsWrapper'; 'statsmodels.tsa.arima.model' is not a package