3. User Interaction


1737 selectitem (headline, items)
allows the user to choose one or more items before continuing the program
values = 1740 readvalue (valuenames, defaults)
allows the user to enter one or more values before continuing the program

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 1743 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
1747quant16.xpl

The resulting selection box is shown in Figure 1.

Figure: The selectitem example.

1761

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])
1771quant17.xpl

A screenshot of this procedure is shown in Figure 2.

Figure: A selectitem in action.

1785

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 1794 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 the Smoothing Methods Tutorial)

\begin{displaymath}\widehat m_{h}(x)=
\frac{\sum\limits_{i=1}^n K\left(\frac{\di...
...^n K\left(\frac{\displaystyle x_{i}-x}{\displaystyle h}\right)}\end{displaymath}

and plot it.

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 h is important for the nonparametric Nadaraya-Watson estimator of the true regression function m. In our example we have always used $h = 0.05 (\max x_i - \min x_i)$. Many algorithms have been created to determine the ``right'' bandwidth for a given data set. (This issue is addressed in detail in the Smoothing Methods Tutorial.) However, sometimes it turns out that the human eye chooses better than any algorithm. Thus we use 1801 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)
1805quant18.xpl

The resulting 1810 readvalue box is shown in Figure 3

Figure: The readvalue example.

1822

1831 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 1834 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 1837 readvalue is a text vector too. Note that you cannot mix numerical and text items in def !

Let's now add 1840 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])
1844quant19.xpl

Figure 4 shows this quantlet in action.

Figure: readvalue in action.

1851



Method and Data Technologies   MD*TECH Method and Data Technologies
  http://www.mdtech.de  mdtech@mdtech.de