Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Density Value for each Return

I have a dataframe "foo" looking like this

Date       Return
1998-01-01  0.02
1998-01-02  0.04
1998-01-03 -0.02
1998-01-04 -0.01
1998-01-05  0.02
...
1998-02-01  0.1
1998-02-02 -0.2
1998-02-03 -0.1
etc.

I would like to add to this dataframe a new column showing me the density value of the corresponding return. I tried:

foo$density <- for(i in 1:length(foo$Return)) density(foo$Return, 
from = foo$Return[i], to = foo$Return[i], n = 1)$y

But it didn't work. I really have difficulty applying a "function" to each row. But maybe there is also another way to do it, not using density()?

What I essentially would like to do is to extract the fitted density values from density() to the returns in foo. If I just do plot(density(foo$Return)) it gives me the curve, however I would like to have the density values attached to the returns.

@Joris:

foo$density <- density(foo$Return, n=nrow(foo$Return))$y 

calculates something, however seems to return wrong density values.

Thank you for helping me out! Dani

like image 701
Dani Avatar asked Dec 22 '22 20:12

Dani


1 Answers

On second thought, forget about the density function, I suddenly realized what you wanted to do. Most density functions return a grid, so don't give you the evaluation in the exact points. If you want that, you can eg use the sm package:

require(sm)
foo <- data.frame(Return=rpois(100,5))
foo$density <- sm.density(foo$Return,eval.points=foo$Return)$estimate
# the plot
id <- order(foo$Return)
hist(foo$Return,freq=F)
lines(foo$Return[id],foo$density[id],col="red")

If the number of different values is not that big, you can use ave() :

foo$counts <- ave(foo$Return,foo$Return,FUN=length)

If the purpose is to plot a density function, there's no need to calculate it like you did. Just use

plot(density(foo$Return))

Or, to add a histogram underneath (mind the option freq=F)

hist(foo$Return,freq=F)
lines(density(foo$Return),col="red")
like image 50
Joris Meys Avatar answered Jan 19 '23 11:01

Joris Meys