estimate_density_d_logs: Estimate the density, its derivatives, and the first and...

Description Usage Arguments Value Examples

View source: R/estimate_density.R

Description

Wrapper around estimate_density_sorted to calculate the first and second derivative of the log of the density. The function estimates the density at all unique points in the input data. It can be useful to estimate the density outside of the treatment effect functions if one computes both the eif and the waq estimator to avoid estimating the density twice, or if one wishes to inspect the density itself.

Usage

1

Arguments

X

numeric vector, random sample from the distribution for which the density should be estimated.

...

additional arguments passed to estimate_density_sorted. DO NOT DISABLE ESTIMATION OF DERIVATIVES.

Value

a list with four elements:

xf

numerical vector, the points where the density was estimated (sorted unique points in X)

f

matrix with number of rows equal to length(xf) and three columns for the density, its first derivative, and its second derivative

d1_logf

(unless estDerivs is set to FALSE) the first derivative of the log density, calculated as f[,2]/f[,1]

d2_logf

(unless estDerivs is set to FALSE) the second derivative of the log density, calculated as (f[,1]*f[,3] - f[,2]^2)/f[,1]^2

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# draw a random sample with additive treatment effect
X <- rexp(n=1000, rate=2)
Y <- 0.5 + rexp(n=200, rate=2)
# estimate the density and its derivatives
dens <- estimate_density_d_logs(X)
# pass as argument to avoid computing density inside estimation functions
eif_additive(X,Y,xf=dens$xf,d1_logf=dens$d1_logf,d2_logf=dens$d2_logf)
waq(X,Y,xf=dens$xf,d2_logf=dens$d2_logf)
# calculate and inspect the weights used by waq
w_waq <- approx(x=dens$xf,y=dens$d2_logf,xout=sort(X),rule=2)$y
# normalize weights and get correct sign
w_waq <- w_waq/sum(w_waq)
# plot the weights
plot(sort(X),w_waq)
# plot the weights against quantiles
plot(seq(X)/length(X),w_waq)

michaelpollmann/parTreat documentation built on Dec. 21, 2021, 5:58 p.m.