《统计建模与R软件》书本课后习题答案(共62页).docx

上传人:飞****2 文档编号:14224749 上传时间:2022-05-03 格式:DOCX 页数:69 大小:143.30KB
返回 下载 相关 举报
《统计建模与R软件》书本课后习题答案(共62页).docx_第1页
第1页 / 共69页
《统计建模与R软件》书本课后习题答案(共62页).docx_第2页
第2页 / 共69页
点击查看更多>>
资源描述

《《统计建模与R软件》书本课后习题答案(共62页).docx》由会员分享,可在线阅读,更多相关《《统计建模与R软件》书本课后习题答案(共62页).docx(69页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、精选优质文档-倾情为你奉上第二章答案:Ex2.1x-c(1,2,3)y-c(4,5,6)e-c(1,1,1)z=2*x+y+ez1=crossprod(x,y)#z1为x1与x2的内积或者x%*%yz2=tcrossprod(x,y)#z1为x1与x2的外积或者x%o%yz;z1;z2要点:基本的列表赋值方法,内积和外积概念。内积为标量,外积为矩阵。Ex2.2A-matrix(1:20,c(4,5);AB-matrix(1:20,nrow=4,byrow=TRUE);BC=A+B;C#不存在AB这种写法E=A*B;EF-A1:3,1:3;FH-matrix(c(1,2,4,5),nrow=1)

2、;H#H起过渡作用,不规则的数组下标G-B,H;G要点:矩阵赋值方法。默认是byrow=FALSE,数据按列放置。取出部分数据的方法。可以用数组作为数组的下标取出数组元素。Ex2.3x-c(rep(1,times=5),rep(2,times=3),rep(3,times=4),rep(4,times=2);x#或者省略times=,如下面的形式x-c(rep(1,5),rep(2,3),rep(3,4),rep(4,2);x要点:rep()的使用方法。rep(a,b)即将a重复b次Ex2.4n-5;H-array(0,dim=c(n,n)for(iin1:n)for(jin1:n)Hi,j-

3、1/(i+j-1);HG-solve(H);G#求H的逆矩阵ev-eigen(H);ev#求H的特征值和特征向量要点:数组初始化;for循环的使用待解决:如何将很长的命令(如for循环)用几行打出来再执行?每次想换行的时候一按回车就执行了还没打完的命令.Ex2.5StudentData-data.frame(name=c(zhangsan,lisi,wangwu,zhaoliu,dingyi),sex=c(F,M,F,M,F),age=c(14,15,16,14,15),height=c(156,165,157,162,159),weight=c(42,49,41.5,52,45.5);Stu

4、dentData要点:数据框的使用待解决:SSH登陆linux服务器中文显示乱码。此处用英文代替。Ex2.6write.table(StudentData,file=studentdata.txt)#把数据框StudentData在工作目录里输出,输出的文件名为studentdata.txt.StudentData_a-read.table(studentdata.txt);StudentData_a#以数据框的形式读取文档studentdata.txt,存入数据框StudentData_a中。write.csv(StudentData_a,studentdata.csv)#把数据框Stude

5、ntData_a在工作目录里输出,输出的文件名为studentdata.csv,可用Excel打开.要点:读写文件。read.table(file)write.table(Rdata,file)read.csv(file)write.csv(Rdata,file)外部文件,不论是待读入或是要写出的,命令中都得加双引号。Ex2.7Fun-function(n)if(n=0)list(fail=pleaseinputaintegerabove0!)elserepeatif(n=1)breakelseif(n%2=0)n-n/2elsen-3*n+1list(sucess!)在linux下新建一个R

6、文件,输入上述代码,保存为2.7.R然后在当前目录下进入R环境,输入source(2.7.R),即打开了这个程序脚本。然后就可以执行函数了。输入Fun(67),显示sucess!输入Fun(-1),显示$fail1pleaseinputaintegerabove0!待解决:source(*.R)是可以理解为载入这个R文件吧?如何在R环境下关闭R文件呢?OK,自己写的第一个R程序Ex3.1新建txt文件如下:3.1.txt74.379.575.073.575.874.073.567.275.873.578.875.673.575.075.872.079.576.573.579.568.875.0

7、78.872.068.876.573.572.775.070.478.078.874.364.376.574.374.770.472.776.570.472.075.875.870.476.565.077.273.572.780.572.065.080.371.277.676.568.873.577.280.572.074.369.781.267.381.667.372.784.369.774.371.274.375.072.075.467.381.675.071.271.269.773.570.475.072.767.370.376.573.572.068.073.568.074.372.7

8、72.774.370.4编写一个函数(程序名为data_outline.R)描述样本的各种描述性统计量。data_outline-function(x)n-length(x)m-mean(x)v-var(x)s-sd(x)me-median(x)cv-100*s/mcss-sum(x-m)2)uss-sum(x2)R-max(x)-min(x)R1-quantile(x,3/4)-quantile(x,1/4)sm-s/sqrt(n)g1-n/(n-1)*(n-2)*sum(x-m)3)/s3g2-(n*(n+1)/(n-1)*(n-2)*(n-3)*sum(x-m)4)/s4-(3*(n-1

9、)2)/(n-2)*(n-3)data.frame(N=n,Mean=m,Var=v,std_dev=s,Median=me,std_mean=sm,CV=cv,CSS=css,USS=uss,R=R,R1=R1,Skewness=g1,Kurtosis=g2,row.names=1)进入R,source(data_outline.R)#将程序调入内存serumdata-scan(3.1.txt);serumdata#将数据读入向量serumdata。data_outline(serumdata)结果如下:NMeanVarstd_devMedianstd_meanCVCSSUSSR.69615

