|
In the chapter about teachware we have seen a lot of menus which are used
to execute several programs. The command to achieve this is
selectitem
.
It allows us to select one or more items and continues a program as soon
as the user pressed the OK button:
selhead = "My headline"; sets the headline selitem = "item 1"|"item 2"|"item 3"; sets the items ; ; now we open a dialog box with the items and wait ; until the user presses OK ; sel = selectitem(selhead, selitem) sel
The return vector sel contains zeroes and ones; zero if the user has not selected the item and one if the user has selected the item.
Contents of sel [1,] 1 [2,] 0 [3,] 1
Let's now do a complex example: We choose between two univariate regression methods for a given data set. Then we plot the data and the regression estimate.
proc()=regression(x,y) selhead = "Choose regression method" selitem = "Linear regression"|"Nadaraya-Watson" disp = createdisplay(1,1) continue = 1 show (disp, 1, 1, x~y) while (continue<>0) sel = selectitem (selhead, selitem) if (sel[1]==1) l = grlinreg(x~y) show (disp, 1, 1, x~y, l) endif if (sel[2]==1) h = (max(x)-min(x))/20 w = sort(x~y) mh = regest(w) l = setmask(mh, "line") show (disp, 1, 1, x~y, l) endif continue = sum(sel)>0 endo endp library("plot") library("smoother") x = read("bostonh") regression(x[,13], x[,14])
Let's now discuss the quantlet regression in detail. First we define our headline and items. Then we plot the data set. Finally we build a loop which will be finished if continue equals zero.
In the loop we open first the selection box and
the user selects one of the two
methods. Thus we receive in sel either
#(1,0) in the case that the user selects Linear
regression, or #(0,1) in the case that the user selects
Nadaraya-Watson. The two
if
statements test whether sel[1] equals one
or if sel[2] equals one.
In the first case we compute a linear regression; in the second case we compute the Nadaraya-Watson regression estimator (see Smoothing Methods (6))
However, two possibilities are not handled well: If the user selects nothing (sel = #(0,0)) or if the user selects both (sel = #(1,1)). In the latter case, we would always get the Nadaraya-Watson estimator.
The case sel = #(0,0) can be handled by continue = sum(sel) which simply finishes our procedure. We add up all elements and compare if the sum is larger than zero. If we have selected one or more items, then the sum will be positive and continue will be true (=1). Otherwise continue will be false (=0).
Our last regression program is modified such that we can also handle the case sel = #(1,1) properly.
We know that the bandwidth is important for the
nonparametric Nadaraya-Watson estimator of the true
regression function
. In our example we have always
used
.
Many algorithms have been created to determine the ``right'' bandwidth
for a given data set. (This issue is addressed in detail in
Smoothing Methods (6).)
However, sometimes it turns out that the
human eye chooses better than any algorithm. Thus we use
readvalue
to get the bandwidth interactively from the user.
Let's do a simple example first:
item = "item 1"|"item 2"|"item 3" ; set the items def = 0|0|0 ; sets the default values ; ; now we open a dialog box with the items and ; wait until the user presses OK ; val = readvalue(item, def)
readvalue
has two input parameters: A text string item such that
the user knows what we are asking him and a default value def.
The output of
readvalue
is a vector of the same length as
def. Note that def can also be a text vector rather
than a numerical vector. In this case the output of
readvalue
is a text vector too. Note that you cannot mix
numerical and text items in def !
Let's now add
readvalue
to our regression program to allow
the user to choose the bandwidth interactively:
proc()=regression(x,y) selhead = "Choose regression method" selitem = "Linear regression"|"Nadaraya-Watson" disp = createdisplay(1,1) continue = 1 show (disp, 1, 1, x~y) h = (max(x)-min(x))/20 while (continue<>0) sel = selectitem (selhead, selitem) n = sum(sel) if (n>0) if (sel[2]==1) h = readvalue ("Bandwidth", h) endif disp = createdisplay(1, n) i = 1 if (sel[1]==1) l = grlinreg(x~y) show (disp, 1, i, x~y, l) i = i+1 endif if (sel[2]==1) w = sort(x~y) mh = regest(w, h) l = setmask(mh, "line") show (disp, 1, i, x~y, l) i = i+1 endif endif continue = (n>0) endo endp library("plot") library("smoother") x = read("bostonh") regression(x[,13], x[,14])