Does an expected value command exist in R

前提是你 提交于 2019-12-21 21:19:53

问题


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!


回答1:


You can use weighted.mean:

weighted.mean(y, p)
# [1] 0.007



回答2:


Here's another option:

> c(y %*% p)
[1] 0.007


来源:https://stackoverflow.com/questions/26959127/does-an-expected-value-command-exist-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!