Statistical Model Analysis

When fitting models to data, it is often useful to analyze how well the model fits the data and how well the fitting meets the assumptions of the model. For a number of common statistical models, this is accomplished in the Wolfram System by way of fitting functions that construct (RefLink[FittedModel, paclet:ref/FittedModel]) objects.

Object for fitted model information.

(RefLink[FittedModel, paclet:ref/FittedModel]) objects can be evaluated at a point or queried for results and diagnostic information. Diagnostics vary somewhat across model types. Available model fitting functions fit linear, generalized linear, and nonlinear models.

Functions that generate (RefLink[FittedModel, paclet:ref/FittedModel]) objects.

This fits a linear model assuming x values are 1, 2, …:

lm=LinearModelFit[{1.5,3.4,7.1,8.3,10.4},x,x]

Here is the functional form of the fitted model:

Normal[lm]

This evaluates the model at x==2.5:

lm[2.5]

Here is a shortened list of available results for the linear fitted model:

lm["Properties"]//Short

The major difference between model fitting functions such as (RefLink[LinearModelFit, paclet:ref/LinearModelFit]) and functions such as (RefLink[Fit, paclet:ref/Fit]) and (RefLink[FindFit, paclet:ref/FindFit]) is the ability to easily obtain diagnostic information from the (RefLink[FittedModel, paclet:ref/FittedModel]) objects. The results are accessible without refitting the model.

This gives the residuals for the fitting:

lm["FitResiduals"]

Here multiple results are obtained at once in a list:

lm[{"BestFitParameters","ANOVATable"}]

Fitting options relevant to property computations can be passed to (RefLink[FittedModel, paclet:ref/FittedModel]) objects to override defaults.

This gives default 95% confidence intervals:

lm["ParameterConfidenceIntervals"]

Here 90% intervals are obtained:

lm["ParameterConfidenceIntervals",ConfidenceLevel->.9]

Typical data for these model-fitting functions takes the same form as data in other fitting functions such as (RefLink[Fit, paclet:ref/Fit]) and (RefLink[FindFit, paclet:ref/FindFit]).

Data specifications.

Linear Models

Linear models with assumed independent normally distributed errors are among the most common models for data. Models of this type can be fitted using the (RefLink[LinearModelFit, paclet:ref/LinearModelFit]) function.

Linear model fitting.

Linear models have the form (OverHat[y])==(Subscript[β, 0])+(Subscript[β, 1])(Subscript[f, 1])+(Subscript[β, 2])(Subscript[f, 2])+⋯, where (OverHat[y]) is the fitted or predicted value, the (Subscript[β, i]) are parameters to be fitted, and the (Subscript[f, i]) are functions of the predictor variables (Subscript[x, i]). The models are linear in the parameters (Subscript[β, i]). The (Subscript[f, i]) can be any functions of the predictor variables. Quite often the (Subscript[f, i]) are simply the predictor variables (Subscript[x, i]).

This fits a linear model to the first 20 primes:

lm=LinearModelFit[Array[Prime,20],x,x]

Options for model specification and for model analysis are available.

Options for (RefLink[LinearModelFit, paclet:ref/LinearModelFit]).

The (RefLink[Weights, paclet:ref/Weights]) option specifies weight values for weighted linear regression. The (RefLink[NominalVariables, paclet:ref/NominalVariables]) option specifies which predictor variables should be treated as nominal or categorical. With (RefLink[NominalVariables, paclet:ref/NominalVariables])->(RefLink[All, paclet:ref/All]), the model is an analysis of variance (ANOVA) model. With (RefLink[NominalVariables, paclet:ref/NominalVariables])->{(Subscript[x, 1]),…,(Subscript[x, i-1]),(Subscript[x, i+1]),…,(Subscript[x, n])} the model is an analysis of covariance (ANCOVA) model with all but the i(^th) predictor treated as nominal. Nominal variables are represented by a collection of binary variables indicating equality and inequality to the observed nominal categorical values for the variable.

(RefLink[ConfidenceLevel, paclet:ref/ConfidenceLevel]), (RefLink[VarianceEstimatorFunction, paclet:ref/VarianceEstimatorFunction]), and (RefLink[WorkingPrecision, paclet:ref/WorkingPrecision]) are relevant to the computation of results after the initial fitting. These options can be set within (RefLink[LinearModelFit, paclet:ref/LinearModelFit]) to specify the default settings for results obtained from the (RefLink[FittedModel, paclet:ref/FittedModel]) object. These options can also be set within an already constructed (RefLink[FittedModel, paclet:ref/FittedModel]) object to override the option values originally given to (RefLink[LinearModelFit, paclet:ref/LinearModelFit]).

Here are the default and mean-squared error variance estimates:

