R软件课后习题第五章

更新时间:2023-09-15 14:09:01 阅读量: 资格考试认证 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第五章 5.1

####写出求正态总体均值检验的R程序(程序名:mean.test1.R) mean.test1<-function(x, mu=0, sigma=-1, side=0){ source(\

n<-length(x); xb<-mean(x) if (sigma>0){

z<-(xb-mu)/(sigma/sqrt(n))

P<-P_value(pnorm, z, side=side)

data.frame(mean=xb, df=n, Z=z, P_value=P) } else{

t<-(xb-mu)/(sd(x)/sqrt(n))

P<-P_value(pt, t, paramet=n-1, side=side) data.frame(mean=xb, df=n-1, T=t, P_value=P) } }

####写出求P值的R程序(程序名:P_value.R) P_value<-function(cdf, x, paramet=numeric(0), side=0){ n<-length(paramet) P<-switch(n+1, cdf(x),

cdf(x, paramet),cdf(x, paramet[1], paramet[2]), cdf(x, paramet[1], paramet[2], paramet[3]) )

if (side<0) P

else if (side>0) 1-P else

if (P<1/2) 2*P else 2*(1-P) }

####输入数据,再调用函数mean.test1()

>x<-c(220,188,162,230,145,160,238,188,247,113,126,245,164,231,256,183,190,158,224,175) > source(\

> a<-mean.test1(x, mu=225,side=0) > a 得到:

mean df T P_value 1 192.15 19 -3.478262 0.002516436

可知,P值小于0.05,故与正常值存在差异

5.2

####输入数据,再调用函数mean.test1()

> x<-c(1067,919,1196,785,1126,936,918,1156,920,948) > source(\

> mean.test1(x, mu=1000,side=1) 得到:

mean df T P_value 1 997.1 9 -0.06971322 0.5270268

所以灯泡寿命为1000小时以上的概率是0.4729732

5.3

####写出两总体均值检验的R程序(程序名:mean.test2.R) mean.test2<-function(x, y,

sigma=c(-1, -1), var.equal=FALSE, side=0){ source(\

n1<-length(x); n2<-length(y) xb<-mean(x); yb<-mean(y) if (all(sigma>0)){

z<-(xb-yb)/sqrt(sigma[1]^2/n1+sigma[2]^2/n2) P<-P_value(pnorm, z, side=side)

data.frame(mean=xb-yb, df=n1+n2, Z=z, P_value=P) } else{

if (var.equal == TRUE){

Sw<-sqrt(((n1-1)*var(x)+(n2-1)*var(y))/(n1+n2-2)) t<-(xb-yb)/(Sw*sqrt(1/n1+1/n2)) nu<-n1+n2-2 } else{

S1<-var(x); S2<-var(y)

nu<-(S1/n1+S2/n2)^2/(S1^2/n1^2/(n1-1)+S2^2/n2^2/(n2-1)) t<-(xb-yb)/sqrt(S1/n1+S2/n2) }

P<-P_value(pt, t, paramet=nu, side=side)

data.frame(mean=xb-yb, df=nu, T=t, P_value=P) } }

####输入数据,再调用函数mean.test2() > x<-c(113,120,138,120,100,118,138,123) > y<-c(138,116,125,136,110,132,130,110) > source(\

> mean.test2(x, y, var.equal=TRUE, side=0) 得到:

mean df T P_value 1 -3.375 14 -0.5659672 0.5803752

P值大于0.05,故接受原假设

5.4

####写出均值已知和均值未知两种情况方差比检验的R程序 (程序名:var.test2.R)

var.test2<-function(x, y,mu=c(Inf,Inf),side=0){ source(\

n1<-length(x); n2<-length(y) if (all(all(mu

Sx2<-sum((x-mu[1])^2)/n1;Sy2<-sum((y-mu[2])^2)/n2 df1=n1;df2=n2 } else{

Sx2<-var(x); Sy2<-var(y);df1=n1-1;df2=n2-1 }

r<-Sx2/Sy2

P<-P_value(pf, r, paramet=c(df1,df2), side=side) data.frame(rate=r, df1=df1, df2=df2,F=f, P_value=P) } }

####输入数据

>x<-c(-0.70,-5.60,2.00,2.80,0.70,3.50,4.00,5.80,7.10,-0.50,2.50,-1.60,1.70,3.00,0.40,4.50,4.60,2.50,6.00,-1.40)

> a<-shapiro.test(x) > a

Shapiro-Wilk normality test data: x

W = 0.9699, p-value = 0.7527>0.05

>y<-c(3.70,6.50,5.00,5.20,0.80,0.20,0.60,3.40,6.60,-1.10,6.00,3.80,2.00,1.60,2.00,2.20,1.20,3.10,1.70,-2.00)

> b<-shapiro.test(y) > b

Shapiro-Wilk normality test data: y

W = 0.971, p-value = 0.7754>0.05

由以上可知,两组数据均为正态分布 ####输入数据,再调用函数mean.test2()

>x<-c(-0.70,-5.60,2.00,2.80,0.70,3.50,4.00,5.80,7.10,-0.50,2.50,-1.60,1.70,3.00,0.40,4.50,4.60,2.50,6.00,-1.40)

>y<-c(3.70,6.50,5.00,5.20,0.80,0.20,0.60,3.40,6.60,-1.10,6.00,3.80,2.00,1.60,2.00,2.20,1.20,3.10,1.70,-2.00)

> source(\

> a<-mean.test2(x, y, var.equal=TRUE, side=0);a

mean df T P_value 1 -0.56 38 -0.641872 0.5248097

> b<-mean.test2(x, y, var.equal=FALSE, side=0);b mean df T P_value 1 -0.56 36.08632 -0.641872 0.525013

> c<-t.test(x-y, alternative = \

One Sample t-test

data: x - y

t = -0.6464, df = 19, p-value = 0.5257

alternative hypothesis: true mean is not equal to 0 95 percent confidence interval: -2.373146 1.253146 sample estimates: mean of x -0.56

以上P值均大于0.05,故均值无差异。 ####输入数据,再调用函数var.test2()

>x<-c(-0.70,-5.60,2.00,2.80,0.70,3.50,4.00,5.80,7.10,-0.50,2.50,-1.60,1.70,3.00,0.40,4.50,4.60,2.50,6.00,-1.40)

>y<-c(3.70,6.50,5.00,5.20,0.80,0.20,0.60,3.40,6.60,-1.10,6.00,3.80,2.00,1.60,2.00,2.20,1.20,3.10,1.70,-2.00)

> source(\> var.test2(x,y)

rate df1 df2 F P_value 1 1.598361 19 19 1.598361 0.3152554 P值大于0.05,故方差无差异。

5.5

####输入数据,再调用函数var.test2() > a<-shapiro.test(x) > b<-shapiro.test(y) > source(\> c<-var.test2(x,y)

> d<-mean.test2(x, y, var.equal=TRUE, side=0) > a;b;c;d

Shapiro-Wilk normality test

data: x

W = 0.9396, p-value = 0.4934

Shapiro-Wilk normality test

data: y

W = 0.938, p-value = 0.5313

rate df1 df2 F P_value 1 1.964622 11 9 1.964622 0.3200455 mean df T P_value 1 -39.01667 20 -8.81475 2.524319e-08

由以上可知,两组数据均为正态分布,且方差相同;且两组数据有差异

5.6

####调用binom.test()函数 >binom.test(57, 400, p = 0.147,

+ alternative = c(\+ conf.level = 0.95)

Exact binomial test data: 57 and 400

number of successes = 57, number of trials = 400, p-value = 0.8876 alternative hypothesis: true probability of success is not equal to 0.147 95 percent confidence interval: 0.1097477 0.1806511 sample estimates:

probability of success 0.1425 P值大于0.05,故支持结果。

5.7

####调用binom.test()函数 > binom.test(150, 328, p = 0.5,

+ alternative = c(\+ conf.level = 0.95)

Exact binomial test

data: 150 and 328

number of successes = 150, number of trials = 328, p-value = 0.1359 alternative hypothesis: true probability of success is not equal to 0.5 95 percent confidence interval: 0.4024796 0.5129321 sample estimates:

probability of success 0.4573171

本文来源:https://www.bwwdw.com/article/h6vh.html

Top