# RProject8_4_density.r
require(graphics)
set.seed(0)
  x=ifelse(runif(100)<.5, rnorm(100) +5,rnorm(100))
  #  x=ifelse(runif(100)<.5, rgamma(100,shape=3,scale=2),rnorm(100))
  #  x=ifelse(runif(100)<.5, (rnorm(100) +5)^2,(rnorm(100)^2))
  
  
    par(mfcol=c(2,2))


x.density1<-density(x,bw="sj")
hist(x,nclass=50,probability=TRUE,
     main=paste("Density Estimate (bw='sj')",
                "\nN = ", as.character(length(x)),"  Bandwidth=", as.character(round(x.density1$bw,digits=3)),
               collapse=""))
lines(x.density1$x, x.density1$y, col="green")
rug(x)

x.density1<-density(x,bw="nrd0")

hist(x,nclass=50,probability=TRUE,
     main=paste("Density Estimate (bw='nrd0')",
                "\nN = ", as.character(length(x)),"  Bandwidth=", as.character(round(x.density1$bw,digits=3)),
                collapse=""))
lines(x.density1$x, x.density1$y, col="green")
rug(x)


x.density1<-density(x,bw="nrd0",adjust=.2)

hist(x,nclass=50,probability=TRUE,
     main=paste("Density Estimate (bw='nrd0 x .2')",
                "\nN = ", as.character(length(x)),"  Bandwidth=", as.character(round(x.density1$bw,digits=3)),
                collapse=""))
lines(x.density1$x, x.density1$y, col="green")
rug(x)

x.density1<-density(x,bw="nrd0",adjust=4.)

hist(x,nclass=50,probability=TRUE,
     main=paste("Density Estimate (bw='nrd0 x 4')",
                "\nN = ", as.character(length(x)),"  Bandwidth=", as.character(round(x.density1$bw,digits=3)),
                collapse=""))
lines(x.density1$x, x.density1$y, col="green")
rug(x)

### Alternate Kernels:
par(mfcol=c(1,1))
(kernels <- eval(formals(density.default)$kernel))
## [1] "gaussian"     "epanechnikov" "rectangular"  "triangular"  
## [5] "biweight"     "cosine"       "optcosine"
## show the kernels in the R parametrization
plot (density(0, bw = 1), xlab = "",
      main = "R's density() kernels with bw = 1")
for(i in 2:length(kernels))
  lines(density(0, bw = 1, kernel =  kernels[i]), col = i)
legend(1.5,.4, legend = kernels, col = seq(kernels),
       lty = 1, cex = .8, y.intersp = 1)

# 
stem(x)
## 
##   The decimal point is at the |
## 
##   -2 | 9
##   -0 | 5411109977665543311110
##    0 | 0002235678990123455789
##    2 | 02436788999
##    4 | 011112222456666789000011112233456777
##    6 | 13335664
args(stem)
## function (x, scale = 1, width = 80, atom = 1e-08) 
## NULL
#help(stem)
stem(x,scale=2)
## 
##   The decimal point is at the |
## 
##   -2 | 9
##   -1 | 541110
##   -0 | 9977665543311110
##    0 | 000223567899
##    1 | 0123455789
##    2 | 024
##    3 | 36788999
##    4 | 011112222456666789
##    5 | 000011112233456777
##    6 | 1333566
##    7 | 4
stem(x,scale=3)
## 
##   The decimal point is at the |
## 
##   -2 | 9
##   -2 | 
##   -1 | 5
##   -1 | 41110
##   -0 | 99776655
##   -0 | 43311110
##    0 | 000223
##    0 | 567899
##    1 | 01234
##    1 | 55789
##    2 | 024
##    2 | 
##    3 | 3
##    3 | 6788999
##    4 | 0111122224
##    4 | 56666789
##    5 | 0000111122334
##    5 | 56777
##    6 | 1333
##    6 | 566
##    7 | 4
boxplot(x)