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?

Upvotes: 0

Views: 481

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 269311

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)

Upvotes: 0

filups21
filups21

Reputation: 1907

You could calculate the SD analytically (more info):

enter image description here

Or numerically:

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

Upvotes: 1

Diego Barbosa
Diego Barbosa

Reputation: 31

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)

Upvotes: 0

Related Questions