使用R计算方差和标准差

第一个例子:

> x <- c(2.5,0.5,2.2,1.9,3.1,2.3,2,1,1.5,1.1)
> y <- c(2.4,0.7,2.9,2.2,3.0,2.7,1.6,1.1,1.6,0.9)
> cov(x,y)

[1] 0.6154444
> var(x,y)
[1] 0.6154444

> Z <- cbind(x,y)
> Z

        x   y
 [1,] 2.5 2.4
 [2,] 0.5 0.7
 [3,] 2.2 2.9
 [4,] 1.9 2.2
 [5,] 3.1 3.0
 [6,] 2.3 2.7
 [7,] 2.0 1.6
 [8,] 1.0 1.1
 [9,] 1.5 1.6
[10,] 1.1 0.9
> typeof(Z)
[1] "double"
> cov(Z)
          x         y
x 0.6165556 0.6154444
y 0.6154444 0.7165556

第二个例子:

> A <- c(12,23,34,44,59,70,98)
> B <- c(12,15,25,27,32,88,99)
> C <- c(15,35,78,82,90,95,97)
> Matix <- cbind(A,B,C)
> Matix

      A  B  C
[1,] 12 12 15
[2,] 23 15 35
[3,] 34 25 78
[4,] 44 27 82
[5,] 59 32 90
[6,] 70 88 95
[7,] 98 99 97
> cov(Matix)
         A         B         C
A 872.6190  967.1190  802.3095
B 967.1190 1267.6190  794.4762
C 802.3095  794.4762 1035.2381
> var(A)    #计算方差Covariance | R Tutorial
[1] 872.619
> sd(A)     #计算标准差Standard Deviation
[1] 29.54013

如果是合并到行,用rbind()函数
求平均值用mean()函数
关于主成份分析的原理:
http://www.cs.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf

你可能感兴趣的:(统计分析)