How to obrain p-values for coefficients with polyval - polyparci?
Mostra commenti meno recenti
I am using polyfit / polyval to obtain coefficients for a simple quadratic relationship:
[p,S] = polyfit(x2,y2,order);
[y_fit,delta] = polyval(p,x2,S);
With polyparci i can compute the confidence intervals fo the coefficients:
ci = polyparci(p,S);
but there is no built-in way to have p-values for those coefficient? I know it's implicit in polyparci but I can't figure out a simple way of extracting that specific output.
Thanks in advance
3 Commenti
Torsten
il 15 Feb 2023
What do you mean by "obtain p values" ? You prescribe the p values and MATLAB returns the confidence intervals according to your p-value. Or do you want to prescribe confidence intervals to get the p values ?
the cyclist
il 15 Feb 2023
Modificato: the cyclist
il 15 Feb 2023
@Torsten, I think you may be confusing p-value with what is usually called "alpha", the Type 1 error rate threshold. alpha is prescribed ahead of modeling, and will be a factor in calculating the confidence interval. p-value is an output of the model.
The null hypothesis can be rejected if the p-value is smaller than the prescribed alpha.
Star Strider
il 15 Feb 2023
Modificato: Star Strider
il 10 Gen 2024
The ‘polyparci’ function returns the 95% parameter confidence limits. or ‘alpha’ if it is provided. The parameters are ‘significant’ at ‘p=0.05’ (or whatever ‘alpha’ value is chosen) if they have the same signs. If the signs differ, then the confidence intervals include 0, and that parameter is likely not necessary in the regression.
EDIT — (10 Jan 2024 at 18:30)
Risposte (2)
Note that polyparci() is not a MATLAB built in fcn and it is posted here. Note that it is quite stratforward to obtain p-values of a polynomial using fitlm(). Here is a short demo example for a cubic polynomial fit:
x = -2:.25:2;
y = 3*x.^2-5*x+10+randn(size(x))*10;
%% fitlm() to compute p-values of fit model coefficients:
fitlm(x,y, 'poly3')
% Here is how to display confidence interval of 95%
[FM, S]=polyfit(x,y,3); % Fit Model coeffs: FM
[FM_vals, delta] = polyval(FM,x, S); % Prediction Interval at 95%: delta
plot(x,y, 'ko', 'MarkerFaceColor','c', 'MarkerSize', 7)
hold on
plot(x, FM_vals, 'k-', 'linewidth',2)
plot(x, FM_vals+2*delta, 'r--', x, FM_vals-2*delta, 'r--')
legend('Data', 'Fit Model', '95% Prediction Interval')
grid on
xlabel('x')
ylabel('y(x)')
4 Commenti
@LEONARDO CIAMBEZI, note that fitlm is a function from the Statistics Learning Toolbox, not base MATLAB.
@Sulaymon Eshkabilov, note that your fitlm example is using 3rd-order polynomial to fit a 2nd-order function, so your fitting coefficients are somewhat nonsensical. A more suitable example is
x = -2:.25:2;
y = 3*x.^2-5*x+10+randn(size(x))*0.4;
%% fitlm() to compute p-values of fit model coefficients:
fitlm(x,y, 'poly2')
Chinmayee L M
il 10 Gen 2024
Can I use fitlm to fit the equation 'y = a*exp(b*x)' ? If yes, how?
The fit function from the curve fit toolbox fits this equation with 'exp1' model name. But, I don't get p-values from it.
Star Strider
il 10 Gen 2024
Spostato: the cyclist
il 10 Gen 2024
‘Can I use fitlm to fit the equation 'y = a*exp(b*x)' ? If yes, how?’
Example —
x = (0:0.1:20).';
y = 5*exp(-0.25*x) + randn(size(x));
mdl = fitnlm(x, y, @(b,x) b(1).*exp(b(2).*x), randn(2,1))
[ypred,yci] = predict(mdl, x);
figure
hp1 = plot(x, y, 'p', 'DisplayName','Data');
hold on
hp2 = plot(x, ypred, '-r', 'DisplayName','Regression');
hp3 = plot(x, yci, '--r', 'DisplayName', '95% Confidence Interval');
hold off
grid
legend([hp1; hp2; hp3(1)], 'Location','best')
.
the cyclist
il 10 Gen 2024
Modificato: the cyclist
il 10 Gen 2024
The equation y = a*exp(b*x) is non-linear in the parameter b, so you should not try to fit a linear model to it.
But, if you take the log of both sides of the equation, you get log(y) = log(a) + b*x, which you can sensibly fit with a linear model.
x = (0:0.1:5).';
y = 5*exp(-0.25*x) + 0.1*randn(size(x));
logy = log(y); % Transformed data
% Non-linear model
mdl_nlm = fitnlm(x, y, @(b,x) b(1).*exp(b(2).*x), randn(2,1))
% Linear model on log-transformed equation
mdl_log_lm = fitlm(x, logy)
Note that Star Strider's and my second coefficient are equal (to within the model tolerance), and my first coefficient is equal to the log of his (again with a bit of tolerance):
log(5.045)
Star Strider
il 10 Gen 2024
0 voti
‘... but there is no built-in way to have p-values for those coefficient?’
There is now. I did not realise that there was a need for it, however as an improvement to polyparci, I added that as the second output (along with some significant improvements to it) and uploaded it about a week ago.
Categorie
Scopri di più su Calculus in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

