I have read "StatisticalModelAnalysis-Tutorial" and try to follow the definitions to calculate the ParameterError step-by-step. What I figured out is ParameterErrors
in nlm["ParameterTable"]
is calculated from diagonal entries of Covariance Matrix Sqrt[nlm["CovarianceMatrix"]] // Diagonal
. From link above the definition of CovarianceMatrix
is as follows:
"The matrix is sigma^2 (X^T W X)^(-1), where sigma^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."
This is the Function, data and weights I putted in NonLinearModelFit
:
f = Function[{a, b, x}, b + a*x];
data = {{6., 21.}, {8., 31.}, {9., 39.}};
weights = {1., 1., 1.};
nlm = NonlinearModelFit[data, f[a, b, x], {a, b}, x, Weights -> weights, VarianceEstimatorFunction -> (1 &)];
The "DesignMatrix" X
I think is produced from y_i
estimates:
para = a /. nlm["BestFitParameters"];
parb = b /. nlm["BestFitParameters"];
yestimates = f[para, parb, data[[All, 1]]]
designmatrix = DesignMatrix[data, f[para, parb, x], x];
designmatrix // MatrixForm
Now one point I do not understand. What is sigma^2
? I thought it could be EstimatedVariance:
varianceestimate = nlm["EstimatedVariance"];
Which in my case gives 1 because I did VarianceEstimatorFunction-> (1&)
in nlm.
The "Matrix of weights" W I thought to be:
matrixofweights = DiagonalMatrix[weights];
And now calculate the Covariance Matrix from definiton above:
varianceestimate*Inverse[Transpose[designmatrix].matrixofweights.designmatrix]//MatrixForm
out:{{6.08061, -0.189471},{-0.189471, 0.00624628}}
And comparison with original CovarianceMarix don't give the same result:
nlm["CovarianceMatrix"] // MatrixForm
out: {{0.214286, -1.64286}, {-1.64286, 12.9286}}
Can somebody help?
Edit: By the way, at least there is no factor between these different results. so my mistake could not only due to sigma^2
.
Covariance
andVariance
? en.wikipedia.org/wiki/Covariance en.wikipedia.org/wiki/Variance $\endgroup$Sqrt[nlm["CovarianceMatrix"]] // Diagonal
in this case I'm sure... I'am not sure in expression "variance estimate" $\endgroup$