## Smooth 10 first values of a vector
kern_smooth(stockprice$DAX[1:20], h = 5)
## Prediction at time step 2 and 3
kern_smooth(stockprice$DAX, h = 1, t.for.pred = c(2, 3))
## Smoothing using a vector of dates for time
kern_smooth(x = stockprice$DAX[1:10], t = stockprice$DateID[1:10], h = 5)
## Smoothing conserves original order
kern_smooth(x = stockprice$DAX[10:1], t = stockprice$DateID[10:1], h = 5)
## Effect of the bandwidth
plot(stockprice$DAX[1:100] ~ stockprice$DateID[1:100],
las = 1, ylab = "DAX index", xlab = "Date")
points(kern_smooth(stockprice$DAX[1:100], stockprice$DateID[1:100], h = 1),
type = "l", col = "grey")
points(kern_smooth(stockprice$DAX[1:100], stockprice$DateID[1:100], h = 3),
type = "l", col = "blue")
points(kern_smooth(stockprice$DAX[1:100], stockprice$DateID[1:100], h = 10),
type = "l", col = "red")
legend("topright", fill = c("grey", "blue", "red"),
legend = c("1", "3", "10"), bty = "n", title = "Bandwidth (h)")
## Effect of the kernel
plot(stockprice$DAX[1:100] ~ stockprice$DateID[1:100],
las = 1, ylab = "DAX index", xlab = "Date")
points(kern_smooth(stockprice$DAX[1:100], stockprice$DateID[1:100], h = 10),
type = "l", col = "orange")
points(kern_smooth(stockprice$DAX[1:100], stockprice$DateID[1:100], h = 10, kernel = "box"),
type = "l", col = "blue")
points(kern_smooth(stockprice$DAX[1:100], stockprice$DateID[1:100], h = 10, kernel = "norm"),
type = "l", col = "red")
legend("topright", fill = c("orange", "blue", "red"),
legend = c("epanechnikov", "box", "normal"), bty = "n", title = "Kernel method")
Run the code above in your browser using DataLab