I have simulated bivariate data (x,y) where y has mean 1/x and some variance. The data looks something like this: Data
I am using kernel smoothing regression to try and find this relationship.
kernelreg = ksmooth( train_points$x, train_points$y, kernel = "normal", bandwidth = h) plot(y~x, train_points , cex = 0.5, col = "dodgerblue", main = "Data set") lines(kernelreg,lwd = 2, col = 2)
I am wondering how I can write a function to run this regression through a list of bandwidths and compute the rmse in training and test data. Thus showing the optimum bandwidth which minimizes the error of the model.
You can put your model into a function and iterate over the
bandwith
argument withlapply
. Then you can simply calculate RMSE for each run and take themin
.However take a look at the
KernSmooth
package, as suggested by the documentation ofksmooth
: