bls<-function(x0,x,y){ x0%*%solve(t(x)%*%x,t(x)%*%y) } knn<-function(x0,x,y,k){ x=as.matrix(x) p=dim(x)[2] n=dim(x)[1] dis=rep(0,n) for(i in 1:p){ dis= (x0[i]-x[,i])^2+dis } ind=order(dis)[1:k] mean(y[ind]) } loess0<-function(x0,x,y,kern,lam){ x=as.matrix(x) p=dim(x)[2] n=dim(x)[1] dis=rep(0,n) for(i in 1:p){ dis= (x0[i]-x[,i])^2+dis } ##My code is below w = kern(dis,lam) return (sum(w*y)/sum(w)) ##Your code here } loess1<-function(x0,x,y,kern,lam){ x=as.matrix(x) p=dim(x)[2] n=dim(x)[1] dis=rep(0,n) for(i in 1:p){ dis= (x0[i]-x[,i])^2+dis } ##My code is below w = kern(dis,lam) ## below we calculate the teta1 ky = sum(w*y) kx = sum(w*x) kxy = sum(w*x*y) k = sum(w) kxx = sum(w*x*x) teta1 = (kxy*k - kx*ky)/(kxx*k - kx*kx) ## below we calculate the teta0 teta0 = (ky*kxx - kxy*kx)/(k*kxx - kx*kx) return(teta1*x0 + teta0) ##Your code here } kern<-function(x,lam){ exp(-x/lam)/lam } ###### ## sin(x) Regression Example ###### set.seed(100) x=runif(100,0,2*pi) y=sin(x)+rnorm(100,,0.1) xgrid=seq(0,2*pi,length=500) n=length(xgrid) ygrid=vector(length=n) k=15 lam=0.01 for(i in 1:n){ ##ygrid[i]= loess0(xgrid[i],x,y,kern,lam) ##loess d=0 , d=1 ygrid[i]= loess1(xgrid[i],x,y,kern,lam) ##loess d=0 , d=1 } plot(x,y,pch=16) lines(xgrid,ygrid,col=c("red")) lines(xgrid,sin(xgrid),col=c("blue"))