如何在R中找到标准偏差?

时间:2020-02-23 14:43:49  来源:igfitidea点击:

作为一种统计语言,R提供标准函数sd("")以查找值的标准偏差。

那么标准偏差是多少?

  • "标准偏差是对值分散度的度量"。

  • 标准偏差越高,值的分布范围越广。

  • 标准偏差越低,值的分布范围越窄。

  • 简单来说,公式定义为–标准差是"方差"的平方根。

标准偏差的重要性

标准差在统计数据中非常流行,但是为什么呢?下面列出了其受欢迎程度和重要性的原因。

  • 标准偏差通过平方将负数转换为正数。

  • 它显示了较大的偏差,因此您可以特别查看它们。

  • 它显示了集中趋势,这在分析中非常有用。

  • 它在财务,业务,分析和度量中起着重要作用。

在进入主题之前,请牢记此定义!

方差–定义为观察值与期望值之间的平方差。

在列表中找到R的标准偏差

用这种方法,我们将创建一个列表" x"并为其添加一些值。
然后,我们可以在列表中找到这些值的标准偏差。

x <- c(34,56,87,65,34,56,89)    #creates list 'x' with some values in it.

 sd(x)  #calculates the standard deviation of the values in the list 'x'

输出-> 22.28175

现在,我们可以尝试从列表" y"中提取特定值以找到标准偏差。

y <- c(34,65,78,96,56,78,54,57,89)  #creates a list 'y' having some values
 
data1 <- y[1:5] #extract specific values using its Index

sd(data1) #calculates the standard deviation for Indexed or extracted values from the list.

输出-> 23.28519

查找存储在CSV文件中的值的标准偏差

在这种方法中,我们将导入CSV文件,以找到R中存储在该文件中的值的标准偏差。

readfile <- read.csv('testdata1.csv')  #reading a csv file

data2 <- readfile$Values      #getting values stored in the header 'Values'

sd(data2)                              #calculates the standard deviation  

输出-> 17.88624

高低标准偏差

通常,在低标准偏差下,这些值将非常接近平均值,而在高标准偏差下,这些值将与平均值相差甚远。

我们可以用一个例子来说明。

x <- c(79,82,84,96,98)
mean(x)
--->  82.22222
sd(x)
--->  10.58038

要在R中使用条形图绘制这些值,请运行以下代码。

要安装ggplot2软件包,请在R studio中运行此代码。

—> install.packages(" ggplot2")

library(ggplot2)

values <- data.frame(marks=c(79,82,84,96,98), students=c(0,1,2,3,4,))
head(values)                  #displayes the values
 marks students
1    79        0
2    82        1
3    84        2
4    96        3
5    98        4
x <- ggplot(values, aes(x=marks, y=students))+geom_bar(stat='identity')
x                             #displays the plot

在以上结果中,您可以观察到大多数数据都聚集在平均值(79,82,84)周围,这表明它是一个低标准偏差。

高标准偏差的图示。

y <- c(23,27,30,35,55,76,79,82,84,94,96)
mean(y)
---> 61.90909
sd(y)
---> 28.45507

要使用R中ggplot中的条形图绘制这些值,请运行以下代码。

library(ggplot2)

values <- data.frame(marks=c(23,27,30,35,55,76,79,82,84,94,96), students=c(0,1,2,3,4,5,6,7,8,9,10))
head(values)                  #displayes the values
marks students
1    23        0
2    27        1
3    30        2
4    35        3
5    55        4
6    76        5
x <- ggplot(values, aes(x=marks, y=students))+geom_bar(stat='identity')
x                             #displays the plot

在以上结果中,您可以看到广泛的数据。
您可以看到最低的23分,与平均分数61分相去甚远。
这称为高标准偏差

到目前为止,您已经对使用sd(’)函数计算R语言中的标准差有了一定的了解。
让我们通过解决简单的问题来总结本教程。

示例1:偶数列表的标准偏差

求出1-20之间(不包括1和20)的偶数标准差。

解决方案:1到20之间的偶数是

-> 2,4,6,8,10,12,14,16,18

让我们找到这些值的标准偏差。

x <- c(2,4,6,8,10,12,14,16,18)  #list of even numbers from 1 to 20

sd(x)                           #calculates the standard deviation of these 
                          values in the list of even numbers from 1 to 20

输出-> 5.477226

示例2:美国人口数据的标准差

找出美国各州人口的标准差。

为此,请导入CSV文件并读取值以找到标准偏差,然后将结果绘制在R中的直方图中。

df<-read.csv("population.csv")      #reads csv file
data<-df$X2016.Population           #extarcts the data from population 
                                   column
mean(data)                          #calculates the mean
                        
View(df)                            #displays the data
sd(data)                            #calculates the standard deviation

输出--->平均值= 6432008,标准差= 7376752