Hello,

This has to do with what kind of variance estimator is being used.
R uses the unbiased estimator and Python the MLE one.



var1 <- function(x){
  n <- length(x)
  (sum(x^2) - sum(x)^2/n)/(n - 1)
}
var2 <- function(x){
  n <- length(x)
  (sum(x^2) - sum(x)^2/n)/n
}

sd1 <- function(x) sqrt(var1(x))
sd2 <- function(x) sqrt(var2(x))

z <- matrix(c(1,2,3,4,5,6,7,8,9), nrow=3, ncol=3, byrow=T)

apply(z, 1, sd1)  # R
apply(z, 1, sd2)  # Python

apply(z, 2, sd1)  # R
apply(z, 2, sd2)  # Python


Hope this helps,

Rui Barradas

Às 11:27 de 24/05/19, Bogdan Tanasa escreveu:
Dear all, please would you advise :

do python and R have different ways to compute the standard deviation (sd) ?

for example, in python, starting with :

a = np.array([[1,2,3],  [4,5,6], [7,8,9]])
print(a.std(axis=1)) ### per row : [0.81649658 0.81649658 0.81649658]
print(a.std(axis=0)) ### per column : [2.44948974 2.44948974 2.44948974]

# and in R :



z <- matrix(c(1,2,3,4,5,6,7,8,9), nrow=3, ncol=3, byrow=T)
# z# [,1] [,2] [,3]#[1,] 1 2 3#[2,] 4 5 6#[3,] 7 8 9
# apply(z, 1, sd)
sd(z[1,]) #1
sd(z[2,]) #1
sd(z[3,]) #1
# apply(z, 2, sd)
sd(z[,1]) #3
sd(z[,2]) #3
sd(z[,3]) #3

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to