R软件与统计建模.docx

上传人:b****5 文档编号:5043466 上传时间:2022-12-12 格式:DOCX 页数:17 大小:19.74KB
下载 相关 举报
R软件与统计建模.docx_第1页
第1页 / 共17页
R软件与统计建模.docx_第2页
第2页 / 共17页
R软件与统计建模.docx_第3页
第3页 / 共17页
R软件与统计建模.docx_第4页
第4页 / 共17页
R软件与统计建模.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

R软件与统计建模.docx

《R软件与统计建模.docx》由会员分享,可在线阅读,更多相关《R软件与统计建模.docx(17页珍藏版)》请在冰豆网上搜索。

R软件与统计建模.docx

R软件与统计建模

Ex4.5

>x<-c(54,67,68,78,70,66,67,70,65,69)

>t.test(x)#t.test()做单样本正态分布区间估计

OneSamplet-test

data:

x

t=35.947,df=9,p-value=4.938e-11

alternativehypothesis:

truemeanisnotequalto0

95percentconfidenceinterval:

63.158571.6415

sampleestimates:

meanofx

67.4

平均脉搏点估计为67.4,95%区间估计为63.158571.6415。

>t.test(x,alternative="less",mu=72)#t.test()做单样本正态分布单侧区间估计OneSamplet-test

data:

x

t=-2.4534,df=9,p-value=0.01828

alternativehypothesis:

truemeanislessthan72

95percentconfidenceinterval:

-Inf70.83705

sampleestimates:

meanofx

67.4

p值小于0.05,拒绝原假设,平均脉搏低于常人。

要点:

t.test()函数的用法。

本例为单样本;可做双边和单侧检验。

Ex4.6

>x<-c(140,137,136,140,145,148,140,135,144,141);x

[1]140137136140145148140135144141

>y<-c(135,118,115,140,128,131,130,115,131,125);y

[1]135118115140128131130115131125

>t.test(x,y,var.equal=TRUE)

TwoSamplet-test

data:

xandy

t=4.6287,df=18,p-value=0.0002087

alternativehypothesis:

truedifferenceinmeansisnotequalto0

95percentconfidenceinterval:

7.5362620.06374

sampleestimates:

meanofxmeanofy

140.6126.8

期望差的95%置信区间为7.5362620.06374。

要点:

t.test()可做两正态样本均值差估计。

此例认为两样本方差相等。

ps:

我怎么觉得这题应该用配对t检验?

Ex4.7

>x<-c(0.143,0.142,0.143,0.137)

>y<-c(0.140,0.142,0.136,0.138,0.140)

>t.test(x,y,var.equal=TRUE)

TwoSamplet-test

data:

xandy

t=1.198,df=7,p-value=0.2699

alternativehypothesis:

truedifferenceinmeansisnotequalto095percentconfidenceinterval:

-0.0019963510.006096351

sampleestimates:

meanofxmeanofy

0.141250.13920

期望差的95%的区间估计为-0.0019963510.006096351

Ex4.8

接Ex4.6

>var.test(x,y)

Ftesttocomparetwovariances

data:

xandy

F=0.2353,numdf=9,denomdf=9,p-value=0.04229alternativehypothesis:

trueratioofvariancesisnotequalto195percentconfidenceinterval:

0.058452760.94743902

sampleestimates:

ratioofvariances

0.2353305

要点:

var.test可做两样本方差比的估计。

基于此结果可认为方差不等。

因此,在Ex4.6中,计算期望差时应该采取方差不等的参数。

>t.test(x,y)

WelchTwoSamplet-test

data:

xandy

t=4.6287,df=13.014,p-value=0.0004712

alternativehypothesis:

truedifferenceinmeansisnotequalto095percentconfidenceinterval:

7.35971320.240287

sampleestimates:

meanofxmeanofy

140.6126.8

期望差的95%置信区间为7.35971320.240287。

要点:

t.test(x,y,var.equal=TRUE)做方差相等的两正态样本的均值差估计t.test(x,y)做方差不等的两正态样本的均值差估计

Ex4.9

>x<-c(rep(0,7),rep(1,10),rep(2,12),rep(3,8),rep(4,3),rep(5,2))>n<-length(x)

>tmp<-sd(x)/sqrt(n)*qnorm(1-0.05/2)

>mean(x)

[1]1.904762

>mean(x)-tmp;mean(x)+tmp

[1]1.494041

[1]2.315483

平均呼唤次数为1.9

0.95的置信区间为1.49,2,32

Ex4.10

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

>t.test(x,alternative="greater")

OneSamplet-test

data:

x

t=23.9693,df=9,p-value=9.148e-10

alternativehypothesis:

truemeanisgreaterthan0

95percentconfidenceinterval:

920.8443Inf