10、.50.320R1SkewnessKurtosis14.60.要点:read.table()用于读表格形式的文件。上述形式的数据由于第七行缺几个数据,故用read.table()不能读入。scan()可以直接读纯文本文件。scan()和matrix()连用还可以将数据存放成矩阵形式。X-matrix(scan(3.1.txt,0),ncol=10,byrow=TRUE)#将上述数据放置成10*10的矩阵。scan()还可以从屏幕上直接输入数据。Yhist(serumdata,freq=FALSE,col=purple,border=red,density=3,angle=60,main=pas

11、te(thehistogramofserumdata),xlab=age,ylab=frequency)#直方图。col是填充颜色。默认空白。border是边框的颜色,默认前景色。density是在图上画条纹阴影,默认不画。angle是条纹阴影的倾斜角度(逆时针方向),默认45度。main,xlab,ylab是标题,x和y坐标轴名称。lines(density(serumdata),col=blue)#密度估计曲线。xlines(x,dnorm(x,mean(serumdata),sd(serumdata),col=green)#正态分布的概率密度曲线plot(ecdf(serumdata),

12、verticals=TRUE,do.p=FALSE)#绘制经验分布图lines(x,pnorm(x,mean(serumdata),sd(serumdata),col=blue)#正态经验分布qqnorm(serumdata,col=purple)#绘制QQ图qqline(serumdata,col=red)#绘制QQ直线Ex3.3stem(serumdata,scale=1)#作茎叶图。原始数据小数点后数值四舍五入。Thedecimalpointisatthe|64|30066|2333368|70|72|55574|8876|78|80|82|84|3boxplot(serumdata,c

13、ol=lightblue,notch=T)#作箱线图。notch表示带有缺口。fivenum(serumdata)#五数总结164.371.273.575.884.3Ex3.4shapiro.test(serumdata)#正态性Shapori-Wilk检验方法Shapiro-Wilknormalitytestdata:serumdataW=0.9897,p-value=0.6437结论:p值0.05,可认为来自正态分布的总体。ks.test(serumdata,pnorm,mean(serumdata),sd(serumdata)#Kolmogrov-Smirnov检验,正态性One-sam

