R语言代码试题答案步骤_第1页
R语言代码试题答案步骤_第2页
R语言代码试题答案步骤_第3页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、R version 3.43 (2017-11-30) - "Kite-Eating Tree11Copyright (C) 2017 The R Foundation for Statistical ComputingPlatform: x86_64w64mingw3刀x64 (64-bit)R是自由软件,不带任何担保。在某些条件下你可以将其自由散布。肘license。'或licence。来看散布的详细条件。R是个合作计划,有许多人为之做出了贡献.用'contributors)来看合作者的详细情况用'citationO'会告诉你如何在出版物中正确地引

2、用R或R程序包。用Hemo来看一些示程序,用'help来阅读在线帮助文件,或用'help.start()通过HTML浏览器来看帮助文件。用'q()退出R.原来保存的工作空间已还原> h=read.csv(,header=true)Error in read.table(file = file, header = headec sep = sep, quote = quote,:找不到对象'true'> h=read.csv(,header=TRUE)>h地区 xl x2 x3 x4 x5 x6 x7 x8 x9 y12345678910

3、1112131415161718192021222324257535 2639 1971 1658 3696 84742 87475 106.5 1.3 24046 7344 1881 1854 1556 2254 61514 93173 107.5 3.6 200244211 1542 1502 1047 1204 38658 36584 104.1 3.7 125313856 1529 1439 906 1506 44236 33628 108.8 3.3 122125463 2730 1584 1354 1972 46557 63886 109.6 3.7 177175809 2042

4、1433 1310 1844 41858 56649 107.7 3.6 165944635 2045 1594 1448 1643 38407 43415 111.0 3.7 146144687 1807 1337 1181 1217 36406 35711 104.8 4.2 129849656 2111 1790 1017 3724 78673 85373 106.0 3.1 262536658 1916 1437 1058 3078 50639 68347 112.6 3.1 188257552 2110 1552 1228 2997 50197 63374 104.5 3.0 215

5、455815 1541 1397 1143 1933 44601 28792 105.3 3.7 150127317 1634 1754 773 2105 44525 52763 104.6 3.6 185935072 1477 1174 671 1487 38512 28800 106.7 3.0 127765201 2197 1572 1005 1656 41904 51768 106.9 3.3 157784607 1886 1191 1085 1525 37338 31499 106.8 3.1 137335838 1783 1371 1030 1652 39846 38572 105

6、.6 3.8 144965442 1625 1302 918 1738 38971 33480 105.7 4.2 146098258 1521 2100 1048 2954 50278 54095 107.9 2.5 22396广西 5553 1146 1377 884 1626 36386 27952 107.5 3.4 142446556 865 1521 993 1320 39485 32377 107.0 2.0 144576870 2229 1177 1102 1471 44498 38914 107.8 3.3 165736074 1651 1284 773 1587 42339

7、 29608 105.9 4.0 150504993 1399 1014 655 1396 41156 19710 105.5 33 12586 5468 1760 974 939 1434 37629 22195 108.9 4.0 1388426 5518 1362 845 467 550 51705 22936 109.5 2.6 1118427 55511789 1322 12122079 43073 38564109.4 3.2 1533328 46021631 1288 10501388 37679 21978108.6 2.7 1284729 4667 1512 1232 906

8、 1097 46483 33181 110.6 3.4 1234630 47691876 1193 10631516 47436 36394105.5 4.2 1406731 52392031 1167 10281281 44576 33796114.8 3.4 13892> Im=lm(yxl+x2+x3+x4+x5+x6+x7+x8+x9/data=h)> ImCall:lm(formula = y xl + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9,data = h)Coefficients:(Intercept)xlx2x3x4x5x6x7

9、x8x9320.6409481.3165881.6498592.178660-0.0056091.6842830.0103200.003655-19.13057650.515575> summary(lm)Call:lm(formula = y xl + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9,data = h)Residuals:MinIQ Median3QMax-940.13 -195.243.42239.00 476.06Coefficients:Estimate Std. Error t value Pr(>|t|)(In tercept

10、)3.206e+02 3.952e+030.081 0.936097xl1.317e+001.062e-0112.400 3.97e-ll*x21.650e+003.008e-015.484 1.93e-05 *x32.179e+005.199e-014.190 0.000412 *x4-5.609e-034.766e-01-0.012 0.990720x51.684e+002.142e-017.864 1.08e-07*x61.032e-021.343e-020.769 0.450665x73.655e-031.070e-020.342 0.736006x8-1.913e+013.197e+

