How to obrain p-values for coefficients with polyval - polyparci?
19 visualizzazioni (ultimi 30 giorni)
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
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)
Sulaymon Eshkabilov
il 15 Feb 2023
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
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
‘... 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!