xarray.DataArray.xlm.modelfit#

DataArray.xlm.modelfit(*args, **kwargs)#

Curve fitting optimization for arbitrary models.

Wraps lmfit.Model.fit with xarray.apply_ufunc().

Parameters:
  • coords (Hashable, xarray.DataArray, or Sequence of Hashable or xarray.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 of lmfit.Model.

  • reduce_dims (str, Iterable of Hashable or None, 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, DataArray or Dataset, optional) – Optional input parameters to the fit.

    • If a lmfit.Parameters object, 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.Parameters object, or a JSON string created with lmfit.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 with lmfit.Model.guess if guess is True.

  • guess (bool, default: False) – Whether to guess the initial parameters with lmfit.Model.guess. For composite models, the parameters will be guessed for each component.

  • errors ({"raise", "ignore"}, default: "raise") – If ‘raise’, any errors from the lmfit.Model.fit optimization 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 the tqdm package to be installed.

  • output_result (bool, default: True) – Whether to include the full lmfit.model.ModelResult object in the output dataset. If True, the result will be stored in a variable named modelfit_results.

  • param_names (list of str, optional) – List of parameter names to include in the output dataset. If not provided, defaults to lmfit.Model.param_names (after calling lmfit.Model.make_params).

  • **kwargs (optional) – Additional keyword arguments to passed to lmfit.Model.fit.

    The weights keyword 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.ModelResult object 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:

xarray.Dataset