11、01-0.598 0.555983x95.052e+011.502e+020336 0.739986Signif. codes: 00.001""0.010.054/0.1' 91Residual standard error: 389.4 on 21 degrees of freedomMultiple R-squared: 0.9923, Adjusted R-squared: 0.9889F-statistic: 298.9 on 9 and 21 DF, p-value: < 2.2e-16> pre=fitted.values(lm)>

12、res=residuals(lm) > sd(res)1 325.7967> res=residuals(lm)> dy=step(lm)Start: AIC=377.73y xl + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9Df Sum of SqRSSAIC-x41213184326 375.73-x91171493201454 375.90-x71177003202005 375.90-x81542953238599 376.26x61895863273891 376.59<none>3184305 377.7x312662

13、5935846898 394.57-x2145610567745361 403.29-x519377500 12561805 418.28-xl123314547 26498852 441.42Step: AIC=375.73 y xl + x2 + x3 + x5 + x6 + x7 + x8 + x9Df Sum of SqRSS AIC-x9117428 3201754 373.90x7185633202889 373.91-x81544373238763 374.26x61918133276139 374.61<none>3184326 375.73x31293613061

14、20456 393.99-x2154679418652267 404.72-x519393345 12577671 416.32-xl125886086 29070412 442.29Step: AIC=373.9 y xl + x2 + x3 + x5 + x6 + x7 + x8Df Sum of SqRSSAIC-x71346343236387 372.24x61748003276554 372.62-x81821503283904 372.69<none>3201754 373.90x3130553536257107 392.67-x2157258368927590 403

15、.69-x519382624 12584378 414.33-xl125868832 29070586 440.29Step: AIC=372.24y xl + x2 + x3 + x5 + x6 + x8x870813 3307201370.911527773389165 371.67<none>3236387 372.245501284 8737672 401.02x28895049 12131436 411.20x59458098 12694485 412.60127733098 30969486 440.25Step: AIC=370.91y xl + x2 + x3 +

16、x5 + x6Df Sum of SqRSS AIC137540 3444741370.17<none>3307201 370.915771063 9078264 400.21x28871193 12178394 409.32x59473521 12780722 410.81128248162 31555363 438.83Step: AIC=370.17 y xl + x2 + x3 + x5Df Sum of SqRSS AIC<none>3444741 370.1715717883 9162624 398.50-x211024981595

17、-x511099831360-xl133258637 36703378441.52> summary(dy)Call:lm(formula = y xl + x2 + x3 + x5 data = h)Residuals:Min IQ Median 3Q Max-943.18 -161.0512.74 250.93 566.25Coefficients:Estimate Std. Error t value Pr(>|t|)(Intercept) -1694.6269562.9773-3.010 0.00574 *xl1.36420.0861x21.767

18、90.2010x32.28940.3485x51.74240.1912Signif. codes: 00.00115.844 7.11e-15 *8.796 2.86e-09*6.569 5.76e-07*0.010.054/0.1' 91Residual standard error: 364 on 26 degrees of freedomMultiple R-squared: 0.9916, Adjusted R-squared: 0.9903F-statistic: 769.2 on 4 and 26 DF, p-value: < 2.2e-16 newdata=data

19、.frame(xl=5200zx2=2000/x3=1100/x4=1000,x5=1300,x6=45000/x7=34000,x8=115.0,x9=3.8)> predict(dy/newdata/interval=,confidenee”)fitIwrupr1 13718.67 13468.98 13968.36>1、乘性误差项,模型形式为y=AKaL/ee对乘性误差项,模型可通过两边収对数转化成线性模型lny = In A + alnK + 01nZz + £令y = In y , /?0 =lnA, x = In K , x2 = In L,则转化为线性回归方

20、程 y = 0o + d + px2 + £以下我们用线性化的乘性误差额模型拟合C-D牛产函数» y二36243962.9 4124.2 4330.6 46235080.2 5977.3 6836.3 7305.4 7983.2 8385.9 8049.78564.3 9653.5 11179.9 12673.0 13786.9 14724.3 15782.8 16840.6 17861.6 18975.9 20604.7 22256.0 24247.0T;» K司 1377.9 1446.7 1451.5 14081536.9 1716.4 2057.7 258

21、2.2 2754.0 2884.3 3086.8 2901.52975.4 3356.R 4044.2 54R7.9 5679.0 6012.() 6246.5 6436.0 6736.1 7098.9 7510.5 85673 9764.9; » L=40152 41024 42361 43725 45295 46436 48197 49873 51282 52783 54334 55329 64749 65491 66152 66808 67455 6Rf)65 6R950 69820 70637 71394 72085 73025 7374()'»yl=log

