我试图得到R中股票价格对数的密度估计值.我知道我可以用它来绘制它plot(density(x)).但是,我实际上想要函数的值.
我正在尝试实现核密度估计公式.这是我到目前为止所拥有的:
a <- read.csv("boi_new.csv", header=FALSE)
S = a[,3] # takes column of increments in stock prices
dS=S[!is.na(S)] # omits first empty field
N = length(dS) # Sample size
rseed = 0 # Random seed
x = rep(c(1:5),N/5) # Inputted data
set.seed(rseed) # Sets random seed for reproducibility
QL <- function(dS){
h = density(dS)$bandwidth
r = log(dS^2)
f = 0*x
for(i in 1:N){
f[i] = 1/(N*h) * sum(dnorm((x-r[i])/h))
}
return(f)
}
QL(dS)
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.已经好几天了!