14、pleKolmogorov-Smirnovtestdata:serumdataD=0.0701,p-value=0.7097alternativehypothesis:two-sidedWarningmessage:Inks.test(serumdata,pnorm,mean(serumdata),sd(serumdata):cannotcomputecorrectp-valueswithties结论:p值0.05,可认为来自正态分布的总体。注意,这里的警告信息,是因为数据中有重复的数值,ks检验要求待检数据时连续的,不允许重复值。Ex3.5yfplot(f,y,col=lightgreen)

15、#plot()生成箱线图xyzboxplot(x,y,z,names=c(1,2,3),col=c(5,6,7)#boxplot()生成箱线图结论:第2和第3组没有显著差异。第1组合其他两组有显著差异。Ex3.6数据太多,懒得录入。离散图应该用plot即可。Ex3.7studatadata.frame(studata)#转化为数据框V1V2V3V4V5V611alicef1356.584.022beckaf1365.398.033gailf1464.390.044karenf1256.377.055kathyf1259.884.566maryf1566.5112.077sandyf1151.3

16、50.588sharonf1562.5112.599tammyf1462.8102.51010alfredm1469.0112.51111dukem1463.5102.51212guidom1567.0133.01313jamesm1257.383.01414jefferym1362.584.01515johnm1259.099.51616philipm1672.0150.01717robertm1264.8128.01818thomasm1157.585.01919williamm1566.5112.0names(studata)attach(studata)#将数据框调入内存plot(we

17、ightheight,col=red)#体重对于身高的散点图coplot(weightheight|sex,col=blue)#不同性别,体重与身高的散点图coplot(weightheight|age,col=blue)#不同年龄,体重与身高的散点图coplot(weightheight|age+sex,col=blue)#不同年龄和性别,体重与身高的散点图Ex3.8xyfzcontour(x,y,z,levels=c(0,1,2,3,4,5,10,15,20,30,40,50,60,80,100),col=blue)#二维等值线persp(x,y,z,theta=120,phi=0,exp

18、and=0.7,col=lightblue)#三位网格曲面Ex3.9attach(studata)cor.test(height,weight)#Pearson相关性检验Pearsonsproduct-momentcorrelationdata:heightandweightt=7.5549,df=17,p-value=7.887e-07alternativehypothesis:truecorrelationisnotequalto095percentconfidenceinterval:0.sampleestimates:cor0.由此可见身高和体重是相关的。Ex3.10Ex3.11上述两

19、题原始数据太多,网上找不到,懒得录入。略。Ex4.2指数分布,的极大似然估计是n/sum(Xi)xlamdaxmean(x)11平均为1个。Ex4.4obj-function(x)fx0nlm(obj,x0)$minimum148.98425$estimate111.-0.$gradient11.e-08-1.e-07$code11$iterations116Ex4.5xt.test(x)#t.test()做单样本正态分布区间估计OneSamplet-testdata:xt=35.947,df=9,p-value=4.938e-11alternativehypothesis:truemeani

20、snotequalto095percentconfidenceinterval:63.6415sampleestimates:meanofx67.4平均脉搏点估计为67.4,95%区间估计为63.6415。t.test(x,alternative=less,mu=72)#t.test()做单样本正态分布单侧区间估计OneSamplet-testdata:xt=-2.4534,df=9,p-value=0.01828alternativehypothesis:truemeanislessthan7295percentconfidenceinterval:-Inf70.83705sampleest

21、imates:meanofx67.4p值小于0.05,拒绝原假设,平均脉搏低于常人。要点:t.test()函数的用法。本例为单样本;可做双边和单侧检验。Ex4.6xyt.test(x,y,var.equal=TRUE)TwoSamplet-testdata:xandyt=4.6287,df=18,p-value=0.alternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:7.06374sampleestimates:meanofxmeanofy140.6126.8期望差的95%置信区

22、间为7.06374。要点:t.test()可做两正态样本均值差估计。此例认为两样本方差相等。ps:我怎么觉得这题应该用配对t检验?Ex4.7xyt.test(x,y,var.equal=TRUE)TwoSamplet-testdata:xandyt=1.198,df=7,p-value=0.2699alternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:-0.sampleestimates:meanofxmeanofy0.13920期望差的95%的区间估计为-0.Ex4.8接Ex4.

