2011研究生R语言考题

上传人:文*** 文档编号:89467621 上传时间:2022-05-13 格式:DOC 页数:21 大小:202KB
收藏 版权申诉 举报 下载
2011研究生R语言考题_第1页
第1页 / 共21页
2011研究生R语言考题_第2页
第2页 / 共21页
2011研究生R语言考题_第3页
第3页 / 共21页
资源描述:

《2011研究生R语言考题》由会员分享,可在线阅读,更多相关《2011研究生R语言考题(21页珍藏版)》请在装配图网上搜索。

1、暨 南 大 学 考 试 试 卷教师填写2010 - 2011_ 学年度第_2_学期课程名称: 数据分析与R语言应用 授课教师姓名:_王斌会_ 考试时间:_2011_年_11_月_8_日课程类别必修 选修 考试方式开卷 闭卷 试卷类别(A、B) A 共 4 页考生填写 经济学院 学院(校) 数量经济学 专业 班(级)姓名 刘伟 学号 1130111008 内招 外招 题 号一二三四五六七八九十总 分得 分得分评阅人一、统计图表(共1小题,共20分)1应用R图表对各类产品供货走势图分析类别月份123456789101112彩电A1冰箱A2空调A3洗衣机A4(1) 要求:数据由R随机数函数生成,产生