22、y);» x1=log(K);» x2=log(L);» x=ones(25,l) xl x2;» b,bint/,rintstats=regress(y 1 ,x);» b,bint,stats,rcoplot(r,rint)b =-1.78540.80110.4016bin( =476870.68550.04781.1978().91670.7554stats = 1.0e+003 *0.00101.80550» exp(-1.7854) ans =0.1677» y二36243962.9 4124.2 4330.6 4

23、6235080.2 59力.3 6836.3 7305.4 7983.2 8385.9 8049.716840.6 17861.6 18975.9 20604.78564.3 9653.5 11179.9 12673.0 13786.9 14724.3 15782.822256.0 24247.0:»yl=-l .7854+0.80 lPxI +0.4016 恢 2;» y2=exp(yl);» c=y-y2c = 1 .0c4003 * (-0.25100.1014-0.00370.25020.18390.18160.2274-0.1560-0.14000.16

24、69003640.0461-0.13450.02830.0396-1.7117-1.0550-0.8671-0.37750.20510.52680.81951.53610.95590.5003)参数参数估计值参数的置信区间A-1.7854-4.7687,1.1978a0.80110.6855,0.9167B0.40160.0478,0.7554R' = 1.0000F = 1805.5p = 0.0000得两个弹性系数为6-0.8011,0-0.4016,资金的贡献率大于劳动者的贡献率c规模报酬«+=1.2027>1表示规模报酬递增,效率系数A=_L78M=0. 167

25、7o其中系数0的显著性概 率P值二0000,显菁性较强。得乘性误差项的C-D牛产函数为y = 0J6770.40162、加性误差项,模型形式为y = AKaL£对加性误差项模型,不能通过变量变换转化成线性模型,只能用非线性最小二乘求解未知参 数。以上面乘性误差项的参数为初值做非线性最小二乘首先编写一个M文件Huaxuel.mfunction yhat=huaxue 1 (beta,x)b l=beta(l );b2=beta(2);b3=beta(3);xl=x(:,l);x2=x(:,2);yhat=b 1 *(x 1 Ab2)*(x2Ab3)然后在MATLAB命令窗口输入:

26、87; xl=I377.9 1446.7 1451.5 1408.1 1536.9 1716.4 2057.7 2582.2 2754.0 2884.3 3086.8 2901.52975.4 3356.8 4044.2 5487.9 5679.0 6012.0 6246.5 6436.0 6736.1 7098.9 7510.5 8567.3 9764.9;» x2二40152 41024 42361 43725 45295 46436 48197 49873 51282 52783 54334 55329 64749 65491 66152 66808 67455 68()65

27、6895() 69820 70637 71394 72085 73025 73740J;» y二3624.1 3962.9 4124.2 4330.6 46235080.2 5977.3 6X36.3 7305.4 7983.2 8385.9 8049.7 8564.3 9653.5 11179.9 12673.0 13786.9 147243 15782.8 16840.6 17861.6 18975.9 20604.7 22256.0 24247.01;» x=xl x2;» beta=-I.7S54 0.8011 0.4016;» betahat,

28、f,J=nlinfit(x,y/huaxue I beta);DW检验第一步:计算出回归估计式的残差crn工(® -弓-1)2A第二步;定义DW统计竜为DW二 ,其中©二叫一”/=2认为立/=2与近似相等,得DW =2 心2,p =nK etet- t=2“ 2 S C; t=2DW 2n-p)» ci= l.()c+0()3*-0.2510-0.1014-O.(X)370.25020.18390.18160.22740.1560-0.140006690.03640.0461013450.0283-0.0396-1.71171.0550-0.8671-0.3775

