统计建模与R软件 第三章习题

习题3.1

计算均值、方差标准差、极差、标准误、变异系数、偏度、峰度等

  1. 先读取数据

两种方式读取数据(从电子书中荡下来数据放入txt文档)
- scan
- read.delim

R1 <- scan("E:\\R\\R3.txt")    
R2 <- read.delim("E:\\R\\R3.txt",header = FALSE)
R1;R2
##   [1] 74.3 78.8 68.8 78.0 70.4 80.5 80.5 69.7 71.2 73.5 79.5 75.6 75.0 78.8
##  [15] 72.0 72.0 72.0 74.3 71.2 72.0 75.0 73.5 78.8 74.3 75.8 65.0 74.3 71.2
##  [29] 69.7 68.0 73.5 75.0 72.0 64.3 75.8 80.3 69.7 74.3 73.5 73.5 75.8 75.8
##  [43] 68.8 76.5 70.4 71.2 81.2 75.0 70.4 68.0 70.4 72.0 76.5 74.3 76.5 77.6
##  [57] 67.3 72.0 75.0 74.3 73.5 79.5 73.5 74.7 65.0 76.5 81.6 75.4 72.7 72.7
##  [71] 67.2 76.5 72.7 70.4 77.2 68.8 67.3 67.3 67.3 72.7 75.8 73.5 75.0 73.5
##  [85] 73.5 73.5 72.7 81.6 70.3 74.3 73.5 79.5 70.4 76.5 72.7 77.2 84.3 75.0
##  [99] 76.5 70.4
##      V1   V2   V3   V4   V5   V6   V7   V8   V9  V10
## 1  74.3 78.8 68.8 78.0 70.4 80.5 80.5 69.7 71.2 73.5
## 2  79.5 75.6 75.0 78.8 72.0 72.0 72.0 74.3 71.2 72.0
## 3  75.0 73.5 78.8 74.3 75.8 65.0 74.3 71.2 69.7 68.0
## 4  73.5 75.0 72.0 64.3 75.8 80.3 69.7 74.3 73.5 73.5
## 5  75.8 75.8 68.8 76.5 70.4 71.2 81.2 75.0 70.4 68.0
## 6  70.4 72.0 76.5 74.3 76.5 77.6 67.3 72.0 75.0 74.3
## 7  73.5 79.5 73.5 74.7 65.0 76.5 81.6 75.4 72.7 72.7
## 8  67.2 76.5 72.7 70.4 77.2 68.8 67.3 67.3 67.3 72.7
## 9  75.8 73.5 75.0 73.5 73.5 73.5 72.7 81.6 70.3 74.3
## 10 73.5 79.5 70.4 76.5 72.7 77.2 84.3 75.0 76.5 70.4
is.data.frame(R1);is.data.frame(R2)
## [1] FALSE
## [1] TRUE
#可发现 scan与read.delim不同
#scan 将数据读取为一维向量,而read.delim读取为数据框,且默认有表头header
  1. 再计算各种统计量
mean(R1);max(R1)-min(R1);var(R1);sd(R1);100*sd(R1)/mean(R1)  #变异系数
## [1] 73.668
## [1] 20
## [1] 15.51513
## [1] 3.938925
## [1] 5.34686

习题3.2-3.3

绘制3.1中数据的各种统计图

hist(R1,freq = FALSE) #直方图 freq-TRUE频数 FALSE频率
lines(density(R1))  #核密度估计曲线
x <- 65:85
y <- dnorm(x,mean(R1),sd(R1))
lines(x,y,col="blue")   #添加正态密度曲线 

plot(ecdf(R1),verticals = TRUE,do.p=FALSE)  #经验分布函数
y <- pnorm(x,mean(R1),sd(R1))
lines(x,y,col="red")  #添加正态分布曲线

qqnorm(R1);qqline(R1)   #QQ图

stem(R1,scale = 0.5)   #茎叶图
## 
##   The decimal point is 1 digit(s) to the right of the |
## 
##   6 | 4
##   6 | 557777788999
##   7 | 000000000001111222222233333344444444444444444444
##   7 | 55555555566666677777777788999
##   8 | 0000111224
boxplot(R1)   #箱线图

习题3.5

绘制小白鼠存活天数的箱线图
1.plot
2.boxplot