sampleestimates:

meanofx

997.1

灯泡平均寿命置信度95%的单侧置信下限为920.8443

要点:

t.test()做单侧置信区间估计

Ex5.1

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

>t.test(x,mu=225)

OneSamplet-test

data:

x

t=-3.4783,df=19,p-value=0.002516

alternativehypothesis:

truemeanisnotequalto225

95percentconfidenceinterval:

172.3827211.9173

sampleestimates:

meanofx

192.15

原假设:

油漆工人的血小板计数与正常成年男子无差异。

备择假设:

油漆工人的血小板计数与正常成年男子有差异。

p值小于0.05,拒绝原假设,认为油漆工人的血小板计数与正常成年男子有差异。

上述检验是双边检验。

也可采用单边检验。

备择假设:

油漆工人的血小板计数小于正常成年男子。

>t.test(x,mu=225,alternative="less")

OneSamplet-test

data:

x

t=-3.4783,df=19,p-value=0.001258

alternativehypothesis:

truemeanislessthan225

95percentconfidenceinterval:

-Inf208.4806

sampleestimates:

meanofx

192.15

同样可得出油漆工人的血小板计数小于正常成年男子的结论。

Ex5.2

>pnorm(1000,mean(x),sd(x))

[1]0.5087941

>x

[1]1067919119678511269369181156920948

>pnorm(1000,mean(x),sd(x))

[1]0.5087941

x<=1000的概率为0.509,故x大于1000的概率为0.491.

要点:

pnorm计算正态分布的分布函数。

在R软件中,计算值均为下分位点。

Ex5.3

>A<-c(113,120,138,120,100,118,138,123)

>B<-c(138,116,125,136,110,132,130,110)

>t.test(A,B,paired=TRUE)

Pairedt-test

data:

AandB

t=-0.6513,df=7,p-value=0.5357

alternativehypothesis:

truedifferenceinmeansisnotequalto0

95percentconfidenceinterval:

-15.628898.87889

sampleestimates:

meanofthedifferences

-3.375

p值大于0.05,接受原假设,两种方法治疗无差异。

Ex5.4

(1)

正态性W检验:

>x<-c(-0.7,-5.6,2,2.8,0.7,3.5,4,5.8,7.1,-0.5,2.5,-1.6,1.7,3,0.4,4.5,4.6,2.5,6,-1.4)

>y<-c(3.7,6.5,5,5.2,0.8,0.2,0.6,3.4,6.6,-1.1,6,3.8,2,1.6,2,2.2,1.2,3.1,1.7,-2)

>shapiro.test(x)

Shapiro-Wilknormalitytest

data:

x

W=0.9699,p-value=0.7527

>shapiro.test(y)

Shapiro-Wilknormalitytest

data:

y

W=0.971,p-value=0.7754

ks检验:

>ks.test(x,"pnorm",mean(x),sd(x))

One-sampleKolmogorov-Smirnovtest

data:

x

D=0.1065,p-value=0.977

alternativehypothesis:

two-sided

Warningmessage:

Inks.test(x,"pnorm",mean(x),sd(x)):

cannotcomputecorrectp-valueswithties

>ks.test(y,"pnorm",mean(y),sd(y))

One-sampleKolmogorov-Smirnovtest

data:

y

D=0.1197,p-value=0.9368

alternativehypothesis:

two-sided

Warningmessage:

Inks.test(y,"pnorm",mean(y),sd(y)):

cannotcomputecorrectp-valueswithties

pearson拟合优度检验,以x为例。

>sort(x)

[1]-5.6-1.6-1.4-0.7-0.50.40.71.72.02.52.52.83.03.54.0[16]4.54.65.86.07.1

>x1<-table(cut(x,br=c(-6,-3,0,3,6,9)))

>p<-pnorm(c(-3,0,3,6,9),mean(x),sd(x))

>p

[1]0.048947120.249900090.620022880.900758560.98828138

>p<-c(p[1],p[2]-p[1],p[3]-p[2],p[4]-p[3],1-p[4]);p

[1]0.048947120.200952980.370122780.280735680.09924144

>chisq.test(x1,p=p)

Chi-squaredtestforgivenprobabilities

data:

x1

X-squared=0.5639,df=4,p-value=0.967

Warningmessage:

Inchisq.test(x1,p=p):

Chi-squaredapproximationmaybeincorrect

p值为0.967,接受原假设,x符合正态分布。

(2)

方差相同模型t检验:

>t.test(x,y,var.equal=TRUE)

TwoSamplet-test

data:

xandy

t=-0.6419,df=38,p-value=0.5248

alternativehypothesis:

truedifferenceinmeansisnotequalto095percentconfidenceinterval:

-2.3261791.206179

sampleestimates:

meanofxmeanofy

