# Normal Kernel Estimation in R [closed]

I'm given the following data:

x->c( 0.66 0.35 1.34 0.28 2.35 0.42 0.66 1.61 0.52 0.41 0.36 0.14 1.65 0.45 0.78 0.34 0.90 0.20 0.24 1.03 0.34 0.39 0.42 0.21 0.37 0.90 2.25 0.37 0.20)


Let X denote the log of the variable x then fit a normal kernel density estimate for X, using bandwidths derived from Silverman's rule of thumb, Sheather-Jones (using Silverman's as h0), and then Terrell's maximal smoothing principle. Plot the histogram of the data and the plot each density estimate. Make conclusions.

I am not sure how I create the normal kernel density estimate after I find the bandwidths. So far I have worked on the Silverman's and this is the code I have:

#Silverman's Rule
h0=(4/(3*length(X)))^(1/5)*sd(X)

#Normal Kernel Function

fhat=function(x,h,n){
for (i in 1:n) {
kernel=sum(exp(-.5*((x-x[i])/h)^2))
}
(1/(n*h*sqrt(2*pi)))*kernel
}


The result I get is:

> fhat(X,h0,length(X))
[1] 0.4726643

> h0
[1] 0.2337528


I know my function for the kernel estimate is incorrect because when I plot it I should get a line instead of a single dot, can anyone help me out?

## closed as off-topic by jbowman, kjetil b halvorsen, Michael Chernick, mdewey, CarlNov 13 '18 at 22:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – jbowman, kjetil b halvorsen, Michael Chernick, mdewey, Carl
If this question can be reworded to fit the rules in the help center, please edit the question.

• If you look at your function fhat, you'll notice that you are calculating n values for kernel - and every iteration of the loop overwrites the previous value. When the loop ends, you have kernel corresponding to i=n, so only a single value is returned. You probably want to define kernel = rep(0,n) at the top of the function and then assign kernel[i] = sum(...) instead. Then you have a vector to return. – jbowman Nov 13 '18 at 16:31