Replies: 4 comments 2 replies
-
I second this |
Beta Was this translation helpful? Give feedback.
-
Hello, Now how to calculation standard error of the coefficient in Polynomial Fitting? |
Beta Was this translation helpful? Give feedback.
-
Here is the code for the polynomial regression: Ref: https://www.originlab.com/doc/origin-Help/PR-Algorithm EDIT: // data and polynomial order are given by
double[] x = { 1, 2, 3, 4, 5 };
double[] y = { 6.2, 16.9, 33, 57.5, 82.5 };
var order = 2;
var Ns = x.Length;
var k = order + 1; // number of parameters
var dof = Ns - k; // degree of freedom
// Create the [Ns X k] design matrix,
var X = Matrix<double>.Build.Dense(Ns, k, (i, j) => Math.Pow(x[i], j));
// Create the Y vector = [y0 y1 ...]
var Y = Vector<double>.Build.DenseOfArray(y);
// Calculate `covariance matrix`
var covariance = (X.Transpose() * X).Inverse();
// Calculate best-fitted parameters
var B = covariance * X.Transpose() * Y;
// Or use Fit.Polynomial
// var B = Vector<double>.Build.DenseOfArray(Fit.Polynomial(x, y, order));
// Calculate the residuals
var residuals = X * B - Y;
// Calculate the `residual sum of squares`
var RSS = residuals.DotProduct(residuals);
// Calculate the `reduced Chi-square`
var reducedChiSquare = RSS / dof;
// Calculate the `residual standard deviation` (also called `standard error of estimate`, or `root mean square of the error`)
var rootMSE = Math.Sqrt(reducedChiSquare);
// Calculate the `standard errors of the coefficients`
var standardErrors = covariance.Diagonal().PointwiseSqrt().Multiply(rootMSE);
// Calculate the t-values and probabilities
var tValues = B.PointwiseDivide(standardErrors);
var pValues = tValues.Map(t => 2 * (1 - StudentT.CDF(0, 1, dof, Math.Abs(t))));
// Calculate the confidence intervals at 95% confidence level
double alpha = 0.05;
var LCL = B - standardErrors * StudentT.InvCDF(0, 1, dof, 1 - alpha / 2);
var UCL = B + standardErrors * StudentT.InvCDF(0, 1, dof, 1 - alpha / 2);
var CI = (UCL - LCL) / 2;
// Calculate the `total sum of square`
var yMean = Y.Average();
var TSS = Y.Select(v => Math.Pow(v - yMean, 2)).Sum();
// The goodness of fit can be evaluated by `Coefficient of Determination`(COD)
var Rsquare = 1.0 - RSS / TSS;
// The other statistics: R-Value, adjusted R square, and norm of residuals
var Rvalue = Math.Sqrt(Rsquare);
var adjRsquare = 1 - (1 - Rsquare) * (Ns - 1) / dof;
var normOfResiduals = Math.Sqrt(RSS);
// Y = B0 + B1*X + B2*X^2
// Parameter Value Error t-value Pr(>|t|) LCL UCL CI half_width
// --------------------------------------------------------------------------------------------
// B0 -0.24 3.07019 -0.07817 0.94481 -13.44995 12.96995 13.20995
// B1 3.46286 2.33969 1.48005 0.27700 -6.60401 13.52972 10.06686
// B2 2.64286 0.38258 6.90799 0.02032 0.99675 4.28897 1.64611
// --------------------------------------------------------------------------------------------
//
// Fit statistics
// -----------------------------------------
// Degree of freedom 2
// Reduced Chi-Sqr 2.04914
// Residual Sum of Sqaures 4.09829
// R Value 0.99947
// R-Square(COD) 0.99893
// Adj. R-Square 0.99786
// Root-MSE(SD) 1.43148
// ----------------------------------------- |
Beta Was this translation helpful? Give feedback.
-
Hello, I'm trying to get the same values for multiple linear regression. Can you please help? |
Beta Was this translation helpful? Give feedback.
-
Can anyone please implement these?
Beta Was this translation helpful? Give feedback.
All reactions