xarray.DataArray.xlm.modelfit#
- DataArray.xlm.modelfit(*args, **kwargs)#
Curve fitting optimization for arbitrary models.
Wraps
lmfit.Model.fitwithxarray.apply_ufunc().- Parameters:
coords (
Hashable,xarray.DataArray, orSequenceofHashableorxarray.DataArray) – Independent coordinate(s) over which to perform the curve fitting. Must share at least one dimension with the calling object. When fitting multi-dimensional functions, supply coords as a sequence in the same order as arguments in func. To fit along existing dimensions of the calling object, coords can also be specified as a str or sequence of strs.model (
lmfit.Model) – A model object to fit to the data. The model must be an instance oflmfit.Model.reduce_dims (
str,IterableofHashableorNone, optional) – Additional dimension(s) over which to aggregate while fitting. For example, calling ds.xlm.modelfit(coords=’time’, reduce_dims=[‘lat’, ‘lon’], …) will aggregate all lat and lon points and fit the specified function along the time dimension.skipna (
bool, default:True) – Whether to skip missing values when fitting. Default is True.params (
lmfit.Parameters,dict-like,DataArrayorDataset, optional) – Optional input parameters to the fit.If a
lmfit.Parametersobject, it will be used for all fits.If a dict-like object, it must be structured like keyword arguments to
lmfit.create_params.Additionally, each value of the dictionary may also be a DataArray, which will be broadcasted appropriately.
If a DataArray, each entry must be a dict-like object, a
lmfit.Parametersobject, or a JSON string created withlmfit.Parameters.dumps.If a Dataset, the name of the data variables in the Dataset must match the name of the data variables in the calling object, and each data variable will be used as the parameters for the corresponding data variable.
If none or only some parameters are passed, the rest will be assigned initial values and bounds with
lmfit.Model.make_params, or guessed withlmfit.Model.guessif guess is True.guess (
bool, default: False) – Whether to guess the initial parameters withlmfit.Model.guess. For composite models, the parameters will be guessed for each component.errors (
{"raise", "ignore"}, default:"raise") – If ‘raise’, any errors from thelmfit.Model.fitoptimization will raise an exception. If ‘ignore’, the return values for the coordinates where the fitting failed will be NaN.progress (
bool, default: False) – Whether to show a progress bar for fitting over data variables. Only useful if there are multiple data variables to fit. Requires thetqdmpackage to be installed.output_result (
bool, default: True) – Whether to include the fulllmfit.model.ModelResultobject in the output dataset. If True, the result will be stored in a variable named modelfit_results.param_names (
listofstr, optional) – List of parameter names to include in the output dataset. If not provided, defaults tolmfit.Model.param_names(after callinglmfit.Model.make_params).**kwargs (optional) – Additional keyword arguments to passed to
lmfit.Model.fit.The
weightskeyword argument may also be provided as a DataArray, which will be broadcasted appropriately. The weights must share at least one dimension with the calling object.
- Returns:
A single dataset which contains:
- modelfit_results
The full
lmfit.model.ModelResultobject from the fit. Only included if output_result is True.- modelfit_coefficients
The coefficients of the best fit.
- modelfit_stderr
The standard errors of the coefficients.
- modelfit_covariance
The covariance matrix of the coefficients. Note that elements corresponding to non varying parameters are set to NaN, and the actual size of the covariance matrix may be smaller than the array.
- modelfit_stats
Statistics from the fit. See
lmfit.minimize.- modelfit_data
Data used for the fit.
- modelfit_best_fit
The best fit data of the fit.
- Return type: