# Chapter 17: Multivariate adaptive histograms. Code

## Greedy histograms

We apply function ``sim.data'', to generate a sample from our running multimodal example.

```dendat<-sim.data(n=600,seed=5,type="mulmodII")
```

We calculate the estimate ``eva'', whose partition has cardinality 20. The estimate is given as an evaluation tree, which extends the class of piecewise constant functions.

```eva<-eval.greedy(dendat,leaf=16)

dp<-draw.pcf(eva)
persp(dp\$x,dp\$y,dp\$z,theta=-20,phi=30)
```

Code of Chapter 2 gives the commands to make a perspective plot and a contour plot from a piecewise constant function. In order to use ``persp'' to draw histograms it is better to use higher resolution:

```dp<-draw.pcf(eva,pnum=c(60,60))
persp(dp\$x,dp\$y,dp\$z,theta=-20,phi=30)

contour(dp\$x,dp\$y,dp\$z,nlevels=25)
```

We use function ``partition'' and ``plotparti'' to plot the partition of the histogram.

```pa<-partition(eva)
plotparti(pa,dendat=dendat)
```

## CART histograms

We apply function ``sim.data'' to generate a sample from our running multimodal example.

```dendat<-sim.data(n=600,seed=5,type="mulmodII")
```

We caculate the CART-histogram ``eva''. The function ``eval.cart'' returns an evaluation tree, which extends the class of piecewise constant functions. We would like to have an histogram whose partition has 15 rectangles. It might not be possible to obtain a histogram of this size, since a partition of size 15 might not exist in the sequence of pruned partitions. Function ``eval.cart'' returns an histogram whose partition has size as close to 15 as possible.

```eva<-eval.cart(dendat,leaf=15)

dp<-draw.pcf(eva)
persp(dp\$x,dp\$y,dp\$z,theta=-20,phi=30)
```

Code of Chapter 2 gives the commands to make a perspective plot and a contour plot from a piecewise constant function. We may change the minimum number of observations in cells.

```eva<-eval.cart(dendat,leaf=15,minobs=2)

dp<-draw.pcf(eva)
persp(dp\$x,dp\$y,dp\$z,theta=-20,phi=30)
```

To see the sizes of the partitions in the sequence of the pruned partitions we may calculate the histogram using the following steps.

```over<-densplit(dendat) # overfitting histogram
treeseq<-prune(over)   # the sequence of partitions
treeseq\$leafs          # print the sizes of the partitions
```

After looking at the sizes of the partitions we choose a suitable size and calculate the corresponding histogram with function ``eval.pick''.

```leafnum<-treeseq\$leafs
eva<-eval.pick(treeseq,leafnum)

dp<-draw.pcf(eva)
persp(dp\$x,dp\$y,dp\$z,theta=-20,phi=30)
```

## Bootstrap aggregation

We apply function ``sim.data'' to generate a sample from our running multimodal example.

```dendat<-sim.data(n=600,seed=5,type="mulmodII")
```

We use function ``eval.bagg'' to calculate a bootstrap aggregated estimator.

```leaf<-7     # number of leaves in the histograms
seed<-1     # seed for choosing bootstrap samples
sample="baggworpl" # without-replacement bootstrap
prune="on"  # we use CART-histograms
B<-50       # the number of histograms in the average

eva<-eval.bagg(dendat,B,leaf,seed=seed,sample=sample,prune=prune)

dp<-draw.pcf(eva)
persp(dp\$x,dp\$y,dp\$z,theta=-20,phi=30)
```

Code of Chapter 2 gives the commands to make a perspective plot and a contour plot from a piecewise constant function. Code of Chapter 4 and Code of Chapter 5 give commands for using level set trees and shape trees to visualize kernel estimates.