2. Quantlet Examples

Throughout the Tutorials we use XploRe example code which can be directly typed into the command line. We have seen this in the previous sections when calculating the mean and creating the scatter plot for the pullover data.

Since it can be cumbersome to type many lines of code, we summarized instructions that belong together into quantlet files. These quantlet files are either displayed within the text, as e.g. 1064start01.xpl or printed at the end of a sequence of instructions:


  x=read("pullover")   ; reads data

  x=x[,2|1]

  library("plot")

  plot(x)              ; shows scatter plot

1068start02.xpl

You can find all example quantlets in the examples directory of your XploRe installation. This means, if XploRe is installed in C:\XploRe, you can find the examples in C:\XploRe\examples.

To load an example quantlet, use the Open item from the Program menu. Change the directory to examples and double-click on the appropriate file. This opens an editor window containing the example code. We execute the quantlet by clicking the Execute item in the menu bar or by entering <Alt E>.

Quantlets contain one XploRe instruction in each line. Additionally, it is possible to add comments. For example, in


  x = 1   ;  assign the value 1 to x

  y = 2   // assign the value 2 to y

everything beyond the ; and // is a comment. You can also use /* and */ to mark the beginning and the end of several lines of comments.

The following subsections intend to give you an impression of the capabilities of XploRe. We will present some of the statistical methods that are explained in detail later on.


2.1 Summary Statistics

XploRe has several functions to print summary statistics of all columns of a data file. The following example uses the function 1103 summarize from the stats library:


  library("stats")

  x=read("pullover")

  library("stats")

  setenv("outputstringformat","%s")

summarize(x, "sales"|"price"|"ads"|"assistance")

1109start03.xpl

This returns the following table in the output window:

  Contents of summ

  [1,]             

  [2,]            Minimum   Maximum    Mean   Median   Std.Error

  [3,]            -----------------------------------------------

  [4,] sales           97       230   172.7      172      33.948

  [5,] price           80       125   104.6       99      15.629

  [6,] ads              0       200     104      105      53.996

  [7,] assistance      71       111    93.8       93      14.038

  [8,]

You will learn more about summary statistics in the Descriptive Statistics Tutorial.


2.2 Histograms

A histogram visualizes univariate data. Hence, we produce separate histograms for the variables (columns) of a data matrix. The following example shows the histogram for the sixth column (diagonal length) of the Swiss bank notes data stored in the file bank2.dat (see Data Sets).


  library("plot")

  y=read("bank2")

  y=y[,6]

  library("plot")

  setsize(640,480)    ; sets display size

  plothist(y)         ; plots histogram

1123start04.xpl

Figure 3 shows the resulting histogram. We recognize two modes in the distribution of the data. This indicates two clusters.

Figure 3: Histogram.
\includegraphics[scale=0.6]{apphist.ps}


2.3 2D Density Estimation

The method of kernel density estimation allows us to estimate the distribution of data in a very flexible way. We introduce this topic in the Smoothing Methods Tutorial. Here we compute the density estimate for the joint density of the upper frame length and the diagonal length (fifth and sixth variable) of the bank2 data (see Data Sets).


  library("smoother")

  library("plot")

  x = read("bank2")

  x=x[,5:6] 

  library("smoother")

  library("plot")

  fh = denxestp(x)             ; density estimation

  fh = setmask(fh,"surface","blue")

  setsize(640,480)

  axesoff()

  cu = grcube(fh)              ; box

  plot(cu.box,cu.x,cu.y, fh)   ; plot box and fh

  setgopt(plotdisplay,1,1,"title","2D Density Estimate")

  axeson()

1138start05.xpl

This example computes the density estimate on a grid of points. It returns a graphical display containing the estimated function. To obtain the three-dimensional surface in Figure 4, you need to rotate the estimate by using the cursor keys. The vertical axis shows the estimated density while the horizontal axes show the upper frame (to the right) and the diagonal length (to the left), respectively.

Figure 4: Two-dimensional density estimate.
\includegraphics[scale=0.6]{app2ds.ps}

The density estimate confirms our impression from Subsection 2.2 that the data set features two clusters.


2.4 Interactive Kernel Regression

XploRe is particularly suited for interactive use. The following quantlet computes a nonparametric regression smoother and asks interactively for the bandwidth.


  proc()=interactive(x)

    error(cols(x)!=2,"x has more than 2 columns!")

    x=setmask(x,"red","cross")

    h=(max(x[,1])-min(x[,1]))/5

    stop=0

    while (stop!=1)

      mh=setmask(regxest(x,h),"line")

      plot(x,mh)

      hnew=readvalue("Bandwidth h (0 to stop)",h)

      while (hnew<0)

        hnew=readvalue("Bandwidth h (0 to stop)",h)

      endo

      if (hnew!=0)

        h=hnew

      else

        stop=1

      endif

    endo

  endp

  

  library("plot")

  library("smoother")

  x=read("nicfoo")

  interactive(x)

1151start06.xpl

When the quantlet is executed, it first shows a graphical display with the nonparametric regression curve (solid line) and the data (red crosses). Next, a dialog box opens which allows us to modify the currently used bandwidth parameter. The quantlet stops, if 0 (zero) has been entered.

1158

1162

Nonparametric smoothing is introduced in the Smoothing Methods Tutorial. If you want to program interactive quantlets yourself, we recommend that you study the Quantlets and Quantlibs Tutorial.



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