你真的看懂R中的stem函数了吗?

哭晕,看到书上的介绍,你一定觉得你已经学会了如何使用R中的 stem()函数绘制茎叶图了。

stem()函数的使用方法是:
stem(x, scale=1,width=80, atom=le-08)
其中x是数据向量.
scale控制绘出茎叶图的长度.
width绘图的宽度.
atom是容差,如果选择scale=2,即将10个个位数俞成两段,0~4为一段,5~9为另一段。

然而事实上,我经过反复的试验,发现width最好取较大的数,他既不表示数据的取值范围也不表示最长的那片叶子的长度,也不表示所有的数据的个数

请看以下来自sf的例子: 
With the default scale, you see that numbers left of the bar go up by two - hence anything after the 4 is forty-something(四十几) or fifty-something:
 
   
   
d<-c(60,85,72,59,37,75,93,7,98,63,41,90,5,17,97)
> stem(d,scale=1)
 
  The decimal point is 1 digit(s) to the right of the |
 
  0 | 577
  2 | 7
  4 | 19
  6 | 0325
  8 | 50378
Using scale=2, you'll see numbers left of the bar go up by one, so now you can get exact reconstruction(再造) of your input, since you input integers:
 
    
    
> stem(d,scale=2)
 
  The decimal point is 1 digit(s) to the right of the |
 
  0 | 57
  1 | 7
  2 |
  3 | 7
  4 | 1
  5 | 9
  6 | 03
  7 | 25
  8 | 5
  9 | 0378

Going further you can even split it by  first and second five within each decade:
 
   
   
> stem(d,scale=4)
 
  The decimal point is 1 digit(s) to the right of the |
 
  0 | 57
  1 |      
  1 | 7
  2 |
  2 |
  3 |
  3 | 7
  4 | 1
  4 |
  5 |
  5 | 9
  6 | 03
  6 |
  7 | 2
  7 | 5
  8 |
  8 | 5
  9 | 03
  9 | 78
Stem plots do not always guarantee you can reproduce the data by reversing the process, and that's not what they're for.

This is probably more of a Stack Overflow question than a CV question, because it focuses on how R works and why, rather than the statistical aspects of stem and leaf plots. Nonetheless...
The way the function is coded is designed to  shorten the length of the output, so that it better fits in the console. Few people, I believe, find that terribly helpful, or at least I don't. Just always remember to start with scale=2, and you may have to play with it further or adjust the width argument. Also, know that there is a fancier偏好者,(发烧友的比较级) version  stem.leaf() in Rcmdr. 


总结:
R的stem函数其实是一个比较糟糕的设计,由于其设计的初衷是让其在控制台上能尽量简短的显示(否则控制台宽度不够),所以,当数据之间的差距较大的时候,就会出问题,他会跳着提升枝干,所以一般要设置sacle,而sacle设置的越大,分茎越多,精度越高,如果你的scale较小,他甚至会自动帮你的数据做四舍五入(这样会降低精度)
> test<-c(57,122,1000)
> stem(test, scale = 2)

  The decimal point is  2 digit(s) to the right of the |
 
    0 | 6
   1 | 2
   2 |
   3 |
   4 |
   5 |
   6 |
   7 |
   8 |
   9 |
  10 | 0
 
> stem(test, scale = 10)
 
  The decimal point is  1 digit(s) to the right of the |
 
    4 | 7
    6 |
    8 |
   10 |
   12 | 2
   14 |
   16 |
   18 |
   20 |
   22 |
   24 |
   26 |
   28 |
   30 |
   32 |
   34 |
   36 |
   38 |
   40 |
   42 |
   44 |
   46 |
   48 |
   50 |
   52 |
   54 |
   56 |
   58 |
   60 |
   62 |
   64 |
   66 |
   68 |
   70 |
   72 |
   74 |
   76 |
   78 |
   80 |
   82 |
   84 |
   86 |
   88 |
   90 |
   92 |
   94 |
   96 |
   98 |
  100 | 0

> stem(test, scale = 20)
 
  The decimal point is 1 digit(s) to the right of the |
 
     5 | 7
    6 |
    7 |
    8 |
    9 |
   10 |
   11 |
   12 | 2
   13 |
   14 |
   15 |
   16 |
   17 |
   18 |
   19 |
   20 |
   21 |
   22 |
   23 |
   24 |
   25 |
   26 |
   27 |
   28 |
   29 |
   30 |
   31 |
   32 |
   33 |
   34 |
   35 |
   36 |
   37 |
   38 |
   39 |
   40 |
   41 |
   42 |
   43 |
   44 |
   45 |
   46 |
   47 |
   48 |
   49 |
   50 |
   51 |
   52 |
   53 |
   54 |
   55 |
   56 |
   57 |
   58 |
   59 |
   60 |
   61 |
   62 |
   63 |
   64 |
   65 |
   66 |
   67 |
   68 |
   69 |
   70 |
   71 |
   72 |
   73 |
   74 |
   75 |
   76 |
   77 |
   78 |
   79 |
   80 |
   81 |
   82 |
   83 |
   84 |
   85 |
   86 |
   87 |
   88 |
   89 |
   90 |
   91 |
   92 |
   93 |
   94 |
   95 |
   96 |
   97 |
   98 |
   99 |
  100 | 0
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值