Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an expected value command exist in R

Tags:

r

statistics

I'm sure there must be a straight forward command for this, but I've searched and can't find one. How do I get the expected value from a vector?

Here are the values

y <- c(0.05, 0.01, -0.1)

And their probabilities

p <- c(0.2, 0.7, 0.1)

I can get E(Y) by doing

sum(y*p)

But I think there is probably a command for it right, I just can't find it. Thanks!

like image 978
Tim H UK Avatar asked Dec 01 '22 01:12

Tim H UK


1 Answers

You can use weighted.mean:

weighted.mean(y, p)
# [1] 0.007
like image 86
Sven Hohenstein Avatar answered Dec 05 '22 02:12

Sven Hohenstein