23、6var.test(x,y)Ftesttocomparetwovariancesdata:xandyF=0.2353,numdf=9,denomdf=9,p-value=0.04229alternativehypothesis:trueratioofvariancesisnotequalto195percentconfidenceinterval:0.sampleestimates:ratioofvariances0.要点:var.test可做两样本方差比的估计。基于此结果可认为方差不等。因此,在Ex4.6中,计算期望差时应该采取方差不等的参数。t.test(x,y)WelchTwoSampl

24、et-testdata:xandyt=4.6287,df=13.014,p-value=0.alternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:7.sampleestimates:meanofxmeanofy140.6126.8期望差的95%置信区间为7.。要点:t.test(x,y,var.equal=TRUE)做方差相等的两正态样本的均值差估计t.test(x,y)做方差不等的两正态样本的均值差估计Ex4.9xntmpmean(x)11.mean(x)-tmp;mean(x)

25、+tmp11.12.平均呼唤次数为1.90.95的置信区间为1.49,2,32Ex4.10xt.test(x,alternative=greater)OneSamplet-testdata:xt=23.9693,df=9,p-value=9.148e-10alternativehypothesis:truemeanisgreaterthan095percentconfidenceinterval:920.8443Infsampleestimates:meanofx997.1灯泡平均寿命置信度95%的单侧置信下限为920.8443要点:t.test()做单侧置信区间估计Ex5.1xt.test(

26、x,mu=225)OneSamplet-testdata:xt=-3.4783,df=19,p-value=0.alternativehypothesis:truemeanisnotequalto22595percentconfidenceinterval:172.9173sampleestimates:meanofx192.15原假设:油漆工人的血小板计数与正常成年男子无差异。备择假设:油漆工人的血小板计数与正常成年男子有差异。p值小于0.05,拒绝原假设,认为油漆工人的血小板计数与正常成年男子有差异。上述检验是双边检验。也可采用单边检验。备择假设:油漆工人的血小板计数小于正常成年男子。t.