2.0652.625

方差不同模型t检验:

>t.test(x,y)

WelchTwoSamplet-test

data:

xandy

t=-0.6419,df=36.086,p-value=0.525

alternativehypothesis:

truedifferenceinmeansisnotequalto095percentconfidenceinterval:

-2.329261.20926

sampleestimates:

meanofxmeanofy

2.0652.625

配对t检验:

>t.test(x,y,paired=TRUE)

Pairedt-test

data:

xandy

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

alternativehypothesis:

truedifferenceinmeansisnotequalto095percentconfidenceinterval:

-2.3731461.253146

sampleestimates:

meanofthedifferences

-0.56

三种检验的结果都显示两组数据均值无差异。

(3)

方差检验:

>var.test(x,y)

Ftesttocomparetwovariances

data:

xandy

F=1.5984,numdf=19,denomdf=19,p-value=0.3153

alternativehypothesis:

trueratioofvariancesisnotequalto195percentconfidenceinterval:

0.63265054.0381795

sampleestimates:

ratioofvariances

1.598361

接受原假设,两组数据方差相同。

Ex5.5

>a<-c(126,125,136,128,123,138,142,116,110,108,115,140)>b<-c(162,172,177,170,175,152,157,159,160,162)

正态性检验,采用ks检验:

>ks.test(a,"pnorm",mean(a),sd(a))

One-sampleKolmogorov-Smirnovtest

data:

a

D=0.1464,p-value=0.9266

alternativehypothesis:

two-sided

>ks.test(b,"pnorm",mean(b),sd(b))

One-sampleKolmogorov-Smirnovtest

data:

b

D=0.2222,p-value=0.707

alternativehypothesis:

two-sided

Warningmessage:

Inks.test(b,"pnorm",mean(b),sd(b)):

cannotcomputecorrectp-valueswithties

a和b都服从正态分布。

方差齐性检验:

>var.test(a,b)

Ftesttocomparetwovariances

data:

aandb

F=1.9646,numdf=11,denomdf=9,p-value=0.3200alternativehypothesis:

trueratioofvariancesisnotequalto195percentconfidenceinterval:

0.50219437.0488630

sampleestimates:

ratioofvariances

1.964622

可认为a和b的方差相同。

选用方差相同模型t检验:

>t.test(a,b,var.equal=TRUE)

TwoSamplet-test

data:

aandb

t=-8.8148,df=20,p-value=2.524e-08

alternativehypothesis:

truedifferenceinmeansisnotequalto0

95percentconfidenceinterval:

-48.24975-29.78358

sampleestimates:

meanofxmeanofy

125.5833164.6000

可认为两者有差别。

Ex5.6

二项分布总体的假设检验:

>binom.test(57,400,p=0.147)

Exactbinomialtest

data:

57and400

numberofsuccesses=57,numberoftrials=400,p-value=0.8876alternativehypothesis:

trueprobabilityofsuccessisnotequalto0.14795percentconfidenceinterval:

0.10974770.1806511

sampleestimates:

probabilityofsuccess

0.1425

P值>0.05,故接受原假设,表示调查结果支持该市老年人口的看法

Ex5.7

二项分布总体的假设检验:

>binom.test(178,328,p=0.5,alternative="greater")

Exactbinomialtest

data:

178and328

numberofsuccesses=178,numberoftrials=328,p-value=0.06794alternativehypothesis:

trueprobabilityofsuccessisgreaterthan0.595percentconfidenceinterval:

0.49576161.0000000

sampleestimates:

probabilityofsuccess

0.5426829

不能认为这种处理能增加母鸡的比例。

Ex5.8

利用pearson卡方检验是否符合特定分布:

>chisq.test(c(315,101,108,32),p=c(9,3,3,1)/16)

Chi-squaredtestforgivenprobabilities

data:

c(315,101,108,32)

X-squared=0.47,df=3,p-value=0.9254

接受原假设,符合自由组合定律。

Ex5.9

利用pearson卡方检验是否符合泊松分布:

>n<-length(z)

>y<-c(92,68,28,11,1,0)

>x<-0:

5

>q<-ppois(x,mean(rep(x,y)));n<-length(y)

>p[1]<-q[1];p[n]=1-q[n-1]

>chisq.test(y,p=p)

Chi-squaredtestforgivenprobabilities

data:

y

X-squared=2.1596,df=5,p-value=0.8267

Warningmessage:

Inchisq.test(y,p=p):

Chi-squaredapproximationmaybeincorrect重新分组,合并频数小于5的组:

>z<-c(92,68,28,12)

>n<-length(z);p<-p[1:

n-1];p[n]<-1-q[n-1]

>chisq.test(z,p=p)

Chi-squaredtestforgivenprobabilities

data:

z

X

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 军事

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1