I have the following data frame:
dat <- read.table(text=" X prob
1 1 0.1
2 2 0.2
3 3 0.4
4 4 0.3", header=TRUE)
Is there any built-in function or elegant way to calulate mean and variance for discrete random variables in R?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is a
weighted.meanfunction in base R and the Hmisc package has a bunch of wtd.* functions.The survey package from Thomas Lumley provides much more than this simplistic example illustrates. It has the mechanism for handling complex weighting schemes for a variety of statistical modeling procedures:
These are sample statistics rather than the variances that would be calculated for abstract random variables. This result does seem appropriate for a statistical system, but might not be the correct answer for a probability homework question.