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;