> lls<-function(x, y, h, grid, method=1,iker = 1)
+ { # SIMPLE BUT NAIVE IMPLEMENTATION of
+ # local constant or local linear with kernel symmetric beta kernel
+ # (1-u^2)^iker and bandwidth h, evaluated at "grid".
+ #method = 0, local linear fit;
+ # = 1, local constant fit
+ #iker is an integer.
+ #result: grid, est
+ ngrid<-length(grid)
+ res <- rep(0,ngrid)
+
+ for (j in 1:ngrid)
+ { w <- locw(x,grid[j],h) #Epanechnikov kernel
+ x0 <- x[ w > 0] #local data
+ y0 <- y[ w > 0]
+ w <- w[w > 0]
+ w <- w^iker
+ coef <- lsfit(x0-grid[j],y0,w)$coef
+ if(method==1)
+ res[j] <- lsfit(x0-grid[j],y0,w)$coef[1]
+ else
+ res[j] <- lsfit(x0-grid[j],y0,w,intercept=FALSE)$coef[1]
+ }
+ list(grid=grid,est=res)
+ }
>
>
> ep=function(t)
+ (3/4)*(1-t^2)*(1-t^2>=0)
> locw=function(T,t,h){
+ y=T-t;Kh=ep(y/h)/h;
+ s1=t(Kh)%*%y;s2=t(Kh)%*%(y^2);
+ return(Kh*(s2-s1*y)/sum(Kh*(s2-s1*y)));
+ }
>
|