2、20,50间的均匀随机数。解:首先对R进行初始化,设定参数,再生成随机数,代码如下:rm(list=ls()options(digits=4)par(mar=c(4,4,2,1)+0.1,cex=0.75)A1=runif(12,20,50);A1A2=runif(12,20,50);A2A3=runif(12,20,50);A3A4=runif(12,20,50);A4(2)分析(图形要进行一定修饰):1)绘制各类产品的月份趋势线图。解:趋势线图如下代码如下:par(mfrow=c(2,2)plot(A1,type=l,ylab=销售量,xlab=月份,main=彩电(A1),xlim=c(

3、1,12),ylim=c(0,50)plot(A2,type=l,ylab=销售量,xlab=月份,main=冰箱(A2),xlim=c(1,12),ylim=c(0,50)plot(A3,type=l,ylab=销售量,xlab=月份,main=空调(A3),xlim=c(1,12),ylim=c(0,50)plot(A4,type=l,ylab=销售量,xlab=月份,main=洗衣机(A4),xlim=c(1,12),ylim=c(0,50)2)绘制各类产品的季度的柱形图。解:首先对数据进行整理,得出各自的季度数据。柱状图如下代码如下:dat=data.frame(A1,A2,A3,A4

4、)q1=c(dat1,1+dat2,1+dat3,1,dat4,1+dat5,1+dat6,1,dat7,1+dat8,1+dat9,1,dat10,1+dat11,1+dat12,1)q2=c(dat1,2+dat2,2+dat3,2,dat4,2+dat5,2+dat6,2,dat7,2+dat8,2+dat9,2,dat10,2+dat11,2+dat12,2)q3=c(dat1,3+dat2,1+dat3,3,dat4,3+dat5,1+dat6,3,dat7,3+dat8,3+dat9,3,dat10,3+dat11,3+dat12,3)q4=c(dat1,4+dat2,4+dat3

5、,4,dat4,4+dat5,4+dat6,4,dat7,4+dat8,4+dat9,4,dat10,4+dat11,4+dat12,4)dat1=data.frame(q1,q2,q3,q4);dat1par(mfrow=c(2,2)barplot(dat1,1,xlab=季度,ylab=销售量,main=彩电(A1),ylim=c(0,150)barplot(dat1,2,xlab=季度,ylab=销售量,main=冰箱(A2),ylim=c(0,150)barplot(dat1,3,xlab=季度,ylab=销售量,main=空调(A3),ylim=c(0,150)barplot(dat

6、1,4,xlab=季度,ylab=销售量,main=洗衣机(A4),ylim=c(0,150)3)绘制各类产品的年度的饼图。解:饼图如下代码如下:par(mfrow=c(1,1)y1=dat11,1+dat12,1+dat13,1+dat14,1y2=dat11,2+dat12,2+dat13,2+dat14,2y3=dat11,3+dat12,3+dat13,3+dat14,3y4=dat11,4+dat12,4+dat13,4+dat14,4x=c(y1,y2,y3,y4)pie(x,labels=c(彩电(A1),冰箱(A2),空调(A3),洗衣机(A4),col=c(red,green

7、,purple,blue)得分评阅人二、统计检验(共2小题,每题10分,共20分)1. 两台铣床生产同一种型号的套管,平日两台铣床加工的套管内槽深度都服从正态分布N(10,0.32)和N(8,0.22),从这两台铣床的产品中分别抽出13个和15个,请分别按方差已知和未知检验两台产品的深度是否不同(=0.05)?(1)两台铣床的产品内槽精度(方差)有无显著差别?解: x=rnorm(13,10,0.3)y =rnorm(15,8,0.2)var.test(x,y) F test to compare two variancesdata: x and y F = 3.899, num df = 1

8、2, denom df = 14, p-value =0.01785alternative hypothesis: true ratio of variances is not equal to 1 95 percent confidence interval: 1.278 12.502 sample estimates:ratio of variances 3.899由于 p-value =0.017850.05,故两台铣床的产品内槽精度(方差)有显著差别。(2) 两台产品的的深度是否不同? 解:1、方差未知时 t.test(x,y)Welch Two Sample t-testdata:

9、x and y t = 17.61, df = 17.2, p-value = 1.934e-12alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: 1.605 2.042 sample estimates:mean of x mean of y 9.982 8.159 由于p-value = 1.934e-120.05,故两台产品的的深度是不同的。 2、方差已知时 u.test=function(x,y,sigmax,sigmay) nx=leng

10、th(x) ny=length(y) xbar=mean(x) ybar=mean(y) u=(xbar-ybar)/sqrt(sigmax2/nx+sigmay2/ny) p=pnorm(u,lower.tail=F) c(u=u,p=p) u.test(x,y,0.3,.02) u p 2.187e+01 2.332e-106由于p=2.332e-1060.05, 故两台产品的的深度是不同的。2. 如果还有一台铣床生产同一种型号的套管,其加工的套管内槽深度都服从正态分布N(12,0.42),从这台铣床的产品中抽出18个,请分别按方差已知和未知检验三台产品的深度是否不同(=0.05)?(1)

11、、方差已知的情况 x1=rnorm(13,10,0.3)x2 =rnorm(15,8,0.2)x3=sample(rnorm(1000,12,0.4),18)n1=length(x1)n2=length(x2)n3=length(x3)se1=sqrt(0.32/n1+0.22/n2)se2=sqrt(0.32/n1+0.42/n3)se3=sqrt(0.22/n2+0.42/n3)x1bar=mean(x1) x2bar=mean(x2)x3bar=mean(x3) u1=(x1bar-x2bar)/se1 u2=(x1bar-x3bar)/se3 u3=(x2bar-x3bar)/se3c

12、hi=u12+u22+u32 p=2*pchisq(chi,3,lower.tail = F);p P a=0.05,所以拒绝三台产品的的深度相等的假设,三台台产品的深度不等。 (2)、方差未知的情况y=c(x1,x2,x3) group=c(rep(1,13),rep(2,15),rep(3,18) oneway.test(ygroup) One-way analysis of means (not assuming equal variances)data: y and group F = 564.7, num df = 2.00, denom df = 28.28, p-value 2.

13、2e-16 P=t1j)/1000 #计算矩阵u每行中的数据大于t1中对应t值绝对值的概率,并将值赋予p_permutations数组ID=c(1:100) # 构造row number result=data.frame(ID,t, p_theoretical, p_permutations) #输出结果 ID t p_theoretical p_permutations1 1 -2.8853207 0.02034 0.0152 2 -1.3143173 0.22517 0.2273 3 -2.6940739 0.02732 0.0064 4 -1.3802458 0.20485 0.228

14、5 5 0.0473332 0.96341 0.9776 6 0.9959910 0.34842 0.2917 7 -0.4321985 0.67701 0.7228 8 -0.4268158 0.68077 0.6789 9 -0.1412908 0.89113 0.89910 10 1.3169132 0.22434 0.25411 11 -0.0489858 0.96213 0.98812 12 1.4028308 0.19826 0.16913 13 1.1741980 0.27408 0.25614 14 -1.0676853 0.31682 0.31715 15 2.6462644

15、 0.02943 0.01216 16 -1.9388080 0.08851 0.06117 17 -0.6731468 0.51982 0.53618 18 -0.3519451 0.73397 0.75519 19 0.5819389 0.57663 0.57720 20 0.6656648 0.52435 0.57721 21 -0.1460266 0.88751 0.74822 22 -2.7035412 0.02693 0.02323 23 -0.7900807 0.45226 0.47224 24 -0.4385279 0.67260 0.62925 25 0.3676752 0.

16、72265 0.74226 26 2.2177703 0.05738 0.03527 27 -0.1415220 0.89096 0.91828 28 -0.0004833 0.99963 1.00029 29 -0.4102665 0.69238 0.68730 30 0.0448795 0.96530 0.94431 31 1.4521009 0.18454 0.19332 32 0.2854683 0.78254 0.79333 33 0.1298656 0.89988 0.89634 34 -0.9887282 0.35175 0.34635 35 -1.4024164 0.19838

17、 0.15836 36 0.1285964 0.90085 0.85037 37 3.0086702 0.01685 0.03238 38 0.6787512 0.51645 0.50339 39 1.0937833 0.30589 0.33140 40 1.9842917 0.08250 0.10241 41 -0.0968299 0.92524 0.96942 42 -3.1236870 0.01415 0.01643 43 0.6596542 0.52801 0.50144 44 1.2064446 0.26211 0.26745 45 -0.2720078 0.79250 0.8124

18、6 46 -0.1762814 0.86445 0.86047 47 -0.2106570 0.83842 0.87548 48 1.9866318 0.08220 0.07249 49 -0.0020090 0.99845 1.00050 50 -2.0646728 0.07283 0.03151 51 -0.2564763 0.80406 0.79752 52 2.8462192 0.02160 0.02853 53 -0.0657272 0.94921 0.96654 54 0.6318659 0.54510 0.58955 55 0.7555194 0.47159 0.42556 56

19、 1.5005975 0.17185 0.17957 57 1.2630394 0.24214 0.24358 58 -0.6921399 0.50844 0.53959 59 -1.3837198 0.20382 0.19160 60 0.4838255 0.64148 0.63961 61 -0.1140807 0.91198 0.94462 62 1.1902273 0.26808 0.25863 63 0.2112508 0.83797 0.83664 64 0.8978211 0.39550 0.37365 65 -0.4111239 0.69177 0.71266 66 3.166

20、8091 0.01326 0.01667 67 1.6002204 0.14822 0.14268 68 0.8488613 0.42063 0.39769 69 0.7102140 0.49775 0.49370 70 0.0780357 0.93972 0.93071 71 1.2471030 0.24763 0.25972 72 0.9615742 0.36442 0.33073 73 1.2386493 0.25059 0.27274 74 -0.6696054 0.52196 0.53775 75 1.2152901 0.25890 0.25476 76 -0.1259623 0.9

21、0287 0.92477 77 -0.0791718 0.93884 0.96478 78 -3.6579469 0.00642 0.01679 79 0.2499050 0.80896 0.78080 80 -2.3661564 0.04552 0.05281 81 0.0254582 0.98031 0.98882 82 -1.9755606 0.08362 0.08583 83 0.0015678 0.99879 1.00084 84 0.1471918 0.88662 0.87185 85 0.0295263 0.97717 0.98486 86 0.2628059 0.79934 0

22、.80987 87 0.0846366 0.93463 0.92888 88 -0.5474582 0.59900 0.63589 89 -2.7182033 0.02632 0.01390 90 0.5733288 0.58218 0.67891 91 2.3551061 0.04631 0.04892 92 0.7709098 0.46292 0.43593 93 -0.4178680 0.68703 0.63494 94 0.2916059 0.77801 0.73695 95 0.2728228 0.79190 0.76296 96 1.5964084 0.14906 0.15597

23、97 1.3246252 0.22188 0.26698 98 0.6856973 0.51228 0.51199 99 -1.2272690 0.25461 0.248100 100 0.5488290 0.59810 0.5912.) Plot the distribution (see hist) of the resulting vector of t-scores obtained at step 1a) after excluding the first element (corresponding to the first row) and on the same graph s

24、how a vertical line for the t-value of the first row. 解:t2=t2:100hist(t2,main=t值分布的直方图)abline(v=t1)2. Olympic MedalsDuring both summer and winter Olympic games the medal table is often of interest to spectators and the media. The medal table is a tally of the number of medals which have been won by

25、each participating country during the games. A good performance on the medal table is often a source of pride for a country. However, it is to be expected that large countries will win more medals than smaller countries, due to the fact that they have a larger pool from which to recruit athletes. Th

26、us smaller countries often argue that a better measure of performance would be medals per capita. However, it is possible that medal tally shouldnt be expected to increase in direct proportion to population. Further, it is reasonable to think that the medal tally will also depend on the resources av

27、ailable to athletes in a country, or on the climate (for example, access to snow).The objective of this analysis is to explore the relationship between a countrys medal tally, population size, wealth (measured by GDP) and climate (approximated by latitude). Further, it is proposed that in future a s

28、tandardised measure of a countrys medal tally should be developed which corrects for population size, climate and wealth. Your should investigate the feasibility of this proposal, and discuss your finndings.The file medals.RDataload(medals.RData) in R is an R data frame with one row for every countr

29、y that has won at least one olympic medal in the previous four Olympic Games.The variable descriptions are as follows:Country Name of the competing country (only countries which have won at least one medal since 2004 are included).Latitude Latitude of the capital city.Summer2004 Total number of meda

30、ls (gold, silver and bronze) won at the Summer Olympics in 2004.Summer2008 Total number of medals won at the Summer Olympics in 2008.Winter2006 Total number of medals won at the Winter Olympics in 2006.Winter2010 Total number of medals won at the Winter Olympics in 2010.Population2007 The population

31、 in 2007 (source: World Fact Book).GDP2009 Gross Domestic Product in billions of US dollars (source: World Fact Book).一、引言众所周知,历届奥运会都以获得的奖牌的总数来衡量一个国家的体育发展水平,同时获得更多的奖牌也成为一个国家的骄傲。然而,不同的国家所处气候纬度、人口规模、GDP总量等因素是不同的,而仅仅靠奖牌的总量来衡量一国的体育水平显然不够全面,也不太公正。本文旨在找出与影响获得奖牌数的一些因素,如:气候(用纬度表示)、人口规模、GDP总量,通过相关分析和回归分析研究它们

32、的内在关系,并在此基础上提出更加全面的衡量一国体育发展水平的新指标。二、相关性分析 为了分析各个国家的金牌数与其气候(纬度表示)、人口规模和收入(GDP)的关系,首先对其进行相关分析。以下就通过相关系数矩阵和散点图考察它们之间的关系。1、2008年夏季奥运会奖牌数及其影响因素相关分析fix(medals) #对原始数据进行适当编辑,用负数表示南纬,朝鲜的GDP为280亿美元,以此填充NALL值x1=medals$Summer2008;x1x2=medals$Winter2010;x2x3=medals$Winter2006;x3x4=medals$Sumer2004;x4y1=medals$L

33、atitude;y1y2=medals$Population;y2y3=medals$GDP;y3A=data.frame(x1,y1,y2,y3);Acor(A) x1 y1 y2 y3x1 1.0000000 0.16387884 0.48614648 0.6624589y1 0.1638788 1.00000000 0.01097671 0.1246518y2 0.4861465 0.01097671 1.00000000 0.1685161y3 0.6624589 0.12465175 0.16851611 1.0000000 par(mfrow=c(1,3)plot(y1,x1)pl

34、ot(y2,x1)plot(y3,x1)由相关系数矩阵可知,2008年各个国家的金牌数与其所处的维度、人口规模和GDP存在正相关性,它们的相关系数分别为0.164、 0.486、 0.662,并且奖牌数与人口规模和GDP中度相关,与维度轻度相关。从三点图可以发现以下几个现象。第一,获得奖牌的国家基本上分布于北纬0到北纬60之间,只有11个获得奖牌的国家位于南半球,且获得奖牌较多的国家基本上位于北纬30到北纬50,即亚热带和温带地区,这与现实所表现出来的是一致的,这是因为分布在南半球的参与奥运会的国家相对较少,自然获得的金牌也少。第二,获得奖牌数与人口规模显然正相关。人口规模越大,获得奖牌数越多

35、,中国和美国是典型,但印度除外。第三,获得奖牌数与GDP也是正相关的,GDP规模越大,获得奖牌数越多。2、2010年冬季奥运会奖牌数及其影响因素相关分析cor(B) x2 y1 y2 y3x2 1.0000000 0.31292367 0.17071234 0.6459346y1 0.3129237 1.00000000 0.01097671 0.1246518y2 0.1707123 0.01097671 1.00000000 0.1685161y3 0.6459346 0.12465175 0.16851611 1.0000000plot(y1,x2)plot(y2,x2)plot(y3,

36、x2)从相关系数矩阵来看,2010年冬季奥运会奖牌数与纬度、人口规模和GDP有这正相关性,相关系数分别为0.3129、0.1707、0.6459。从散点图来看,2010年冬季奥运会获得奖牌的国家出澳大利亚外都位于北纬40与北纬65之间。这与2008年夏季运动会获奖牌国家的纬度相比更加偏北。这是因为冬季奥运会时,南半球国家正值夏季,很多冬季运动项目不宜开展。就奖牌数与人口规模和GDP的关系,与2008年夏季运动会相比变化不大。3、2006年冬季奥运会奖牌数及其影响因素相关分析cor(C) x3 y1 y2 y3x3 1.0000000 0.33983286 0.15271725 0.491000

37、2y1 0.3398329 1.00000000 0.01097671 0.1246518y2 0.1527172 0.01097671 1.00000000 0.1685161y3 0.4910002 0.12465175 0.16851611 1.0000000 plot(y1,x3)plot(y2,x3)plot(y3,x3) 从相关系数矩阵和散点图可以看到,2006年冬季奥运会奖牌数与纬度、人口规模和GDP的关系与2010年冬季奥运会相似。4、2004年夏季奥运会奖牌数及其影响因素相关分析cor(D) x4 y1 y2 y3x4 1.00000000 0.05056714 0.2911

38、7180 0.5879380y1 0.05056714 1.00000000 0.01097671 0.1246518y2 0.29117180 0.01097671 1.00000000 0.1685161y3 0.58793800 0.12465175 0.16851611 1.0000000 plot(y1,x4)plot(y2,x4)plot(y3,x4) 由相关系数矩阵可以看到,2004年夏季奥运会奖牌数与纬度、人口规模和GDP的关系与2008年夏季奥运会相似。 通过相关分析我们可以知道,一个国家在奥运会中获得奖牌的数量和这个国家所处的纬度、人口规模、GDP总量都是有关系的,并且与人

39、口规模和GDP总量的还有较为密切的关系。而冬奥会与夏奥会的奖牌数分布于纬度(也就是气候)有密切联系,冬奥会获得奖牌的除个别的国家外都是处在北半球,而夏奥会有十多个国家分布在南半球。这与我们现实看到的现象是相符合的,也是可以解释的。这是因为,冬奥会的气候和冬奥会的项目都是有利于北半球的国家;夏奥会获得奖牌的国家比冬奥会多,但又比北半球的国家少得多,这是因为,参加奥运会的大多数国家都处于北半球,自然获得的奖牌数就多。我们还可以看到一个有趣的现象是:获得奖牌国家有大约75%都分布在北纬20到北纬60,也就是亚热带和温带地区;有大约50%分布在北纬40到北纬60,也就是温带地区。因此地处气候不适很严酷

40、的地区的国家的更可能获得更多的奖牌。还可以看出的是,不论是冬奥会还是夏奥会人口规模大的和GDP总量大的国家获得奖牌数都多些,比如美国、中国、俄罗斯、日本等。所以,有的人要求调整奖牌板排名的规则和衡量标准是有一定道理的。不应当仅仅看奖牌总数,还要考虑某国家所处的纬度(气候条件)、人口规模(这个指标很重要)、GDP总量(财富的代表)。三、回归分析由以上的相关性分析可知,2006年冬季奥运会奖牌数与纬度、人口规模和GDP的关系与2010年冬季奥运会相似;2004年夏季奥运会奖牌数与纬度、人口规模和GDP的关系与2008年夏季奥运会相似。故只要分别做2010年冬季奥运会奖牌数的回归模型和2008年夏季

41、奥运会奖牌数回归模型即可。以下就2010年冬季奥运会最回归模型。为了消除异方差对各个变量取对数,进行线性回归拟合。代码及其结果如下:fm=lm(log(x2)y1+log(y2)+log(y3);fmCall:lm(formula = x2 y1 + log(y2) + log(y3)summary(fm)Residuals: Min 1Q Median 3Q Max -8.7247 -3.4481 -0.5701 1.1281 24.9531 Coefficients: Estimate Std. Error t value Pr(|t|) (Intercept) -10.92600 7.0

42、5118 -1.550 0.124885 y1 0.05792 0.02384 2.430 0.017165 * log(y2) 0.38423 0.45201 0.850 0.397641 log(y3) 1.39304 0.35328 3.943 0.000162 *-Signif. codes: 0 * 0.001 * 0.01 * 0.05 . 0.1 1 Residual standard error: 5.741 on 87 degrees of freedomMultiple R-squared: 0.3052, Adjusted R-squared: 0.2812 F-stat

43、istic: 12.74 on 3 and 87 DF, p-value: 5.629e-07library(DAAG) #调用DAAG包vif(fm) #计算方差膨胀因子用以判断时候存在严重多重共线性各系数的方差膨胀因子 y1 log(y2) log(y3) 2.335 1.587 2.139library(car)durbin.watson(fm) #计算拟合模型的DW值DW= 1.743由以上分析可知,模型的F值很大,相应的p值很小,因此模型的各个系数是显著的。有各个系数的t值来看,log(y3)(GDP总值的对数)非常显著;y1(纬度也即气候)的系数显著;log(y2)(人口规模的对数)的系数不显著,t值小,其p值为0.398,但也可以这样解释,即在0.4的检验水平上其系数是显著的。同时也可以看到,模型的DW值接近2,故不存在自相关性。通过方差膨胀因子的分析,变量间也不存在严重的多重共线性。因此模型的拟合还是较为合适的。三、新指标构建显然仅仅依靠奖牌总数来衡量一个国家的体育发展水平是有缺陷的。由以上分析可知,影响奖牌数的因素从大到

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


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