27、test(x,mu=225,alternative=less)OneSamplet-testdata:xt=-3.4783,df=19,p-value=0.alternativehypothesis:truemeanislessthan22595percentconfidenceinterval:-Inf208.4806sampleestimates:meanofx192.15同样可得出油漆工人的血小板计数小于正常成年男子的结论。Ex5.2pnorm(1000,mean(x),sd(x)10.x18pnorm(1000,mean(x),sd(x)10.xABt.test(A,B,paired=

28、TRUE)Pairedt-testdata:AandBt=-0.6513,df=7,p-value=0.5357alternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:-15.87889sampleestimates:meanofthedifferences-3.375p值大于0.05,接受原假设,两种方法治疗无差异。Ex5.4(1)正态性W检验:xyshapiro.test(x)Shapiro-Wilknormalitytestdata:xW=0.9699,p-value=0.75

29、27shapiro.test(y)Shapiro-Wilknormalitytestdata:yW=0.971,p-value=0.7754ks检验:ks.test(x,pnorm,mean(x),sd(x)One-sampleKolmogorov-Smirnovtestdata:xD=0.1065,p-value=0.977alternativehypothesis:two-sidedWarningmessage:Inks.test(x,pnorm,mean(x),sd(x):cannotcomputecorrectp-valueswithtiesks.test(y,pnorm,mean(y

30、),sd(y)One-sampleKolmogorov-Smirnovtestdata:yD=0.1197,p-value=0.9368alternativehypothesis:two-sidedWarningmessage:Inks.test(y,pnorm,mean(y),sd(y):cannotcomputecorrectp-valueswithtiespearson拟合优度检验,以x为例。sort(x)1-5.6-1.6-1.4-0.7-0.50.40.71.72.02.52.52.83.03.54.0164.54.65.86.07.1x1pp10.pchisq.test(x1,p=

31、p)Chi-squaredtestforgivenprobabilitiesdata:x1X-squared=0.5639,df=4,p-value=0.967Warningmessage:Inchisq.test(x1,p=p):Chi-squaredapproximationmaybeincorrectp值为0.967,接受原假设,x符合正态分布。(2)方差相同模型t检验:t.test(x,y,var.equal=TRUE)TwoSamplet-testdata:xandyt=-0.6419,df=38,p-value=0.5248alternativehypothesis:truedif

32、ferenceinmeansisnotequalto095percentconfidenceinterval:-2.sampleestimates:meanofxmeanofy2.0652.625方差不同模型t检验:t.test(x,y)WelchTwoSamplet-testdata:xandyt=-0.6419,df=36.086,p-value=0.525alternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:-2.20926sampleestimates:meanofxmea

33、nofy2.0652.625配对t检验:t.test(x,y,paired=TRUE)Pairedt-testdata:xandyt=-0.6464,df=19,p-value=0.5257alternativehypothesis:truedifferenceinmeansisnotequalto095percentconfidenceinterval:-2.sampleestimates:meanofthedifferences-0.56三种检验的结果都显示两组数据均值无差异。(3)方差检验:var.test(x,y)Ftesttocomparetwovariancesdata:xandy

34、F=1.5984,numdf=19,denomdf=19,p-value=0.3153alternativehypothesis:trueratioofvariancesisnotequalto195percentconfidenceinterval:0.sampleestimates:ratioofvariances1.接受原假设,两组数据方差相同。Ex5.5abks.test(a,pnorm,mean(a),sd(a)One-sampleKolmogorov-Smirnovtestdata:aD=0.1464,p-value=0.9266alternativehypothesis:two-

35、sidedks.test(b,pnorm,mean(b),sd(b)One-sampleKolmogorov-Smirnovtestdata:bD=0.2222,p-value=0.707alternativehypothesis:two-sidedWarningmessage:Inks.test(b,pnorm,mean(b),sd(b):cannotcomputecorrectp-valueswithtiesa和b都服从正态分布。方差齐性检验:var.test(a,b)Ftesttocomparetwovariancesdata:aandbF=1.9646,numdf=11,denomdf

36、=9,p-value=0.3200alternativehypothesis:trueratioofvariancesisnotequalto195percentconfidenceinterval:0.sampleestimates:ratioofvariances1.可认为a和b的方差相同。选用方差相同模型t检验:t.test(a,b,var.equal=TRUE)TwoSamplet-testdata:aandbt=-8.8148,df=20,p-value=2.524e-08alternativehypothesis:truedifferenceinmeansisnotequalto0

37、95percentconfidenceinterval:-48.24975-29.78358sampleestimates:meanofxmeanofy125.6000可认为两者有差别。Ex5.6二项分布总体的假设检验:binom.test(57,400,p=0.147)Exactbinomialtestdata:57and400numberofsuccesses=57,numberoftrials=400,p-value=0.8876alternativehypothesis:trueprobabilityofsuccessisnotequalto0.14795percentconfiden

38、ceinterval:0.sampleestimates:probabilityofsuccess0.1425P值0.05,故接受原假设,表示调查结果支持该市老年人口的看法Ex5.7二项分布总体的假设检验:binom.test(178,328,p=0.5,alternative=greater)Exactbinomialtestdata:178and328numberofsuccesses=178,numberoftrials=328,p-value=0.06794alternativehypothesis:trueprobabilityofsuccessisgreaterthan0.595p

39、ercentconfidenceinterval:0.sampleestimates:probabilityofsuccess0.不能认为这种处理能增加母鸡的比例。Ex5.8利用pearson卡方检验是否符合特定分布:chisq.test(c(315,101,108,32),p=c(9,3,3,1)/16)Chi-squaredtestforgivenprobabilitiesdata:c(315,101,108,32)X-squared=0.47,df=3,p-value=0.9254接受原假设,符合自由组合定律。Ex5.9利用pearson卡方检验是否符合泊松分布:nyxq-ppois(x,mean(rep(x,y);np1chisq.test(y,p=p)Ch

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com