0

I want calculate standard error of vector and I don't understand why it doesn't work. Let's consider vector:

a = range(10, 14)

Calculating standard error of mean is just to calculate standard deviation and divide it by square root of length of vector:

import numpy as np

se = np.std(a) / np.sqrt(len(a))
se
Out[819]: 0.5590169943749475

However when I calculate this by function:

import scipy.stats
scipy.stats.sem(a)
Out[820]: 0.6454972243679028

I obtain completely something different. Could you please explain to me why? I don't unsterstand why this difference occurs.

John
  • 1,849
  • 2
  • 13
  • 23
  • Standard error is the Standard Deviation over the square root of the sample size.. sqrt(a) is not the standard deviation. – JeffUK Dec 25 '21 at 21:08
  • Sorry, misstype during writing the question. Problem occurs when `np.std(a)` is used. – John Dec 25 '21 at 21:09
  • 6
    Does this answer your question? [Why does numpy std() give a different result to matlab std()?](https://stackoverflow.com/questions/27600207/why-does-numpy-std-give-a-different-result-to-matlab-std) – JeffUK Dec 25 '21 at 21:12
  • 1
    What @JeffUK said. If you check the [documentation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.sem.html) for `scipy.stats.sem`, it says in the notes that it uses a default value of `ddof = 1`, which "is different to the default (0) used by other ddof containing routines, such as np.std and np.nanstd" – AJ Biffl Dec 25 '21 at 21:45

1 Answers1

0

As mentioned in the comments, the Delta degrees-of-freedom defaults is 1 for sem and 0 for std.

import scipy.stats
import numpy as np
a = np.arange(10, 14)
scipy.stats.sem(a,ddof=1), np.std(a, ddof=1)/np.sqrt(len(a))
Bob
  • 13,867
  • 1
  • 5
  • 27