r代码
> a=c( 914, 920, 910, 934, 953,940, 912, 924, 930)
> t.test(a,mu=950,alternative='less')
One Sample t-test
data: a
t = -4.9589, df = 8, p-value = 0.0005542
alternative hypothesis: true mean is less than 950
95 percent confidence interval:
-Inf 935.2082
sample estimates:
mean of x
926.3333
在R中也没有直接求方差的置信区间的函数, 我们需要编写自己需要的函数,下面的函数chisq.var.test( )可以用来求方差置信区间.
chisq.var.test <- function (x,var,alpha,alternative="two.sided"){
options(digits=4)
result<-list( )
n<-length(x)
v<-var(x)
result$var<-v
chi2<-(n-1)*v/var
result$chi2<-chi2
p<-pchisq(chi2,n-1)
if(alternative == "less"|alternative=="greater"){
result$p.value<-p
} else if (alternative=="two.sided") {
if(p>.5)
p<-1-p
p<-2*p
result$p.value<-p
} else return("your input is wrong")
result$conf.int<-c(
(n-1)*v/qchisq(alpha/2, df=n-1, lower.tail=F),
(n-1)*v/qchisq(alpha/2, df=n-1, lower.tail=T))
result
}
R代码
chisq.var.test(a,0.048**2,0.05,alternative="two.sided")
$var
[1] 0.02648
$chi2
[1] 45.97
$p.value
[1] 4.992e-09
$conf.int
[1] 0.009505 0.218654
p值小于 0.05 ,也就是说 标准差处于不正常状态。
> a
[1] 5.5 5.6 6.3 4.6 5.3 5.0 6.2 5.8 5.1 5.2 5.9
> b
[1] 3.8 4.3 4.2 4.9 4.5 5.2 4.8 4.5 3.9 3.7 3.6 2.9
> shapiro.test(a)
Shapiro-Wilk normality test
data: a
W = 0.9787, p-value = 0.9584
> shapiro.test(b)
Shapiro-Wilk normality test
data: b
W = 0.9785, p-value = 0.9772
> a向量和b向量都是正太分布的,且题目说密度函数差一个平移量,故可以人为是同方差的
r代码:
t.test(a,b,var.equal=TRUE,alternative='greater')
Two Sample t-test
data: a and b
t = 5.306, df = 21, p-value = 1.462e-05
alternative hypothesis: true difference in means is greater than 0
95 percent confidence interval:
0.884 Inf
sample estimates:
mean of x mean of y
5.500 4.192
p值小于0.05 ,故拒绝原假设 a比b要大
> var.test(a,b)
F test to compare two variances
data: a and b
F = 0.4428, num df = 5, denom df = 5, p-value = 0.3921
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.06196 3.16425
sample estimates:
ratio of variances
0.4428
p值大于0.05,a,b 方差没有显著差异。
> t.test(a,b,var.equal=TRUE)
Two Sample t-test
data: a and b
t = 1.8546, df = 10, p-value = 0.09334
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.0007720859 0.0084387526
sample estimates:
mean of x mean of y
0.1406667 0.1368333
p值大于0.05 接手原假设,啊,a,b 均值没有差异
> binom.test(3,15,p=0.3,alternative='less')
Exact binomial test
data: 3 and 15
number of successes = 3, number of trials = 15, p-value = 0.2969
alternative hypothesis: true probability of success is less than 0.3
95 percent confidence interval:
0.0000 0.4398
sample estimates:
probability of success
0.2
p值大于0.05 接受原假设,比例不小于0.3。
或者
> prop.test(3,15,p=0.3)
1-sample proportions test with continuity correction
data: 3 out of 15, null probability 0.3
X-squared = 0.3175, df = 1, p-value = 0.5731
alternative hypothesis: true p is not equal to 0.3
95 percent confidence interval:
0.05315 0.48627
sample estimates:
p
0.2
警告信息:
In prop.test(3, 15, p = 0.3) : Chi-squared近似算法有可能不准
这边的警告是由于样本量太少造成的