So I tried to build the automated grouping. The goal is to select the grouping setting that has the lowest variance.
In other word, I want to find x and y for the following, x,y are natural number,
GROUP 1: 1997 - x
GROUP 2: x+1 - y
GROUP 3: y+1 - 1994
such that the SUM of (variance(Response
in Group1),variance(Response
in Group2),variance(Response
in Group3)) are minimize.
data maindat;
input Year Response ;
datalines;
1994 -4.300511714
1994 -9.646920963
1994 -15.86956805
1993 -16.14857235
1993 -13.05797186
1993 -13.80941206
1992 -3.521394503
1992 -1.102526302
1992 -0.137573583
1992 2.669238665
1992 -9.540489193
1992 -19.27474303
1992 -3.527077011
1991 1.676464068
1991 -2.238822314
1991 4.663079037
1991 -5.346920963
1990 -8.543723186
1990 0.507460641
1990 0.995302284
1990 0.464194011
1989 4.728791571
1989 5.578685423
1988 2.771297564
1988 7.109159247
1987 15.96059456
1987 2.985292226
1986 -4.301136971
1985 5.854674875
1985 5.797294021
1984 4.393329025
1983 -6.622580905
1982 0.268500302
1977 12.23062252
;
run;
My idea is that I'll have 2 do loop (nested)
1st do loop (1st iteration): Group 1 1977 - 1977 1977 - 1977 1977 - 1977 … 1977 - 1977
2nd do loop: Group 2 1978 - 1978 1978 - 1979 1978 - 1980 … 1978 - 1993
Else: Group 3 1979 - 1994 1980 - 1994 1981 - 1994 … 1994 - 1994
1st do loop (2nd iteration): Group 1 1977 - 1978 1977 - 1978 1977 - 1978 … 1977 - 1978
2nd do loop: Group 2 1979 - 1979 1979 - 1980 1979 - 1981 … 1979 - 1993
Else Group 3 1980 - 1994 1981 - 1994 1982 - 1994 … 1994 - 1994
...
1st do loop (n-1th iteration) Group 1 1977 - 1991 1977 - 1991
2nd do loop: Group 2 1992 - 1992 1992 - 1993
Else Group 3 1993 - 1994 1994 - 1994
1st do loop (nth iteration) Group 1 1977 - 1992
2nd do loop: Group 2 1993 - 1993
Else Group 3 1994 - 1994
Then I'll just select the grouping setting that provide the smallest of the sum of the variance(response within the group) of 3 groups.
Here is a manual, exhaustive approach. This should solve your problem as stated, but is not a good way of approaching the problem if you want more groups, or have larger data.
I'm sure there is a more sensible approach using one of the procs but nothing springs to mind immediately.
SRSwift's answer may be the best one for the problem you provided. The difficulty here with the standard algorithm is that you don't seem to have a single local/global minimum of your function (variance of response), but have multiple local minima that cause it to not work terribly well with the relatively low flexibility it has with the data density to adjust. This sort of thing is easy to work around if you have a lot of 'years', where you can instead of skipping around one year at a time skip around by five years or ten or whatever (to avoid local minima); but with only a couple dozen years that is impractical.
This is a core machine learning application, the ability to cluster nodes, and has a number of solutions. Your particular one seems to appeal to the most simple, one I learned in a course a few years ago and find very easy to implement if you think of it in a few pieces.
Then you call minim_f and modif_f alternately; you call minim_f, grab its value, call modif_f with one set of parameters; then check minim_f and see if it's better. If so, keep going with that direction. If not, revert back to the original values from the previous iteration and try a different modification in modif_f. Keep going until you've found the local minimum, which is hopefully a global minimum.
The exact mechanics of this vary; in particular, you might adjust one or more centroids at once, and you have to figure out the right way to keep adjusting until no more adjustments will work.
I wrote a small example of this for your data; it does come to the same answer as SRSwift's, although the proc means calculated variance is not the same as that from SRSwift's program. I am not a statistician and won't say which is right, but they clearly work sufficiently similarly that it's not important. Mine is a very simple implementation of this and would benefit greatly from improvement, but hopefully it explains the basic concepts.