Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Estimating the Expected Value in R

I am trying to estimate the expected value for a set of discrete values

# Number of observations
n <- 5000

# Generate skewed distribution
skewed_distribution <- c(
  rbeta(floor(0.8 * n), 2, 5) * 40,  # Beta distribution for 0 to 40
  rexp(floor(0.2 * n), rate = 1/20) + 40  
               # Exponential distribution for 40 to 100
)

# Ensure the length is exactly n
skewed_distribution <- skewed_distribution[1:n]

# Plot the distribution
hist(skewed_distribution, main = "Skewed Distribution", 
     xlab = "Value", col = "lightblue", border = "black")

I know how do this manually. https://openstax.org/books/statistics/pages/4-2-mean-or-expected-value-and-standard-deviation

I am wondering if there is an easier or efficient way to apply the above E(x)=sum{n*p(x)} formula. Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think mean(skewed_distribution) will do this for you. If you already had the data in a tabulated form then something like sum(n*value)/sum(n) or sum(p*value) would be better, but it’s probably not worth tabulating and then computing if all you have is the vector of values. If you did want this then

tt <- table(skewed_distributions)
n <- as.numeric(names(skewed_distributions))
sum(n*tt)/sum(n)

would work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading