You are running into problems because you define a variable over an assigned set (j) that is a subset of another subset, which is not possible.
You can correct this by defining the assigned set as a subset of the main set (t); same goes for i.
You can’t substract a set element for lagging because the subtracted set element is treated as a string. Here you can use ord(j) to get things running.
To use the subset in the sum, you can use the dollar sign when summing over the main set t (e.g. sum(t$j(t), …).
Here is the complete code:
sets
t total forecasting period /1,2,3,4/
i(t) index of the forecasting period /4/
j(t) order
;
j(t) = not i(t);
parameters
Y(t) historical value
/
1 2
2 4
3 6
4 8
/
;
variables
F(t) forecasted value
b(t) coefficent
e(t) error
etotal total error
;
equations
eq1(i), eq2(i), eq3;
eq1(i).. F(i) =e= sum(t$j(t),b(t)*Y(i-ord(t))) ;
eq2(i).. e(i) =e= F(i) - Y(i) ;
eq3.. etotal =e= sum(i,e(i)) ;
model timereg as /all/
option optcr = 0 ;
solve timereg using lp minimizing etotal ;
display t, i, j, Y
$exit
sets
t total forecasting period /1,2,3,4/
fp(t) forecast periods /2,4/
;
parameters
Y(t) historical value
/
1 2
2 4
3 6
4 8
/,
index(t);
index("2") = 2;
index("4") = 4;
variables
F(t) forecasted value
B(t) coefficent
E(t) error
ETOTAL total error
;
equations
eq1(fp),
eq2(fp),
eq3;
eq1(fp).. F(fp) =e= sum(t$(ord(t) < index(fp)), B(t) * Y(t));
eq2(fp).. E(fp) =e= F(fp) - Y(fp) ;
eq3.. ETOTAL =e= sum(fp, E(fp)) ;
model timereg as /all/
option optcr = 0 ;
solve timereg using lp minimizing etotal ;
display t, Y;
By the way: it might be a good idea to write variables upper case and parameters lower case, so you or somebody else have to check if something is a parameter of a variable by scrolling back in the file.