简体   繁体   中英

How do I calculate the standard deviation of an arbitrary distribution in r?

x    Px
134  .5
565  .25
65   .125
563  .125

Suppose the above is the data.frame. Is there a function that I can input it into to compute the standard deviation?

You could calculate the SD analytically ( more info ):

在此处输入图片说明

Or numerically:

samp = sample(x = x, n = 1e6, replace = TRUE, prob = Px)
sd(samp)

You could use sd function from the stats package ( https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/sd ).

Something like this:

result = sd(your_data_frame$Px)

1) lm Assuming input P shown in the Note at the end, we can use lm

fm <- lm(x ~ 1, P, weight = Px)
sqrt(deviance(fm))
## [1] 216.1202

or we can replace the last line with:

sqrt(nobs(fm) - 1) * sigma(fm)
## [1] 216.1202

2) direct or do it directly:

m <- with(P, sum(x * Px))
with(P, sqrt(sum((x - m)^2 * Px)))
## [1] 216.1202

or

with(P, sqrt(sum(x^2*Px)-sum(x*Px)^2))
## [1] 216.1202

3) In the particular example in the question, if we multiply the probabilities by 8 the results are all integer so if we repeat each item that number of times we can simply take the standard deviation of those numbers using sd except that sd gives the sample standard deviation so we need to correct that to give the population standard deviation.

xx <- with(P, rep(x, 8 * Px))
n <- length(xx)
with(P, sqrt((n-1)/n) * sd(xx))
## [1] 216.1202

Note

The input data frame in reproducible form is assumed to be:

Lines <- "x    Px
134  .5
565  .25
65   .125
563  .125"
P <- read.table(text = Lines, header = TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM