reg_1derivWL {npregderiv} | R Documentation |
Estimating the first derivative of a regression function by the method of Wang and Lin (2015)
Description
The design data xdat
must be increasing and evenly spaced. The first derivative estimate is computed at u
that must be one of the design points xdat
from the interior region. The interior region excludes the first k
points with the smallest xdat
values (left boundary) and the last k
points with the largest xdat
values (right boundary).
Usage
reg_1derivWL(xdat, ydat, k, u)
Arguments
xdat |
numerical vector of the increasing and evenly spaced design points |
ydat |
numerical vector of the corresponding data points |
k |
integer value of the smoothing parameter |
u |
an estimation point (scalar) that must be one of the |
Details
The method's smoothing parameter k
shows how many data points are used from each side of an estimation point u
to compute the first derivative estimate at that point. Choose k<n/4
, where n
is the sample size. The value of k=0.1n
can be taken as a starting point.
Value
The computed first derivative estimate (scalar) at the point u
.
References
Wang, W.W., Lin, L. (2015) http://www.jmlr.org/papers/v16/wang15b.html “Derivative Estimation Based on Difference Sequence via Locally Weighted Least Squares Regression”, Journal of Machine Learning Research, 16, 2617-2641.
See Also
Examples
# EXAMPLE 1: Toy example
xdata=1:100
ydata=xdata^2
reg_1derivWL(xdata,ydata,10,30)
# EXAMPLE 2 (simulated data).
m=function(x)
sin(2*pi*x)+log(6+5*x)
m1=function(x) # first derivative of m
2*pi*cos(2*pi*x)+5/(6+5*x)
N=300 # sample size
xi=2*(1:N/N)-1 # equidistant design points
sigma=0.25 # noise level used in the article
yi=m(xi)+rnorm(N,sd=sigma) # generating data
K=0.1*N # selected value of the smoothing parameter k
x_inter=xi[(K+1):(N-K)] # interior estimation region
N_inter=length(x_inter) # number of points where the second derivative estimate is computed
M1=array(0,N_inter) # Vector of estimated second derivatives at the points x_inter
for (i in 1:N_inter)
M1[i]=reg_1derivWL(xi,yi,K,x_inter[i])
op=par(no.readonly=TRUE)
par(mfrow=c(1,2))
plot(xi,yi,pch=20,cex=1.7,main="Regression function and generated data",xlab="",ylab="",
cex.axis=1.8,cex.main=1.5)
lines(xi,m(xi),'l',col="red",lwd=2)
title(xlab="argument",ylab="regression function",line=2.5,cex.lab=1.8)
legend(0.65,-0.4,lty="solid",col="red",lwd=2,legend="true regression function",bty="n",
cex=1.45,seg.len=0.5)
plot(x_inter,M1,'l',lwd=2,main="Actual and estimated first derivative",xlab="",ylab="",
cex.axis=1.8,cex.main=1.5)
lines(x_inter,m1(x_inter),'l',lwd=2,col="red")
title(xlab="argument",ylab="second derivative",line=2.5,cex.lab=1.8)
legend(0.6,-10,lty=c("solid","solid"),col=c("red","black"),lwd=c(2,2),
legend=c("true first derivative", "estimate"),bty="n",cex=1.45,seg.len=0.5)
par(op)
# EXAMPLE 3: Estimating the first derivative of the deflection function for the data on
# deflection after film deposition scanned at 0 degrees.
# See Savchuk O., and Volinsky, A. (2020) for the experiment's description.
xdesign=wafer40$x_dat
ydata=wafer40$y_after_0
n_data=length(xdesign) # sample size (original)
n_new=1034 # reducing the number of points where the second
# derivative is estimated.
K=ceiling(0.1*n_data) # value of the smoothing parameter
index=(K+1)+0:(n_new-1)*6 # cutting about 10% of points from each side and
# reducing the number of points where the second
# derivative is estimated
x_inter=xdesign[index] # the values of argument where the second
# derivative is to be estimated
y_inter=ydata[index]
Der1=array(0,n_new)
for (i in 1:n_new)
Der1[i]=reg_1derivWL(xdesign,ydata,K,x_inter[i])
op=par(no.readonly=TRUE)
par(mfrow=c(1,2))
plot(xdesign*1000,ydata*10^6,pch=20,main="Deflection AFTER film deposition. Angle: 0 degrees.",
xlab="",ylab="",cex.axis=1.8,cex.main=1.5)
title(ylab=expression(paste("deflection, ",mu,"m")),xlab="position, mm",line=2.25,cex.lab=1.8)
plot(x_inter*1000,Der1,'l',lwd=2,
main="1-st derivative AFTER film deposition. Angle: 0 degrees.",
xlab="",ylab="",cex.axis=1.8, cex.main=1.5)
title(ylab="first derivative of deflection", xlab="position, mm", line=2.5, cex.lab=1.8)
par(op)