if (FALSE) {
# EXAMPLE 1 (INTERFACE=FORMULA): For this example, we load Giovanni
# Baiocchi's Italian GDP panel (see Italy for details), then create a
# data frame in which year is an ordered factor, GDP is continuous.
data("Italy")
attach(Italy)
data <- data.frame(ordered(year), gdp)
# We compute bandwidths for the kernel density estimator using the
# normal-reference rule-of-thumb. Otherwise, we use the defaults (second
# order Gaussian kernel, fixed bandwidths). Note that the bandwidth
# object you compute inherits all properties of the estimator (kernel
# type, kernel order, estimation method) and can be fed directly into
# the plotting utility plot() or into the npudens() function.
bw <- npudensbw(formula=~ordered(year)+gdp, bwmethod="normal-reference")
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Next, specify a value for the bandwidths manually (0.5 for the first
# variable, 1.0 for the second)...
bw <- npudensbw(formula=~ordered(year)+gdp, bws=c(0.5, 1.0),
bandwidth.compute=FALSE)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Next, if you wanted to use the 1.06 sigma n^{-1/(2p+q)} rule-of-thumb
# for the bandwidth for the continuous variable and, say, no smoothing
# for the discrete variable, you would use the bwscaling=TRUE argument
# and feed in the values 0 for the first variable (year) and 1.06 for
# the second (gdp). Note that in the printout it reports the `scale
# factors' rather than the `bandwidth' as reported in some of the
# previous examples.
bw <- npudensbw(formula=~ordered(year)+gdp, bws=c(0, 1.06),
bwscaling=TRUE,
bandwidth.compute=FALSE)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# If you wished to use, say, an eighth order Epanechnikov kernel for the
# continuous variables and specify your own bandwidths, you could do
# that as follows.
bw <- npudensbw(formula=~ordered(year)+gdp, bws=c(0.5, 1.0),
bandwidth.compute=FALSE,
ckertype="epanechnikov",
ckerorder=8)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# If you preferred, say, nearest-neighbor bandwidths and a generalized
# kernel estimator for the continuous variable, you would use the
# bwtype="generalized_nn" argument.
bw <- npudensbw(formula=~ordered(year)+gdp, bwtype = "generalized_nn")
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Next, compute bandwidths using likelihood cross-validation, fixed
# bandwidths, and a second order Gaussian kernel for the continuous
# variable (default). Note - this may take a few minutes depending on
# the speed of your computer.
bw <- npudensbw(formula=~ordered(year)+gdp)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Finally, if you wish to use initial values for numerical search, you
# can either provide a vector of bandwidths as in bws=c(...) or a
# bandwidth object from a previous run, as in
bw <- npudensbw(formula=~ordered(year)+gdp, bws=c(1, 1))
summary(bw)
detach(Italy)
# EXAMPLE 1 (INTERFACE=DATA FRAME): For this example, we load Giovanni
# Baiocchi's Italian GDP panel (see Italy for details), then create a
# data frame in which year is an ordered factor, GDP is continuous.
data("Italy")
attach(Italy)
data <- data.frame(ordered(year), gdp)
# We compute bandwidths for the kernel density estimator using the
# normal-reference rule-of-thumb. Otherwise, we use the defaults (second
# order Gaussian kernel, fixed bandwidths). Note that the bandwidth
# object you compute inherits all properties of the estimator (kernel
# type, kernel order, estimation method) and can be fed directly into
# the plotting utility plot() or into the npudens() function.
bw <- npudensbw(dat=data, bwmethod="normal-reference")
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Next, specify a value for the bandwidths manually (0.5 for the first
# variable, 1.0 for the second)...
bw <- npudensbw(dat=data, bws=c(0.5, 1.0), bandwidth.compute=FALSE)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Next, if you wanted to use the 1.06 sigma n^{-1/(2p+q)} rule-of-thumb
# for the bandwidth for the continuous variable and, say, no smoothing
# for the discrete variable, you would use the bwscaling=TRUE argument
# and feed in the values 0 for the first variable (year) and 1.06 for
# the second (gdp). Note that in the printout it reports the `scale
# factors' rather than the `bandwidth' as reported in some of the
# previous examples.
bw <- npudensbw(dat=data, bws=c(0, 1.06),
bwscaling=TRUE,
bandwidth.compute=FALSE)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# If you wished to use, say, an eighth order Epanechnikov kernel for the
# continuous variables and specify your own bandwidths, you could do
# that as follows:
bw <- npudensbw(dat=data, bws=c(0.5, 1.0),
bandwidth.compute=FALSE,
ckertype="epanechnikov",
ckerorder=8)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# If you preferred, say, nearest-neighbor bandwidths and a generalized
# kernel estimator for the continuous variable, you would use the
# bwtype="generalized_nn" argument.
bw <- npudensbw(dat=data, bwtype = "generalized_nn")
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Next, compute bandwidths using likelihood cross-validation, fixed
# bandwidths, and a second order Gaussian kernel for the continuous
# variable (default). Note - this may take a few minutes depending on
# the speed of your computer.
bw <- npudensbw(dat=data)
summary(bw)
# Sleep for 5 seconds so that we can examine the output...
Sys.sleep(5)
# Finally, if you wish to use initial values for numerical search, you
# can either provide a vector of bandwidths as in bws=c(...) or a
# bandwidth object from a previous run, as in
bw <- npudensbw(dat=data, bws=c(1, 1))
summary(bw)
detach(Italy)
}
Run the code above in your browser using DataLab