x <- scan("E:\\R\\R3.5.txt")
y <- factor(c(rep(1,11),rep(2,10),rep(3,12)))
x;y
##  [1]  2  4  3  2  4  7  7  2  2  5  4  5  6  8  5 10  7 12 12  6  6  7 11
## [24]  6  6  7  9  5  5 10  6  3 10
##  [1] 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3
## Levels: 1 2 3
plot(x~y)

boxplot(x~y)  #plot 与 boxplot 结果一致

习题3.6

绘制三指标散点图,判断相关性 1.plot
2.pairs
3.coplot

x <- read.delim("E:\\R\\R3.6.txt",header = F)
rubber <- x[,2:4]  #数据中第一列为编号,剔除
colnames(rubber) <- c("X1","X2","X3")  #为列命名
rubber
##    X1 X2   X3
## 1  65 45 27.6
## 2  70 45 30.7
## 3  70 48 31.8
## 4  69 46 32.6
## 5  66 50 31.0
## 6  67 46 31.3
## 7  68 47 37.0
## 8  72 43 33.6
## 9  66 47 33.1
## 10 68 48 34.2
plot(rubber)  #三项指标散布图
pairs(rubber)  #与plot结果一致

attach(rubber) 
coplot(X1~X2|X3)  #given X3 | X3分段的情况下,作X1 X2的散点图

习题3.7、3.9

绘制学生身高、体重散点图

stu <- read.delim("E:\\R\\R3.7.txt") #data.frame
stu
##    学号    姓名 性别 年龄 身高  体重
## 1     1   Alice    F   13 56.5  84.0
## 2     2   Becka    F   13 65.3  98.0
## 3     3    Gail    F   14 64.3  90.0
## 4     4   Karen    F   12 56.3  77.0
## 5     5   Kathy    F   12 59.8  84.5
## 6     6    Mary    F   15 66.5 112.0
## 7     7   Sandy    F   11 51.3  50.5
## 8     8  Sharon    F   15 62.5 112.5
## 9     9   Tammy    F   14 62.8 102.5
## 10   10  Alfred    M   14 69.0 112.5
## 11   11    Duke    M   14 63.5 102.5
## 12   12   Guido    M   15 67.0 133.0
## 13   13   James    M   12 57.3  83.0
## 14   14 Jeffrey    M   13 62.5  84.0
## 15   15    John    M   12 59.0  99.5
## 16   16  Philip    M   16 72.0 150.0
## 17   17  Robert    M   12 64.8 128.0
## 18   18  Thomas    M   11 57.5  85.0
## 19   19 William    M   15 66.5 112.0
plot(stu$体重 ~ stu$身高,xlab="身高",ylab="体重",main="体重&身高")

coplot(stu$体重 ~ stu$身高|stu$性别)

coplot(stu$体重 ~ stu$身高|stu$年龄)

coplot(stu$体重 ~ stu$身高|stu$年龄+stu$性别)

相关系数 & 相关性检验 person

cor(stu$体重,stu$身高)
## [1] 0.8777852
cor.test(stu$体重,stu$身高)
## 
##  Pearson's product-moment correlation
## 
## data:  stu$体重 and stu$身高
## t = 7.5549, df = 17, p-value = 7.887e-07
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.7044314 0.9523101
## sample estimates:
##       cor 
## 0.8777852

习题3.8

绘制函数的二维等值线 及 三维网格曲面

x <- seq(-2,3,0.5)
y <- seq(-1,7,0.5)
fun <- function(x,y) x^4-2*x^2*y+x^2-2*x*y+2*y^2+9/2*x-4*y+4
z <- outer(x,y,fun)
contour(x,y,z,levels = c(0,1,2,3,4,5,10,15,20,30,40,50,60,80,100))

persp(x, y, z,theta = -40, phi = 15, expand = 0.7)  #调整参数以获得更好的视觉效果

习题3.10-3.11

绘出求职者数据星图

rt <- read.delim("E:\\R\\R3.11.txt")
rt <- rt[,2:16]
stars(rt,labels = 1:48,ncol = 8)

