Solution for kernel weighted local polynomial regression in R- how to compute the estimates at each value of x using locpoly?

is Given Below:

I am trying to age-standardize data using kernel-weighted local polynomial regression. I used lpoly in Stata, but would like to do it in R since my other scripts are in R. I tried locpoly from the KernSmooth package and I was able to get a plot of the smoothed estimate of the PDF, but I need to compute the estimates at each value of x in my data and I’m not sure how to do that. Any help much appreciated!

Stata code, would like to do this in R:

```
* conditional mean
lpoly y x, at(x) gen(mean)
gen r = y - mean
gen r2 = r^2
* conditional variance
lpoly r2 x, at (x) gen(var)
gen std = sqrt(var)
gen y_kd = (y - mean) /std
```

R code:

```
bw <- dpill(x, y)
lpoly_cond_mean <- data.frame(locpoly(x, y, bandwidth = bw, degree = 1))
plot(lpoly_cond_mean$x, lpoly_cond_mean$y, type = "l",
xlab = "Age in days", ylab = "Expected score", lwd =2)
```