Density value for each return

I have a dataframe "foo" similar to 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 a new column to this data frame showing 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 that did not work. It's really hard for me to apply a “function” to every line. But maybe there is another way to do this without using density ()?

What I would like to do is extract the set density values ​​from density () to the values ​​returned in foo. If I just make a graph (density (foo $ Return)), it gives me a curve, however I would like to have density values ​​tied to returns.

@Joris:

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

calculates something, but seems to return incorrect density values.

Thanks for helping me! Dani

+3
3

, , , . , . , , , sm:

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")

, ave():

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

, , .

plot(density(foo$Return))

, ( freq=F)

hist(foo$Return,freq=F)
lines(density(foo$Return),col="red")
+4

sm.density , , approx approxfun, Returns, . :

set.seed(1)
foo <- data.frame(Date = seq(as.Date("2010-01-01"), as.Date("2010-12-31"),
                             by = "days"),
                  Returns = rnorm(365))
head(foo)
## compute the density, on fin grid (512*8 points)
dens <- with(foo, density(Returns, n = 512 * 8))

approx() x y , approxfun(), , , . -, :

## x and y are components of dens, see str(dens)
BAR <- with(dens, approxfun(x = x, y = y))

BAR() , , . Returns:

> with(foo, BAR(Returns[1]))
[1] 0.3268715

, Returns:

> foo <- within(foo, Density <- BAR(Returns))
> head(foo)
        Date    Returns   Density
1 2010-01-01 -0.6264538 0.3268715
2 2010-01-02  0.1836433 0.3707068
3 2010-01-03 -0.8356286 0.2437966
4 2010-01-04  1.5952808 0.1228251
5 2010-01-05  0.3295078 0.3585224
6 2010-01-06 -0.8204684 0.2490127

, , . , Returns, lines :

plot(dens)
with(foo, lines(sort(Returns), BAR(sort(Returns)), col = "red"))

- : Density (in black) and interpolated version (in red)

(512 * 8 ), , , "" Returns, , , lines() , , , lines(), .

+4

density, @Joris, , , , . , , - NULL. , foo$density, , NULL, , , .. R. . ?'for'.

> bar <- for(i in 1:10) {
+     i + 1
+ }
> bar
NULL

> foo <- data.frame(A = 1:10, B = LETTERS[1:10])
> foo$density <- for(i in seq_len(nrow(foo))) {
+     i + 1
+ }
> head(foo) ## No `density`
  A B
1 1 A
2 2 B
3 3 C
4 4 D
5 5 E
6 6 F

, , , , . , i + 1 i 1,..., 10, :

> bar <- numeric(length = 10)
> for(i in seq_along(bar)) {
+     bar[i] <- i + 1
+ }
> bar
 [1]  2  3  4  5  6  7  8  9 10 11

, , , , R , , C .

> bar <- 1:10 + 1
> bar
 [1]  2  3  4  5  6  7  8  9 10 11

, R 1 1 , , - R-talk.

s|l|t|apply(), , . R , , .

+2
source

Source: https://habr.com/ru/post/1781210/


All Articles