attach(rt)
r <- data.frame(
  G1=(SC+LC+SMS+DRV+AMB+GSP+POT)/7,
  G2=(FL+EXP+SUIT)/3,
  G3=(LA+HON+KJ)/3,
  G4=AA,
  G5=APP
);r
##           G1        G2         G3 G4 G5
## 1  7.4285714  6.333333  6.6666667  2  7
## 2  9.0000000  8.000000  8.3333333  5 10
## 3  8.0000000  7.000000  7.6666667  3  8
## 4  5.2857143  6.000000  6.6666667  8  6
## 5  5.5714286  7.000000  8.0000000  8  8
## 6  6.4285714  7.333333  7.3333333  7  7
## 7  8.4285714  9.666667  8.0000000  8  9
## 8  9.0000000  9.666667  8.3333333  9  9
## 9  7.7142857  9.333333  8.0000000  7  9
## 10 9.8571429  5.666667  4.0000000 10  7
## 11 9.1428571  4.666667  1.6666667 10  7
## 12 9.1428571  4.333333  4.6666667 10  7
## 13 4.7142857  6.000000  8.3333333  8  9
## 14 4.5714286  6.333333  7.3333333  8  9
## 15 4.4285714  5.666667  7.0000000  8  8
## 16 7.8571429  8.000000  7.0000000  6  9
## 17 6.7142857  7.333333  7.3333333  7  7
## 18 5.4285714  4.333333  5.3333333  8  8
## 19 5.4285714  4.666667  4.6666667  8  7
## 20 7.4285714  5.000000  8.6666667  7  8
## 21 7.1428571  4.666667  7.6666667  6  8
## 22 9.2857143  6.666667  9.3333333  7  8
## 23 9.2857143  6.000000  9.6666667  7 10
## 24 8.8571429  6.333333 10.0000000  7  8
## 25 4.0000000  4.000000  7.0000000  7  9
## 26 4.4285714  5.333333  7.0000000  7  8
## 27 6.5714286  3.333333  7.6666667  7 10
## 28 2.0000000  2.000000  4.3333333  5  3
## 29 1.5714286  1.333333  2.6666667  4  3
## 30 3.7142857  2.666667  7.6666667  5  6
## 31 4.5714286  3.333333  8.3333333  4  5
## 32 5.5714286  2.333333  7.3333333  5  3
## 33 4.8571429  2.000000  7.3333333  5  3
## 34 2.5714286  2.000000  5.6666667  6  4
## 35 1.2857143  3.333333  5.6666667  4  7
## 36 4.4285714  4.666667  6.3333333  5  8
## 37 7.2857143  2.333333  5.0000000  6  9
## 38 7.4285714  2.333333  6.0000000  6  9
## 39 9.5714286 10.000000 10.0000000  9  6
## 40 9.8571429 10.000000 10.0000000  9  6
## 41 1.1428571 10.000000  0.6666667  8  7
## 42 0.2857143 10.000000  0.0000000  8  3
## 43 2.5714286  5.666667  5.3333333  9  4
## 44 8.1428571  6.666667  6.6666667  7  7
## 45 5.8571429  5.000000  7.6666667 10  6
## 46 7.0000000  4.666667  8.0000000 10  8
## 47 1.2857143  0.000000  4.3333333 10  7
## 48 1.2857143  0.000000  3.6666667 10  6
stars(r,labels = 1:48,ncol = 8)

stars(r,full = FALSE,draw.segments=TRUE,col.segments=1:5,labels = 1:48,ncol = 8)

绘出调和曲线
调和曲线利用三角表示法,将多维空间中的点对应为二维空间的一条曲线

unison <- function(x){
  if(is.data.frame(x)==TRUE)
    x <- as.matrix(x)
  t <- seq(-pi,pi,pi/30)
  m <- nrow(x); n <- ncol(x)
  f <- array(0,c(m,length(t)))  #create an array(data,dim)
  #如果修改为仅创建一维数组,无法得出结果;因t是一个长度为61的向量,f必须定义为2维
  for(i in 1:m){
    f[i,] <- x[i,1]/sqrt(2)
    for(j in 2:n){
      if(j%%2 == 0)
        f[i,] <- f[i,]+x[i,j]*sin(j/2*t)
      else
        f[i,] <- f[i,]+x[i,j]*cos(j%/%2*t)
    }
  }
  plot(c(-pi,pi),c(min(f),max(f)),
       type = "n",
       main = "The Unison gragh of Data",
       xlab = "t", ylab = "f(t)")
  for(i in 1:m){
    lines(t,f[i,],col=i) 
  }
};unison(r)