3.7.3.1.3. statsmodels.miscmodels.nonlinls.NonlinearLS

class statsmodels.miscmodels.nonlinls.NonlinearLS(endog=None, exog=None, weights=None, sigma=None, missing='none')[source]

Base class for estimation of a non-linear model with least squares

This class is supposed to be subclassed, and the subclass has to provide a method _predict that defines the non-linear function `f(params) that is predicting the endogenous variable. The model is assumed to be

Math:y = f(params) + error

and the estimator minimizes the sum of squares of the estimated error.

Math:min_parmas sum (y - f(params))**2

f has to return the prediction for each observation. Exogenous or explanatory variables should be accessed as attributes of the class instance, and can be given as arguments when the instance is created.

Warning: Weights are not correctly handled yet in the results statistics, but included when estimating the parameters.

similar to scipy.optimize.curve_fit API difference: params are array_like not split up, need n_params information

includes now weights similar to curve_fit no general sigma yet (OLS and WLS, but no GLS)

This is currently holding on to intermediate results that are not necessary but useful for testing.

Fit returns and instance of RegressionResult, in contrast to the linear model, results in this case are based on a local approximation, essentially y = f(X, params) is replaced by y = grad * params where grad is the Gradient or Jacobian with the shape (nobs, nparams). See for example Greene

Examples

class Myfunc(NonlinearLS):

def _predict(self, params):
x = self.exog a, b, c = params return a*np.exp(-b*x) + c

Ff we have data (y, x), we can create an instance and fit it with

mymod = Myfunc(y, x) myres = mymod.fit(nparams=3)

and use the non-linear regression results, for example

myres.params myres.bse myres.tvalues

__init__(endog=None, exog=None, weights=None, sigma=None, missing='none')[source]

3.7.3.1.3.1. Methods

__init__([endog, exog, weights, sigma, missing])
errorsumsquares(params)
fit([start_value, nparams])
fit_minimal(start_value) minimal fitting with no extra calculations
fit_random([ntries, rvs_generator, nparams]) fit with random starting values
from_formula(formula, data[, subset]) Create a Model from a formula and dataframe.
geterrors(params[, weights])
jac_predict(params) jacobian of prediction function using complex step derivative
predict(exog[, params])
start_value()

3.7.3.1.3.2. Attributes

endog_names
exog_names