# 均值估計

``````# x 为均值5, 方差1 的总体中抽取的10 个样本
> x=rnorm(10,5)
> x
[1] 4.927264 4.067237 6.136822 5.722123 6.286754 3.266601 4.443779 3.630787
[9] 4.874269 3.748306
# z 值为qnorm(0.025)=-1.959964, qnorm(0.975)=1.959964
> mean(x)+qnorm(0.025)*1/sqrt(10)
[1] 4.090599
> mean(x)+qnorm(0.975)*1/sqrt(10)
[1] 5.330189```
```

``````> 3+qt(p=0.025,df=20)*5/sqrt(20)
[1] 0.667822
> 3+qt(p=0.975,df=20)*5/sqrt(20)
[1] 5.332178```
```

``````# x 为均值5, 方差1 的总体中抽取的10 个样本
> x=rnorm(10,5)
> x
[1] 4.927264 4.067237 6.136822 5.722123 6.286754 3.266601 4.443779 3.630787
[9] 4.874269 3.748306
# z 值为qnorm(0.025)=-1.959964, qnorm(0.975)=1.959964
> mean(x)+qnorm(0.025)*1/sqrt(10)
[1] 4.090599
> mean(x)+qnorm(0.975)*1/sqrt(10)
[1] 5.330189```
```

``````> 3+qt(p=0.025,df=20)*5/sqrt(20)
[1] 0.667822
> 3+qt(p=0.975,df=20)*5/sqrt(20)
[1] 5.332178```
```

# 方差估计 (變異數估計)

``````> x
[1] -5 -4 -3 -2 -1 0 1 2 3 4 5
> var(x)
[1] 11
> sum((x-mean(x))^2)/(length(x)-1)
[1] 11```
```

``````> x
[1] -5 -4 -3 -2 -1 0 1 2 3 4 5
> (10-1)*var(x)/qchisq(0.025,10-1)
[1] 36.66138
> (10-1)*var(x)/qchisq(0.975,10-1)
[1] 5.20429```
```

# 二項分布的估計

``````> x=rbinom(10,1,0.5)
> x
[1] 1 1 0 1 1 1 0 0 1 0
> t=table(x)
> t
x
0 1
4 6
> t['1']/length(x) # 此即为p的点估计, 还可以使用binom.test(table(x))得到.
1
0.6
> sqrt(t['1']*t['0']/length(x)) # 此为标准误差的点估计
1
1.549193```
```

p的区间估计

``````> binom.test(table(x))
Exact binomial test
data: table(x)
number of successes = 4, number of trials = 10, p-value = 0.7539
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.1215523 0.7376219
sample estimates:
probability of success
0.4
> b=binom.test(table(x))
> str(b)
List of 9
\$ statistic : Named int 4
..- attr(*, "names")= chr "number of successes"
\$ parameter : Named int 10
..- attr(*, "names")= chr "number of trials"
\$ p.value : Named num 0.754
..- attr(*, "names")= chr "0"
\$ conf.int : atomic [1:2] 0.122 0.738
..- attr(*, "conf.level")= num 0.95
\$ estimate : Named num 0.4
..- attr(*, "names")= chr "probability of success"
\$ null.value : Named num 0.5
..- attr(*, "names")= chr "probability of success"
\$ alternative: chr "two.sided"
\$ method : chr "Exact binomial test"
\$ data.name : chr "table(x)"
- attr(*, "class")= chr "htest"```
```

page revision: 6, last edited: 02 Sep 2011 23:36