Monday, October 9, 2017

sd() {base}


sd() function that computes the standard deviation of the values in x.

sd(x, na.rm = FALSE)

The arguments are:
  • x: numeric vector
  • na.rm: logical value indicating whether NA values should be removed before the computation proceeds.

Formula to calculate the Standard Deviation:
σ=(xix¯)2n1
SD quantifies how much the members of a group differ from the mean value for the group.

x = c(1,6,10,23,4,5,56)
sd1 = sqrt(sum((x - mean(x))^2)/(length(x)-1))
sd2 = sd(x)
sd1 ; sd2 #same result
## [1] 19.44222
## [1] 19.44222

na.rm:
y = c(1,6,10,23,NA, NA, 4,5,56, 56, NA)
sd3 = sqrt(sum((y - mean(y))^2)/(length(y)-1))
sd4 = sd(y)
sd5 = sd(y, na.rm = TRUE) #remove NA values to compute the SD
sd3 ; sd4 ; sd5
## [1] NA
## [1] NA
## [1] 23.11114
summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 
meanset = mean(iris$Sepal.Length[iris$Species=='setosa'])
meanversi = mean(iris$Sepal.Length[iris$Species=='versicolor'])
meanvir = mean(iris$Sepal.Length[iris$Species=='virginica'])

sdset = sd(iris$Sepal.Length[iris$Species=='setosa'])
sdversi = sd(iris$Sepal.Length[iris$Species=='versicolor'])
sdvir = sd(iris$Sepal.Length[iris$Species=='virginica'])
plot(iris$Species, iris$Sepal.Length, col = 'lightblue', main = 'SD as a measure of spread')
segments(1, meanset+sdset,1, meanset-sdset, col = 'deeppink', lwd = 5)
segments(2, meanversi+ sdversi, 2, meanversi-sdversi, col = 'deeppink', lwd = 5)
segments(3, meanvir + sdvir/2, 3, meanvir-sdvir/2, col = 'deeppink',lwd = 5)

No comments:

Post a Comment

duplicated() {base}

duplicated()  function determines which elements are duplicated and returns a logical vector. The parameters of the function are:   ...