R语言入门:均值置信区间

Candice
Candice
Candice
96
文章
0
评论
2020-04-1809:05:00 评论 4,077 740字
摘要

本文将介绍如何利用R语言进行均值置信区间的计算。

一、问题

有来自群体的样本。根据样本推断该群体均值的置信区间。

二、解决方案

用t.test函数处理样本x:

代码

> t.test(x)

函数的输出包括95%可信度的置信区间。可以用conf.level参数调整可信度,查看其他可信度的区间。

如果样本数量n比较小,那么就只有在整个群体服从正太分布的情况才会得到有意义的置信区间。通常可以认为n<30就是样本数较少了。

三、讨论

用t.test函数处理向量输出的内容不少。其中重要的是置信区间:

代码

> x <- rnorm(50, mean=100, sd=15)

> t.test(x)

One Sample t-test

data: x

t = 59.2578, df = 49, p-value < 2.2e-16

alternative hypothesis: true mean is not equal to 0

95 percent confidence interval:

97.16167 103.98297

sample estimates:

mean of x

100.5723

在这个例子中,置信区间约为97.16 < μ < 103.98,也可以简写成(97.16, 103.98)。

可以通过设置conf.level=0.99将置信水平提高到99%:

代码

> t.test(x, conf.level=0.99)

One Sample t-test

data: x

t = 59.2578, df = 49, p-value < 2.2e-16

alternative hypothesis: true mean is not equal to 0

99 percent confidence interval:

96.0239 105.1207

sample estimates:

mean of x

100.5723

这样就将置信区间扩大到96.02 < μ < 105.12。

End.

来源:数据分析网

  • 我的微信公众号
  • 微信扫一扫
  • weinxin
  • 我的微信公众号
  • 微信扫一扫
  • weinxin
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: