Last data update: 2014.03.03

R: Bivariate kernel density estimation for rounded data
dbivrR Documentation

Bivariate kernel density estimation for rounded data

Description

Bivariate kernel density estimation for rounded data

Usage

dbivr(xrounded, roundvalue, burnin = 2, samples = 5, adaptive = FALSE,
  gridsize = 1000)

Arguments

xrounded

rounded values from which to estimate bivariate density, matrix with 2 columns (x,y)

roundvalue

rounding value (side length of square in that the true value lies around the rounded one)

burnin

burn-in sample size

samples

sampling iteration size

adaptive

set to TRUE for adaptive bandwidth

gridsize

number of evaluation grid points

Value

The function returns a list object with the following objects (besides all input objects):

Mestimates

kde object containing the corrected density estimate

gridx

Vector Grid on which density is evaluated (x)

gridy

Vector Grid on which density is evaluated (y)

resultDensity

Array with Estimated Density for each iteration

resultX

Matrix of true latent values X estimates

delaigle

Matrix of Delaigle estimator estimates

Examples

# Create Mu and Sigma  -----------------------------------------------------------
mu1 <- c(0, 0)
mu2 <- c(5, 3)
mu3 <- c(-4, 1)
Sigma1 <- matrix(c(4, 3, 3, 4), 2, 2)
Sigma2 <- matrix(c(3, 0.5, 0.5, 1), 2, 2)
Sigma3 <- matrix(c(5, 4, 4, 6), 2, 2)
# Mixed Normal Distribution -------------------------------------------------------
mus <- rbind(mu1, mu2, mu3)
Sigmas <- rbind(Sigma1, Sigma2, Sigma3)
props <- c(1/3, 1/3, 1/3)
## Not run: xtrue=rmvnorm.mixt(n=1000, mus=mus, Sigmas=Sigmas, props=props)
roundvalue=2
xrounded=plyr::round_any(xtrue,roundvalue)
est <- dbivr(xrounded,roundvalue=roundvalue,burnin=5,samples=10)

#Plot corrected and Naive distribution
plot(est,trueX=xtrue)
#for comparison: plot true density
 dens=dmvnorm.mixt(x=expand.grid(est$Mestimates$eval.points[[1]],est$Mestimates$eval.points[[2]]),
  mus=mus, Sigmas=Sigmas, props=props)
 dens=matrix(dens,nrow=length(est$gridx),ncol=length(est$gridy))
 contour(dens,x=est$Mestimates$eval.points[[1]],y=est$Mestimates$eval.points[[2]],
    xlim=c(min(est$gridx),max(est$gridx)),ylim=c(min(est$gridy),max(est$gridy)),main="True Density")
## End(Not run)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(Kernelheaping)
Loading required package: MASS
Loading required package: ks
Loading required package: KernSmooth
KernSmooth 2.23 loaded
Copyright M. P. Wand 1997-2009
Loading required package: misc3d
Loading required package: mvtnorm
Loading required package: rgl
Loading required package: sparr
Loading required package: spatstat
Loading required package: nlme
Loading required package: rpart

spatstat 1.45-2       (nickname: 'Caretaker Mode') 
For an introduction to spatstat, type 'beginner' 


Attaching package: 'spatstat'

The following object is masked from 'package:MASS':

    area



Welcome to 'sparr': SPAtial Relative Risk (v0.3-8)
T.M. Davies, M.L. Hazelton & J.C. Marshall
-type 'help("sparr")' for details
-type 'citation("sparr")' for how to cite use of this package

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Kernelheaping/dbivr.Rd_%03d_medium.png", width=480, height=480)
> ### Name: dbivr
> ### Title: Bivariate kernel density estimation for rounded data
> ### Aliases: dbivr
> 
> ### ** Examples
> 
> # Create Mu and Sigma  -----------------------------------------------------------
> mu1 <- c(0, 0)
> mu2 <- c(5, 3)
> mu3 <- c(-4, 1)
> Sigma1 <- matrix(c(4, 3, 3, 4), 2, 2)
> Sigma2 <- matrix(c(3, 0.5, 0.5, 1), 2, 2)
> Sigma3 <- matrix(c(5, 4, 4, 6), 2, 2)
> # Mixed Normal Distribution -------------------------------------------------------
> mus <- rbind(mu1, mu2, mu3)
> Sigmas <- rbind(Sigma1, Sigma2, Sigma3)
> props <- c(1/3, 1/3, 1/3)
> ## Not run: 
> ##D xtrue=rmvnorm.mixt(n=1000, mus=mus, Sigmas=Sigmas, props=props)
> ##D roundvalue=2
> ##D xrounded=plyr::round_any(xtrue,roundvalue)
> ##D est <- dbivr(xrounded,roundvalue=roundvalue,burnin=5,samples=10)
> ##D 
> ##D #Plot corrected and Naive distribution
> ##D plot(est,trueX=xtrue)
> ##D #for comparison: plot true density
> ##D  dens=dmvnorm.mixt(x=expand.grid(est$Mestimates$eval.points[[1]],est$Mestimates$eval.points[[2]]),
> ##D   mus=mus, Sigmas=Sigmas, props=props)
> ##D  dens=matrix(dens,nrow=length(est$gridx),ncol=length(est$gridy))
> ##D  contour(dens,x=est$Mestimates$eval.points[[1]],y=est$Mestimates$eval.points[[2]],
> ##D     xlim=c(min(est$gridx),max(est$gridx)),ylim=c(min(est$gridy),max(est$gridy)),main="True Density")
> ## End(Not run)
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>