{lm["EstimatedVariance"],lm["EstimatedVariance",VarianceEstimatorFunction->(Mean[#^2]&)]}

(RefLink[IncludeConstantBasis, paclet:ref/IncludeConstantBasis]), (RefLink[LinearOffsetFunction, paclet:ref/LinearOffsetFunction]), (RefLink[NominalVariables, paclet:ref/NominalVariables]), and (RefLink[Weights, paclet:ref/Weights]) are relevant only to the fitting. Setting these options within an already constructed (RefLink[FittedModel, paclet:ref/FittedModel]) object will have no further impact on the result.

A major feature of the model-fitting framework is the ability to obtain results after the fitting. The full list of available results can be obtained using "Properties".

This is the number of properties available for linear models:

lm["Properties"]//Length

The properties include basic information about the data, fitted model, and numerous results and diagnostics.

Properties related to data and the fitted function.

The "BestFitParameters" property gives the fitted parameter values {(Subscript[β, 0]),(Subscript[β, 1]),…}. "BestFit" is the fitted function (Subscript[β, 0])+(Subscript[β, 1])(Subscript[f, 1])+(Subscript[β, 2])(Subscript[f, 2])+… and "Function" gives the fitted function as a pure function. "BasisFunctions" gives the list of functions (Subscript[f, i]), with (Subscript[f, 0]) being the constant 1 when a constant term is present in the model. The "DesignMatrix" is the design or model matrix for the data. "Response" gives the list of the response or y values from the original data.

Types of residuals.

Residuals give a measure of the pointwise difference between the fitted values and the original responses. "FitResiduals" gives the differences between the observed and fitted values {(Subscript[y, 1])-(Subscript[(OverHat[y]), 1]),(Subscript[y, 2])-(Subscript[(OverHat[y]), 2]),…}. "StandardizedResiduals" and "StudentizedResiduals" are scaled forms of the residuals. The i(^th) standardized residual is ((Subscript[y, i])-(Subscript[(OverHat[y]), i]))/(Sqrt[((OverHat[σ])^2) (1-(Subscript[h, ii]))/(Subscript[w, i])]), where ((OverHat[σ])^2) is the estimated error variance, (Subscript[h, ii]) is the i(^th) diagonal element of the hat matrix, and (Subscript[w, i]) is the weight for the i(^th) data point. The i(^th) studentized residual uses the same formula with ((OverHat[σ])^2) replaced by (Subscript[(OverHat[σ]), i]^2), the variance estimate omitting the i(^th) data point.

Properties related to the sum of squared errors.

"ANOVATable" gives a formatted analysis of variance table for the model. "ANOVATableEntries" gives the numeric entries in the table and the remaining ANOVATable properties give the elements of columns in the table so individual parts of the table can easily be used in further computations.

This gives a formatted ANOVA table for the fitted model:

lm["ANOVATable"]

Here are the elements of the MS column of the table:

lm["ANOVATableMeanSquares"]

Properties and diagnostics for parameter estimates.

"CovarianceMatrix" gives the covariance between fitted parameters. The matrix is ((((OverHat[σ])^2)(Transpose[(X])W X))^(-1)), where ((OverHat[σ])^2) is the variance estimate, X is the design matrix, and W is the diagonal matrix of weights. "CorrelationMatrix" is the associated correlation matrix for the parameter estimates. "ParameterErrors" is equivalent to the square root of the diagonal elements of the covariance matrix.

"ParameterTable" and "ParameterConfidenceIntervalTable" contain information about the individual parameter estimates, tests of parameter significance, and confidence intervals.

Here is some data:

data={{8.71,6.92,18.89},{6.05,5.97,15.08},{6.24,0.99,5.92},{8.25,3.37,11.39},{6.58,8.22,20.77},{4.14,9.,21.09},{4.35,9.94,24.32},{8.99,4.47,13.79},{2.82,3.91,10.68},{5.14,0.4,3.82}};

This fits a model using both predictor variables:

lm2=LinearModelFit[data,{x,y},{x,y}]

These are the formatted parameter and parameter confidence interval tables:

lm2[{"ParameterTable","ParameterConfidenceIntervalTable"}]

Here 99% confidence intervals are used in the table:

lm2["ParameterConfidenceIntervalTable",ConfidenceLevel->.99]

The Estimate column of these tables is equivalent to "BestFitParameters". The t-statistics are the estimates divided by the standard errors. Each p-value is the two-sided p-value for the t-statistic and can be used to assess whether the parameter estimate is statistically significantly different from 0. Each confidence interval gives the upper and lower bounds for the parameter confidence interval at the level prescribed by the (RefLink[ConfidenceLevel, paclet:ref/ConfidenceLevel]) option. The various ParameterTable and ParameterConfidenceIntervalTable properties can be used to get the columns or the unformatted array of values from the table.

"VarianceInflationFactors" is used to measure the multicollinearity between basis functions. The i(^th) inflation factor is equal to 1/(1-(Subscript[R, i]^2)), where (Subscript[R, i]^2) is the coefficient of variation from fitting the i(^th) basis function to a linear function of the other basis functions. With (RefLink[IncludeConstantBasis, paclet:ref/IncludeConstantBasis])->(RefLink[True, paclet:ref/True]), the first inflation factor is for the constant term.

"EigenstructureTable" gives the eigenvalues, condition indices, and variance partitions for the nonconstant basis functions. The Index column gives the square root of the ratios of the eigenvalues to the largest eigenvalue. The column for each basis function gives the proportion of variation in that basis function explained by the associated eigenvector. "EigenstructureTablePartitions" gives the values in the variance partitioning for all basis functions in the table.

Properties related to influence measures.

Pointwise measures of influence are often employed to assess whether individual data points have a large impact on the fitting. The hat matrix and catcher matrix play important roles in such diagnostics. The hat matrix is the matrix H such that (OverHat[y])==H y, where y is the observed response vector and (OverHat[y]) is the predicted response vector. "HatDiagonal" gives the diagonal elements of the hat matrix. "CatcherMatrix" is the matrix C such that β==C y, where β is the fitted parameter vector.

"FitDifferences" gives the DFFITS values that provide a measure of influence of each data point on the fitted or predicted values. The i(^th) DFFITS value is given by (Sqrt[(Subscript[h, ii])/(1-(Subscript[h, ii]))]) (Subscript[r, ti]), where (Subscript[h, ii]) is the i(^th) hat diagonal and (Subscript[r, ti]) is the i(^th) studentized residual.

"BetaDifferences" gives the DFBETAS values that provide measures of influence of each data point on the parameters in the model. For a model with p parameters, the i(^th) element of "BetaDifferences" is a list of length p with the j(^th) value giving the measure of the influence of data point i on the j(^th) parameter in the model. The i(^th) "BetaDifferences" vector can be written as {(Subscript[c, i1]),…,(Subscript[c, ip])}(Sqrt[(Subscript[r, ti])/(1-(Subscript[h, ii]))])/((∑_(j=1)^n)(∑_(k=1)^p)(Subscript[c, jk]^2)), where (Subscript[c, jk]) is the j, k(^th) element of the catcher matrix.

"CookDistances" gives the Cook distance measures of leverage. The i(^(th )) Cook distance is given by (Subscript[h, ii]) ((Subscript[r, si])^2)/p (1-(Subscript[h, ii])), where (Subscript[r, si]) is the i(^th)  standardized residual.

The i(^th) element of "CovarianceRatios" is given by (((n-p))^p)/((1-(Subscript[h, ii])) ((((Subscript[r, ti]^2)+n-p-1))^p)) and the i(^th) "FVarianceRatios" value is equal to ((Subscript[(OverHat[σ]), (i)])^2)/(((OverHat[σ])^2)(1-(Subscript[h, ii])) ), where ((Subscript[(OverHat[σ]), (i)])^2) is the i(^th) single deletion variance.

The Durbin–Watson d-statistic "DurbinWatsonD" is used for testing the existence of a first-order autoregressive process. The d-statistic is equivalent to (∑_(i=1)^(n-1))((((Subscript[r, i+1])-(Subscript[r, i])))^2)/(∑_(i=1)^n)(Subscript[r, i]^2), where (Subscript[r, i]) is the i(^th) residual.

This plots the Cook distances for the bivariate model:

ListPlot[lm2["CookDistances"],Filling->0]

Properties of predicted values.

Tabular results for confidence intervals are given by "MeanPredictionConfidenceIntervalTable" and "SinglePredictionConfidenceIntervalTable". These include the observed and predicted responses, standard error estimates, and confidence intervals for each point. Mean prediction confidence intervals are often referred to simply as confidence intervals and single prediction confidence intervals are often referred to as prediction intervals.

Mean prediction intervals give the confidence interval for the mean of the response y at fixed values of the predictors and are given by (OverHat[y])±(Subscript[t, n-p])(α/2) (Sqrt[f.(OverHat[Σ]).f]), where (Subscript[t, n-p])(α/2)  is the α/2 quantile of the Student t distribution with n-p degrees of freedom, f is the vector of basis functions evaluated at fixed predictors, and (OverHat[Σ]) is the estimated covariance matrix for the parameters. Single prediction intervals provide the confidence interval for predicting y at fixed values of the predictors, and are given by (OverHat[y])±(Subscript[t, n-p])(α/2) (Sqrt[((OverHat[σ])^2)+f.(OverHat[Σ]).f]), where ((OverHat[σ])^2) is the estimated error variance.

"MeanPredictionBands" and "SinglePredictionBands" give formulas for mean and single prediction confidence intervals as functions of the predictor variables.

Here is the mean prediction table:

lm2["MeanPredictionConfidenceIntervalTable"]

This gives the 90% mean prediction intervals:

lm2["MeanPredictionConfidenceIntervals",ConfidenceLevel->.9]

Goodness-of-fit measures.

Goodness-of-fit measures are used to assess how well a model fits or to compare models. The coefficient of determination "RSquared" is the ratio of the model sum of squares to the total sum of squares. "AdjustedRSquared" penalizes for the number of parameters in the model and is given by 1-(((n-1)/(n-p)))(1-(R^2)).

"AIC" and "BIC" are likelihood-based goodness-of-fit measures. Both are equal to -2 times the log-likelihood for the model plus k p, where p is the number of parameters to be estimated including the estimated variance. For "AIC" k is 2, and for "BIC" k is log(n).

Generalized Linear Models

The linear model can be seen as a model with each response value y being an observation from a normal distribution with mean value (OverHat[y])==(Subscript[β, 0])+(Subscript[β, 1])(Subscript[f, 1]) +(Subscript[β, 2])(Subscript[f, 2])+⋯. The generalized linear model extends to models of the form (OverHat[y])==(g^(-1))((Subscript[β, 0])+(Subscript[β, 1])(Subscript[f, 1]) +(Subscript[β, 2])(Subscript[f, 2]) +…), with each y assumed to be an observation from a distribution of known exponential family form with mean (OverHat[y]), and g being an invertible function over the support of the exponential family. Models of this sort can be obtained via (RefLink[GeneralizedLinearModelFit, paclet:ref/GeneralizedLinearModelFit]).

Generalized linear model fitting.

The invertible function g is called the link function and the linear combination (Subscript[f, 1]) (Subscript[β, 1])+(Subscript[f, 2]) (Subscript[β, 2])+(Subscript[β, 0])+⋯ is referred to as the linear predictor. Common special cases include the linear regression model with the identity link function and Gaussian or normal exponential family distribution, logit and probit models for probabilities, Poisson models for count data, and gamma and inverse Gaussian models.

The error variance is a function of the prediction (OverHat[y]) and is defined by the distribution up to a constant ϕ, which is referred to as the dispersion parameter. The error variance for a fitted value (OverHat[y]) can be written as (OverHat[ϕ]) v((OverHat[y])), where (OverHat[ϕ]) is an estimate of the dispersion parameter obtained from the observed and predicted response values, and v((OverHat[y])) is the variance function associated with the exponential family evaluated at the value (OverHat[y]).

This fits a linear regression model:

glm1=GeneralizedLinearModelFit[Sqrt[Range[10]],x,x]

This fits a canonical gamma regression model to the same data:

glm2=GeneralizedLinearModelFit[Sqrt[Range[10]],x,x,ExponentialFamily->"Gamma"]

Here are the functional forms of the models:

Map[Normal,{glm1,glm2}]

Logit and probit models are common binomial models for probabilities. The link function for the logit model is log((y/(1-y))) and the link for the probit model is the inverse CDF for a standard normal distribution (Sqrt[2]) (erf^(-1))(2 y-1). Models of this type can be fitted via (RefLink[GeneralizedLinearModelFit, paclet:ref/GeneralizedLinearModelFit]) with (RefLink[ExponentialFamily, paclet:ref/ExponentialFamily])->"Binomial" and the appropriate (RefLink[LinkFunction, paclet:ref/LinkFunction]) or via (RefLink[LogitModelFit, paclet:ref/LogitModelFit]) and (RefLink[ProbitModelFit, paclet:ref/ProbitModelFit]).

Logit and probit model fitting.

Parameter estimates are obtained via iteratively reweighted least squares with weights obtained from the variance function of the assumed distribution. Options for (RefLink[GeneralizedLinearModelFit, paclet:ref/GeneralizedLinearModelFit]) include options for iteration fitting such as (RefLink[PrecisionGoal, paclet:ref/PrecisionGoal]), options for model specification such as (RefLink[LinkFunction, paclet:ref/LinkFunction]), and options for further analysis such as (RefLink[ConfidenceLevel, paclet:ref/ConfidenceLevel]).

Options for (RefLink[GeneralizedLinearModelFit, paclet:ref/GeneralizedLinearModelFit]).

The options for (RefLink[LogitModelFit, paclet:ref/LogitModelFit]) and (RefLink[ProbitModelFit, paclet:ref/ProbitModelFit]) are the same as for (RefLink[GeneralizedLinearModelFit, paclet:ref/GeneralizedLinearModelFit]) except that (RefLink[ExponentialFamily, paclet:ref/ExponentialFamily]) and (RefLink[LinkFunction, paclet:ref/LinkFunction]) are defined by the logit or probit model and so are not options to (RefLink[LogitModelFit, paclet:ref/LogitModelFit]) and (RefLink[ProbitModelFit, paclet:ref/ProbitModelFit]).

(RefLink[ExponentialFamily, paclet:ref/ExponentialFamily]) can be "Binomial", "Gamma", "Gaussian", "InverseGaussian", "Poisson", or "QuasiLikelihood". Binomial models are valid for responses from 0 to 1. Poisson models are valid for non-negative integer responses. Gaussian or normal models are valid for real responses. Gamma and inverse Gaussian models are valid for positive responses. Quasi-likelihood models define the distributional structure in terms of a variance function v(μ) such that the log of the quasi-likelihood function for the i(^th) data point is given by (∫_(Subscript[y, i])^(OverHat[(Subscript[y, i])]))(((Subscript[y, i])-μ)/(ϕ v(μ)))dμ. The variance function for a "QuasiLikelihood" model can be optionally set via (RefLink[ExponentialFamily, paclet:ref/ExponentialFamily])->{"QuasiLikelihood", "VarianceFunction"->fun}, where fun is a pure function to be applied to fitted values.

(RefLink[DispersionEstimatorFunction, paclet:ref/DispersionEstimatorFunction]) defines a function for estimating the dispersion parameter ϕ. The estimate (OverHat[ϕ]) is analogous to ((OverHat[σ])^2) in linear and nonlinear regression models.

(RefLink[ExponentialFamily, paclet:ref/ExponentialFamily]), (RefLink[IncludeConstantBasis, paclet:ref/IncludeConstantBasis]), (RefLink[LinearOffsetFunction, paclet:ref/LinearOffsetFunction]), (RefLink[LinkFunction, paclet:ref/LinkFunction]), (RefLink[NominalVariables, paclet:ref/NominalVariables]), and (RefLink[Weights, paclet:ref/Weights]) all define some aspect of the model structure and optimization criterion and can only be set within (RefLink[GeneralizedLinearModelFit, paclet:ref/GeneralizedLinearModelFit]). All other options can be set either within (RefLink[GeneralizedLinearModelFit, paclet:ref/GeneralizedLinearModelFit]) or passed to the (RefLink[FittedModel, paclet:ref/FittedModel]) object when obtaining results and diagnostics. Options set in evaluations of (RefLink[FittedModel, paclet:ref/FittedModel]) objects take precedence over settings given to (RefLink[GeneralizedLinearModelFit, paclet:ref/GeneralizedLinearModelFit]) at the time of the fitting.

This gives 95% and 99% confidence intervals for the parameters in the gamma model:

{glm2["ParameterConfidenceIntervals"],glm2["ParameterConfidenceIntervals",ConfidenceLevel->.99]}

Properties related to data and the fitted function.

"BestFitParameters" gives the parameter estimates for the basis functions. "BestFit" gives the fitted function (g^(-1))((Subscript[β, 0])+(Subscript[β, 1])(Subscript[f, 1])+(Subscript[β, 2])(Subscript[f, 2])+…), and "LinearPredictor" gives the linear combination (Subscript[β, 0])+(Subscript[β, 1])(Subscript[f, 1])+(Subscript[β, 2])(Subscript[f, 2])+…. "BasisFunctions" gives the list of functions (Subscript[f, i]), with (Subscript[f, 0]) being the constant 1 when a constant term is present in the model. "DesignMatrix" is the design or model matrix for the basis functions.

Properties related to dispersion and model deviances.

Deviances and deviance tables generalize the model decomposition given by analysis of variance in linear models. The deviance for a single data point is 2 (OverHat[ϕ])((Subscript[ℓ, m])(y)-(Subscript[ℓ, m])((OverHat[y]))), where (Subscript[ℓ, m]) is the log-likelihood function for the fitted model. "Deviances" gives a list of the deviance values for all data points. The sum of all deviances gives the model deviance. The model deviance can be decomposed as sums of squares, which are in an ANOVA table for linear models. The full model is the model whose predicted values are the same as the data.

Here is some data with two predictor variables:

glmdata={{3.58,1.83,0.21},{3.58,4.47,0.17},{3.58,3.11,0.19},{3.58,4.2,0.18},{3.58,2.83,0.2},{4.01,2.53,0.19},{4.01,3.63,0.18},{4.01,3.93,0.17},{4.01,4.05,0.17},{4.01,1.33,0.22},{2.01,3.46,0.22},{2.01,1.77,0.25},{2.01,2.44,0.23},{2.01,2.39,0.25},{2.01,3.78,0.2},{3.59,3.49,0.19},{3.59,3.82,0.19},{3.59,3.05,0.2},{3.59,4.51,0.17},{3.59,3.37,0.18},{2.62,1.42,0.25},{2.62,3.9,0.19},{2.62,2.51,0.21},{2.62,4.59,0.18},{2.62,4.28,0.19}};

This fits the data to an inverse Gaussian model:

glm3=GeneralizedLinearModelFit[glmdata,{x,y},{x,y},ExponentialFamily->"InverseGaussian"]

Here is the deviance table for the model:

glm3["DevianceTable"]

As with sums of squares, deviances are additive. The Deviance column of the table gives the increase in the model deviance when the given basis function is added. The Residual Deviance column gives the difference between the model deviance and the deviance for the submodel containing all previous terms in the table. For large samples, the increase in deviance is approximately (χ^2) distributed with degrees of freedom equal to that for the basis function in the table.

"NullDeviance" is the deviance for the null model, the constant model equal to the mean of all observed responses for models including a constant, or (g^(-1))(0) if a constant term is not included.

As with "ANOVATable", a number of properties are included to extract the columns or unformatted array of entries from "DevianceTable".

Types of residuals.

"FitResiduals" is the list of residuals, differences between the observed and predicted responses. Given the distributional assumptions, the magnitude of the residuals is expected to change as a function of the predicted response value. Various types of scaled residuals are employed in the analysis of generalized linear models.

If (Subscript[d, i]) and (Subscript[r, i])==(Subscript[y, i])-(Subscript[(OverHat[y]), i]) are the deviance and residual for the i(^th) data point, the i(^th) deviance residual is given by (Subscript[r, di])==(Sqrt[(Subscript[d, i])]) sgn((Subscript[r, i]) ). The i(^th) Pearson residual is defined as (Subscript[r, pi])==(Subscript[r, i])/(Sqrt[v((Subscript[(OverHat[y]), i]))]), where v is the variance function for the exponential family distribution. Standardized deviance residuals and standardized Pearson residuals include division by (Sqrt[ (OverHat[ϕ])(1-(Subscript[h, ii]))]), where (Subscript[h, ii]) is the i(^(th )) diagonal of the hat matrix. "LikelihoodResiduals" values combine deviance and Pearson residuals. The i(^th) likelihood residual is given by  sgn((Subscript[r, i]) )(Sqrt[(((Subscript[r, di])^2)+(Subscript[h, ii]) ((Subscript[r, pi])^2)/(1-(Subscript[h, ii])))/(OverHat[ϕ])]).

"AnscombeResiduals" provide a transformation of the residuals toward normality, so a plot of these residuals should be expected to look roughly like white noise. The i(^th) Anscombe residual can be written as ((v((Subscript[(OverHat[y]), i])))^(1/3)) /(Sqrt[v((Subscript[(OverHat[y]), i]))])(∫_(Subscript[(OverHat[y]), i])^(Subscript[y, i]))(((((v(μ))^(1/3))))^(-1))dμ.

"WorkingResiduals" gives the residuals from the last step of the iterative fitting. The i(^th) working residual can be obtained as (Subscript[r, i]) ((∂g(μ))/(∂μ)) evaluated at μ==(Subscript[(OverHat[y]), i]).

This plots the residuals and Anscombe residuals for the inverse Gaussian model:

Map[ListPlot[#,Filling->0]&,glm3[{"FitResiduals","AnscombeResiduals"}]]

Properties and diagnostics for parameter estimates.

"CovarianceMatrix" gives the covariance between fitted parameters and is very similar to the definition for linear models. With (RefLink[CovarianceEstimatorFunction, paclet:ref/CovarianceEstimatorFunction])->"ExpectedInformation", the expected information matrix obtained from the iterative fitting is used. The matrix is (((OverHat[ϕ])(Transpose[(X])W X))^(-1)), where X is the design matrix and W is the diagonal matrix of weights from the final stage of the fitting. The weights include both weights specified via the (RefLink[Weights, paclet:ref/Weights]) option and the weights associated with the distribution's variance function. With (RefLink[CovarianceEstimatorFunction, paclet:ref/CovarianceEstimatorFunction])->"ObservedInformation", the matrix is given by -ϕ (I^(-1)), where I is the observed Fisher information matrix, which is the Hessian of the log-likelihood function with respect to parameters of the model.

"CorrelationMatrix" is the associated correlation matrix for the parameter estimates. "ParameterErrors" is equivalent to the square root of the diagonal elements of the covariance matrix.  "ParameterTable" and "ParameterConfidenceIntervalTable" contain information about the individual parameter estimates, tests of parameter significance, and confidence intervals. The test statistics for generalized linear models asymptotically follow normal distributions.

Properties related to influence measures.

"CookDistances" and "HatDiagonal" extend the leverage measures from linear regression to generalized linear models. The hat matrix from which the diagonal elements are extracted is defined using the final weights of the iterative fitting.

The Cook distance measures of leverage are defined as in linear regression with standardized residuals replaced by standardized Pearson residuals. The i(^th) Cook distance is given by (Subscript[h, ii]) / (1-(Subscript[h, ii]))((Subscript[r, spi])^2)/p, where (Subscript[r, spi]) is the i(^th)  standardized Pearson residual.

Properties of predicted values.

Goodness-of-fit measures.

"LogLikelihood" is the log-likelihood for the fitted model. "AIC" and "BIC" are penalized log-likelihood measures k p+2 ℓ, where ℓ is the log-likelihood for the fitted model, p is the number of parameters estimated including the dispersion parameter, and k is 2 for "AIC" and log(n) for "BIC" for a model of n data points. "LikelihoodRatioStatistic" is given by 2 (ℓ-(Subscript[ℓ, 0])), where (Subscript[ℓ, 0]) is the log-likelihood for the null model.

A number of the goodness-of-fit measures generalize (R^2) from linear regression as either a measure of explained variation or as a likelihood-based measure. "CoxSnellPseudoRSquared" is given by 1-((((e^((Subscript[ℓ, 0])-ℓ))))^(2/n)). "CraggUhlerPseudoRSquared" is a scaled version of Cox and Snell's measure (1-((((e^((Subscript[ℓ, 0])-ℓ))))^(2/n)))/(1-((((e^(Subscript[ℓ, 0]))))^(2/n))). "LikelihoodRatioIndex" involves the ratio of log-likelihoods 1-ℓ/(Subscript[ℓ, 0]), and "AdjustedLikelihoodRatioIndex" adjusts by penalizing for the number of parameters 1-(ℓ-p)/(Subscript[ℓ, 0]). "EfronPseudoRSquared" uses the sum of squares interpretation of (R^2) and is given as 1-(∑_(i=1)^n)(Subscript[r, i]^2)/(∑_(i=1)^n)((((Subscript[y, i])-(OverBar[y])))^2), where (Subscript[r, i]) is the i(^th)  residual and (OverBar[y]) is the mean of the responses (Subscript[y, i]).

"PearsonChiSquare" is equal to (∑_(i=1)^n)(Subscript[r, pi]^2), where the (Subscript[r, pi]) are Pearson residuals.

Nonlinear Models

A nonlinear least-squares model is an extension of the linear model where the model need not be a linear combination of basis function. The errors are still assumed to be independent and normally distributed. Models of this type can be fitted using the (RefLink[NonlinearModelFit, paclet:ref/NonlinearModelFit]) function.

Nonlinear model fitting.

Nonlinear models have the form (OverHat[y])==f((Subscript[x, 1]),…,(Subscript[x, i]),(Subscript[β, 1]),…,(Subscript[β, j])), where (OverHat[y]) is the fitted or predicted value, the (Subscript[β, i]) are parameters to be fitted, and the (Subscript[x, i]) are predictor variables. As with any nonlinear optimization problem, a good choice of starting values for the parameters may be necessary. Starting values can be given using the same parameter specifications as for (RefLink[FindFit, paclet:ref/FindFit]).

This fits a nonlinear model to a sequence of square roots:

nlm=NonlinearModelFit[Array[Sqrt,20],Log[a+b x],{a,b},x]

Options for model fitting and for model analysis are available.

Options for (RefLink[NonlinearModelFit, paclet:ref/NonlinearModelFit]).

General numeric options such as (RefLink[AccuracyGoal, paclet:ref/AccuracyGoal]), (RefLink[Method, paclet:ref/Method]), and (RefLink[WorkingPrecision, paclet:ref/WorkingPrecision]) are the same as for (RefLink[FindFit, paclet:ref/FindFit]).

The (RefLink[Weights, paclet:ref/Weights]) option specifies weight values for weighted nonlinear regression. The optimal fit is for a weighted sum of squared errors.

All other options can be relevant to computation of results after the initial fitting. They can be set within (RefLink[NonlinearModelFit, paclet:ref/NonlinearModelFit]) for use in the fitting and to specify the default settings for results obtained from the (RefLink[FittedModel, paclet:ref/FittedModel]) object. These options can also be set within an already constructed (RefLink[FittedModel, paclet:ref/FittedModel]) object to override the option values originally given to (RefLink[NonlinearModelFit, paclet:ref/NonlinearModelFit]).

Properties related to data and the fitted function.

Basic properties of the data and fitted function for nonlinear models behave like the same properties for linear and generalized linear models with the exception that "BestFitParameters" returns a rule as is done for the result of (RefLink[FindFit, paclet:ref/FindFit]).

This gives the fitted function and rules for the parameter estimates:

nlm[{"BestFit","BestFitParameters"}]

Many diagnostics for nonlinear models extend or generalize concepts from linear regression. These extensions often rely on linear approximations or large sample approximations.

Types of residuals.

As in linear regression, "FitResiduals" gives the differences between the observed and fitted values {(Subscript[y, 1])-(Subscript[(OverHat[y]), 1]),(Subscript[y, 2])-(Subscript[(OverHat[y]), 2]),…}, and "StandardizedResiduals" and "StudentizedResiduals" are scaled forms of these differences.

The i(^th) standardized residual is ((Subscript[y, i])-(Subscript[(OverHat[y]), i]))/(Sqrt[((OverHat[σ])^2) (1-(Subscript[h, ii]))/(Subscript[w, i])]), where ((OverHat[σ])^2) is the estimated error variance, (Subscript[h, ii]) is the i(^th) diagonal element of the hat matrix, and (Subscript[w, i]) is the weight for the i(^th) data point, and the i(^th) studentized residual is obtained by ((OverHat[σ])^2) replacing with the i(^th) single deletion variance (Subscript[(OverHat[σ]), i]^2). For nonlinear models a first-order approximation is used for the design matrix, which is needed to compute the hat matrix.

Properties related to the sum of squared errors.

"ANOVATable" provides a decomposition of the variation in the data attributable to the fitted function and to the errors or residuals.

This gives the ANOVA table for the nonlinear model:

nlm["ANOVATable"]

The uncorrected total sums of squares gives the sum of squared responses, while the corrected total gives the sum of squared differences between the responses and their mean value.

Properties and diagnostics for parameter estimates.

"CovarianceMatrix" gives the approximate covariance between fitted parameters. The matrix is ((((OverHat[σ])^2)(Transpose[(X])W X))^(-1)), where ((OverHat[σ])^2) is the variance estimate, X is the design matrix for the linear approximation to the model, and W is the diagonal matrix of weights. "CorrelationMatrix" is the associated correlation matrix for the parameter estimates. "ParameterErrors" is equivalent to the square root of the diagonal elements of the covariance matrix.

"ParameterTable" and "ParameterConfidenceIntervalTable" contain information about the individual parameter estimates, tests of parameter significance, and confidence intervals obtained using the error estimates.

Curvature diagnostics.

The first-order approximation used for many diagnostics is equivalent to the model being linear in the parameters. If the parameter space near the parameter estimates is sufficiently flat, the linear approximations and any results that rely on first-order approximations can be deemed reasonable. Curvature diagnostics are used to assess whether the approximate linearity is reasonable. "FitCurvatureTable" is a table of curvature diagnostics.

"MaxIntrinsicCurvature" and "MaxParameterEffectsCurvature" are scaled measures of the normal and tangential curvatures of the parameter spaces at the best-fit parameter values. "CurvatureConfidenceRegion" is a scaled measure of the radius of curvature of the parameter space at the best-fit parameter values. If the normal and tangential curvatures are small relative to the value of the "CurvatureConfidenceRegion", the linear approximation is considered reasonable. Some rules of thumb suggest comparing the values directly, while others suggest comparing with half the "CurvatureConfidenceRegion".

Here is the curvature table for the nonlinear model:

nlm["FitCurvatureTable"]

Properties related to influence measures.

The hat matrix is the matrix H such that (OverHat[y])==H y, where y is the observed response vector and (OverHat[y]) is the predicted response vector. "HatDiagonal" gives the diagonal elements of the hat matrix. As with other properties, H uses the design matrix for the linear approximation to the model.

The i(^th) element of "SingleDeletionVariances" is equivalent to ((n-p)(OverHat[σ]) -(Subscript[r, i]^2)/(1-(Subscript[h, ii])))/(n-p-1), where n is the number of data points, p is the number of parameters, (Subscript[h, ii]) is the i(^th) hat diagonal, (OverHat[σ]) is the variance estimate for the full dataset, and (Subscript[r, i]) is the i(^th) residual.

Properties of predicted values.

Tabular results for confidence intervals are given by "MeanPredictionConfidenceIntervalTable" and "SinglePredictionConfidenceIntervalTable". These results are analogous to those for linear models obtained via (RefLink[LinearModelFit, paclet:ref/LinearModelFit]), again with first-order approximations used for the design matrix.

"MeanPredictionBands" and "SinglePredictionBands" give functions of the predictor variables.

Here the fitted function and mean prediction bands are obtained:

{fit[x_],mp[x_]}=nlm[{"BestFit","MeanPredictionBands"}]

This plots the fitted curve and confidence bands:

Plot[{fit[x],mp[x]},{x,1,20}]

Goodness-of-fit measures.

"AdjustedRSquared", "AIC", "BIC", and "RSquared" are all direct extensions of the measures as defined for linear models. The coefficient of determination "RSquared" is 1-((Subscript[SS, res])/(Subscript[SS, T])), where (Subscript[SS, res]) is the residual sum of squares and (Subscript[SS, T]) is the uncorrected total sum of squares. The coefficient of determination does not have the same interpretation as the percentage of explained variation in nonlinear models as it does in linear models because the sum of squares for the model and for the residuals do not necessarily sum to the total sum of squares. "AdjustedRSquared" penalizes for the number of parameters in the model and is given by 1-((n/(n-p)))(1-(R^2)) .

"AIC" and "BIC" are equal to -2 times the log-likelihood for the model plus k p, where p is the number of parameters to be estimated including the estimated variance. For "AIC" k is 2, and for "BIC" k is log(n).