Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: kdensity with user defined kernel functions


From   "Ben Jann" <[email protected]>
To   <[email protected]>
Subject   st: RE: kdensity with user defined kernel functions
Date   Fri, 3 Feb 2006 23:31:26 +0100

The kernel functions used by -kdens- (see -ssc describe kdens-) 
are provided by Mata functions such as -kernel_biweight()- or 
-kernel_epan2()-. See

 . viewsource kernels.mata

for examples (after -kdens- has been installed). It would be 
relatively easy to define one's own Mata kernel functions. 
However, changes to the code of -kdens- would be required to be 
able to use them because the list of available kernel functions 
is hard-coded into -kdens-.

However, there is a backdoor (use with care): Simply re-define 
an existing kernel function. For example, the epan2 kernel is 
the standard kernel used by -kdens-. We could change epan2 to be 
the rectangular kernel as follows: 

====example.do====
clear

mata:
// the kernel
real matrix kernel_epan2(real matrix x)
{
	return(.5*(abs(x):<1))
}
// kernel "roughness"
real matrix kernel_I_epan2(| real scalar r, real matrix x)
{
	if (r==2 & args()==1) return(.5) 
	else _error("kernel integral not available")
}
// canonical bandwidth of kernel
real scalar kernel_del0_epan2() return((9/2)^.2)
end

sysuse auto
kdens price, ci
==================

Note that actually three functions have been defined. This is 
because -kdens- also requires information on the kernel 
"roughness" and the canonical bandwidth of the kernel. The 
roughness is needed for approximate variance estimation and the 
canonical bandwidth is used for the automatic bandwidth scaling.
[Furthermore note that -kernel_I_...()- usually contains much 
more stuff required for kernel density estimation for bounded 
variables.]

Let me know if you need help.

ben

> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of D'Exelle Ben
> Sent: Friday, February 03, 2006 12:41 PM
> To: [email protected]
> Subject: st: kdensity with user defined kernel functions
> 
> Hi all,
> 
> I need to make a kernel density estimation with a kernel function that
is
> not supported by the kdensity command. Is there a way to define your
own
> specific kernel function within the kdensity command? If this is not
> possible within the kdensity command, is there another way to make
kernel
> estimations with user-defined kernel functions?
> 
> Thank you for any reactions,
> 
> Ben D'Exelle
> 
> 
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index