Trapping Error in Do2VStats() - mark4flies - 03-10-2018 05:47 PM
I am writing a program to select the best fit from the available models in the Statistics 2Var app. I plan to submit it when everything is working.
(It publishes two functions that compute the available criteria for model selection. R square should be used to evaluate the performance of the selected model. It should [b]not[/b] be used to select the model. AICc and adjusted R square are available as the model selection criterion.)
I am able to step through the program for the first several models and see the correct values in the variables but when the error occurs, the debugger kicks me out with a syntax error message. I tried to trap it with IFERR but I am not getting anywhere.
(I have been further hampered by the instability of the virtual calculators on macOS and iOS where the button and field labels disappear or changes in the program editor are not saved. This behavior is particularly troublesome in the debugger. I have to close the VC every five minutes or so.)
Thanks in advance for any help you might offer!
Code:
#pragma mode( separator(.,;) integer(h32) )
EXPORT AICc(sse,n,p)
BEGIN
IF sse <= 0 THEN MSGBOX("SSE must be greater than zero"); RETURN "Error"; END;
IF n < 3 THEN MSGBOX("n must be greater than 2"); RETURN "Error"; END;
IF n-p < 2 THEN MSGBOX("n must be greater than p by 2"); RETURN "Error"; END;
IF p < 1 THEN MSGBOX("p must be greater than zero"); RETURN "Error"; END;
RETURN n * LN( 2 * Pi() * sse / n ) + (n + p) / (1 - (p + 2) / n);
END;
EXPORT AdjRSqr(rSqr,n,p)
BEGIN
IF rSqr <= 0 OR rSqr >= 1 THEN MSGBOX("R square must be in interval (0,1)"); RETURN "Error"; END;
IF n < 3 THEN MSGBOX("n must be greater than 2"); RETURN "Error"; END;
IF n-p < 1 THEN MSGBOX("n must be greater than p"); RETURN "Error"; END;
IF p < 1 THEN MSGBOX("p must be positive"); RETURN "Error"; END;
RETURN 1-((n-1)*(1-rSqr)/(n-p));
END;
criterion := 1;
bestfitresults := MAKELIST("",X,1,11);
VIEW "Start", START()
BEGIN
criterion := 1;
bestfitresults := MAKELIST("",X,1,11);
STARTVIEW(2,1);
END;
VIEW "Select Criterion",SELECTCRIT()
BEGIN
CHOOSE(criterion,"Criterion","AICc","Adj R Sqr");
STARTVIEW(2,1);
END;
VIEW "Best Fit",BESTFIT()
BEGIN
LOCAL p,f,res,sse;
p := {2,2,2,2,2,2,3,3,4,5,4};
FOR f FROM 1 TO 11 DO
S1(4) := f;
Do2VStats(S1);
IF criterion == 1 THEN
res := Resid(S1);
sse := ΣLIST(res.*res);
bestfitresults(f) := AICc(sse,NbItem,p(f));
ELSE
bestfitresults(f) := AdjRSqr(CoefDet,NbItem,p(f));
END;
END;
// identify best fit (minimum AICc or maximum adj R square) and set S1
RETURN bestfitresults;
STARTVIEW(1,1);
END;
VIEW "Plot Criterion",PLOTCRITERION()
BEGIN
// push bestfitresults to D1 and make line plot with autoscale in Statistics 1Var
END;
VIEW "Autoscale",AUTOSCALE()
BEGIN
STARTVIEW(10,1);
END;
RE: Trapping Error in Do2VStats() - mark4flies - 03-10-2018 09:58 PM
I have made some progress but it still does not seem to work as expected. Have a look please!
Code:
#pragma mode( separator(.,;) integer(h32) )
EXPORT AICc(sse,n,p)
BEGIN
IF sse <= 0 THEN MSGBOX("SSE must be greater than zero"); RETURN "Error"; END;
IF n ≤ p+2 THEN MSGBOX("n must be greater than p by 2"); RETURN "Error"; END;
IF p < 1 THEN MSGBOX("p must be greater than zero"); RETURN "Error"; END;
RETURN n * LN( 2 * Pi() * sse / n ) + (n + p) / (1 - (p + 2) / n);
END;
EXPORT AdjRSqr(rSqr,n,p)
BEGIN
IF rSqr <= 0 OR rSqr >= 1 THEN MSGBOX("R square must be in interval (0,1)"); RETURN "Error"; END;
IF n < 3 THEN MSGBOX("n must be greater than 2"); RETURN "Error"; END;
IF n ≤ p THEN MSGBOX("n must be greater than p"); RETURN "Error"; END;
IF p < 1 THEN MSGBOX("p must be positive"); RETURN "Error"; END;
RETURN 1-((n-1)*(1-rSqr)/(n-p));
END;
criterion := 1;
bestfitresult := {};
fit := {};
VIEW "Start", START()
BEGIN
criterion := 1;
bestfitresult := {};
fit := {};
STARTVIEW(6,1);
END;
VIEW "Select Criterion",SELECTCRIT()
BEGIN
CHOOSE(criterion,"Criterion","AICc","Adj R Sqr");
STARTVIEW(2,1);
END;
VIEW "Best Fit",BestFit()
BEGIN
LOCAL f,sse,r2,bestf,bestresult;
LOCAL p := {2,2,2,2,2,2,3,3,4,5,4,2};
FOR f FROM 1 TO 12 DO
S1(4) := f;
Do2VStats(S1);
CASE
IF criterion == 1 THEN
sse := ΣLIST(Resid(S1)^2);
IF sse > 0 AND NbItem > (p(f)+2) THEN
CONCAT(bestfitresult,AICc(sse,NbItem,p(f)));
CONCAT(fit,f);
END;
END;
IF criterion == 2 THEN
IFERR r2 := CoefDet THEN
BREAK;
ELSE
IF NbItem > p(f) THEN
CONCAT(bestfitresult,AdjRSqr(CoefDet,NbItem,p(f)));
CONCAT(fit,f);
END;
END;
END;
END;
END;
bestf := 1;
bestresult := bestfitresult(1);
FOR f FROM 2 TO SIZE(fit) DO
CASE
IF criterion == 1 AND bestfitresult(f) < bestresult THEN
bestf := f;
bestresult := bestfitresult(f);
END;
IF criterion == 2 AND bestfitresult(f) > bestresult THEN
bestf := f;
bestresult := bestfitresult(f);
END;
END;
END;
S1(4) := fit(bestf);
Do2VStats(S1);
STARTVIEW(2,1);
END;
VIEW "Plot Criterion",PlotCriterion()
BEGIN
PRINT(fit);
D1 := fit;
PRINT(bestfitresult);
D2 := bestfitresult;
Statistics_1Var.H1(1) := "D1";
Statistics_1Var.H1(2) := "D2";
Statistics_1Var.H1(3) := 4;
STARTAPP("Statistics 1Var");
END;
VIEW "Autoscale",AUTOSCALE()
BEGIN
STARTVIEW(10,1);
END;
|