Search code examples
rmeandiscrete

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.


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.