29、I).20510.52680.81951.53610.9559J;» ej=1.0e+003 *-0.1014-0.00370.25020.18390.18160.227401560-0.14000.16690.03640.0461-0.13450.0283-0.0396-1.7117-1.0550-0.86710.37750.20510.52680.81951.53610.95590.50031;» scatter®©) » sum(ei.*ej)/sum(ei.A2)ans =0.7212» 2*(l-sum(ei.*ej)/su

30、m(ei.A2)ans =0.5576» sum( (ei-ej).A2)/sum(ei.A2)ans =0.5772且查表得出:当 n=25, k=2 时,dL =1.29, =1.45 ,即有 0<DM=0.5772< dL =1.29,可以认为误差项存在正自相关。> h=ts(read.csv(,zheader=TRUE)>hTime Series:Start = 1End = 56Freque ncy = 1X781J -582,53-634,1366,-16-14&3-7410,89HU-4812,-1413,3214,5615,-8616,

31、-6617,501&2619,5920,-4721,-8322,223,24,12425-1062611327,-762&-4729,-3230,3931,-3032,633,-73珂1835,236,-2437,23卩&-3839,9140,-5641,-5842,143,1444,445,7746, -12747,9748,1049,-2850,-17512352-253,4854, -13155,6556-17 > plot(h/type=,oH)R R Graphics: Devke 2 (ACTIVE)Time> local(pkg <- s

32、elect.Iist(sort(.packages(all.available = TRUE)启raphics二TRUE)+ if(nchar(pkg) library(pkg, character.only=TRUE)Warning message:程辑包'urea'是用R版本344来建造的> adf=ur.df(as.vector(h)/type=c(,drift,)/selectlags=c(,'AICH)> summary(adf)# # Augmented Dickey-Fuller Test Unit Root Test #Test regres

33、sion driftCall:lm(formula = z.diff z. lag.l + 1 + 乙 diff.lag)Residuals:Min IQ Median 3Q Max-96.191-23.390 -0.5811&446 133.241Coefficients:Signif. codes: 0*0.001十'0.010.05 T 0.1Residual standard error: 50.89 on 51 degrees of freedomMultiple R-squared: 0.7589” Adjusted R-squared: 0.7494F-stati

34、stic: 80.25 on 2 and 51 DF, p-value: < 2.2e-16Estimate Std. Error t value Pr(>|t|)(In tercept)-9.43817.0489-1.3390.187z. lag.l-1.78370.2386-7.476 9.65e-10*z. diff.lag0.19560.13791.4180.162Value of test-statistic is: -7.4761 27.9471Critical values for test statistics: lpct 5pct lOpcttau2 -3.51

35、-2.89 -2.58phil 6.70 4.713.86> acf(h)X78Lag> pacf(h)LLOV 嚅dagar=sarima(h/l,0,4,details=F)ar$fitCall:stats:arima(x = xdata, order = c(p, d, q), seasonal = list(order = c(R D,Q), period = S), xreg = xmean” include.mean = FALSE, optim.control = list(trace = trc, REPORT =1, reltol = tol)Coefficien

36、ts:arlmalma2ma3ma4xmean-0.0957 -0.7605 -0.051-0.25910.0706-5.0886se 0.73180.72440.6370.20130.19390.4252sigmaA2 estimated as 1850: log likelihood =:-291.97,aic = 597.95$degrees_of_freedom1 50$ttableEstimate SE t.value p.valuearl-0.0957 0.7318-0.13080.8965mal-0.7605 0.7244-1.04980.2988ma2-0.0510 0.637

37、0-0.08000.9365ma3-0.2591 0.2013-1.28750.2038ma40.0706 0.19390.36410.7173xmean -5.0886 0.4252 -11.9668 0.0000$AIC1 8.73734$AICc1 8.814721$BIC1 7.954342> ma=sarima(h/O,l/l/details=F)> ma$fitCall:stats:arima(x = xdata, order = c(p, d, q), seasonal = list(order = c(P D,Q), period = S), xreg = cons

38、tant, optim.control = list(trace = trc, REPORT = reltol = tol)Coefficients:malconstant-1.00000.1275s.e. 0.04520.4833sigmaA2 estimated as 3412: log likelihood = -303.77, aic = 613.53$degrees_of_freedom 1 53$ttableEstimate SE t.value p.valuemal-1.0000 0.0452 -22.13900.000constant0.1275 0.48330.26380.7

39、93$AIC1 9.206399$AICc1 9.250355$BIC1 8.278733> arma=sarima(h,l/l/l/details=F)> arma$fitCall:stats:arima(x = xdata, order = c(p, d, q), seasonal = list(order = c(R D,Q), period = S), xreg = constant, optim.control = list(trace = trc, REPORT = 1,reltol = tol)Coefficients:mal constant-0.4893 -1.00000.1052s.e. 0.11610.04690.2858ic = 600.53sigmaA2 estimated as 2548: log likelihood = -296.27,$degrees_of_freedom1 52$ttableEstimate SE t.value p.valuearl-0.4893 0.1161-4.21270.0001mal-1.0000 0.0469 -2132070.0000constant0.1052 0.28580.36800.714

温馨提示

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

最新文档

评论

0/150

提交评论