R语言代码试题答案步骤.docx
《R语言代码试题答案步骤.docx》由会员分享,可在线阅读,更多相关《R语言代码试题答案步骤.docx(20页珍藏版)》请在冰豆网上搜索。
R语言代码试题答案步骤
Rversion3.4.3(2017-11-30)--"Kite-EatingTree"
Copyright(C)2017TheRFoundationforStatisticalComputing
Platform:
x86_64-w64-mingw32/x64(64-bit)
R是自由软件,不带任何担保。
在某些条件下你可以将其自由散布。
用'license()'或'licence()'来看散布的详细条件。
R是个合作计划,有许多人为之做出了贡献.
用'contributors()'来看合作者的详细情况
用'citation()'会告诉你如何在出版物中正确地引用R或R程序包。
用'demo()'来看一些示程序,用'help()'来阅读在线帮助文件,或
用'help.start()'通过HTML浏览器来看帮助文件。
用'q()'退出R.
[原来保存的工作空间已还原]
>h=read.csv("F:
//1.csv",header=true)
Errorinread.table(file=file,header=header,sep=sep,quote=quote,:
找不到对象'true'
>h=read.csv("F:
//1.csv",header=TRUE)
>h
地区x1x2x3x4x5x6x7x8x9y
1753526391971165836968474287475106.51.324046
2734418811854155622546151493173107.53.620024
3421115421502104712043865836584104.13.712531
438561529143990615064423633628108.83.312212
5546327301584135419724655763886109.63.717717
6580920421433131018444185856649107.73.616594
7463520451594144816433840743415111.03.714614
8468718071337118112173640635711104.84.212984
9965621111790101737247867385373106.03.126253
10665819161437105830785063968347112.63.118825
11755221101552122829975019763374104.53.021545
12581515411397114319334460128792105.33.715012
1373171634175477321054452552763104.63.618593
1450721477117467114873851228800106.73.012776
15520121971572100516564190451768106.93.315778
16460718861191108515253733831499106.83.113733
17583817831371103016523984638572105.63.814496
1854421625130291817383897133480105.74.214609
19825815212100104829545027854095107.92.522396
20广西55531146137788416263638627952107.53.414244
216556865152199313203948532377107.02.014457
22687022291177110214714449838914107.83.316573
2360741651128477315874233929608105.94.015050
2449931399101465513964115619710105.53.312586
255468176097493914343762922195108.94.013884
26551813628454675505170522936109.52.611184
27555117891322121220794307338564109.43.215333
28460216311288105013883767921978108.62.712847
2946671512123290610974648333181110.63.412346
30476918761193106315164743636394105.54.214067
31523920311167102812814457633796114.83.413892
>lm=lm(y~x1+x2+x3+x4+x5+x6+x7+x8+x9,data=h)
>lm
Call:
lm(formula=y~x1+x2+x3+x4+x5+x6+x7+x8+x9,
data=h)
Coefficients:
(Intercept)x1x2x3x4x5x6x7x8x9
320.6409481.3165881.6498592.178660-0.0056091.6842830.0103200.003655-19.13057650.515575
>summary(lm)
Call:
lm(formula=y~x1+x2+x3+x4+x5+x6+x7+x8+x9,
data=h)
Residuals:
Min1QMedian3QMax
-940.13-195.243.42239.00476.06
Coefficients:
EstimateStd.ErrortvaluePr(>|t|)
(Intercept)3.206e+023.952e+030.0810.936097
x11.317e+001.062e-0112.4003.97e-11***
x21.650e+003.008e-015.4841.93e-05***
x32.179e+005.199e-014.1900.000412***
x4-5.609e-034.766e-01-0.0120.990720
x51.684e+002.142e-017.8641.08e-07***
x61.032e-021.343e-020.7690.450665
x73.655e-031.070e-020.3420.736006
x8-1.913e+013.197e+01-0.5980.555983
x95.052e+011.502e+020.3360.739986
---
Signif.codes:
0‘***’0.001‘**’0.01‘*’0.05‘.’0.1‘’1
Residualstandarderror:
389.4on21degreesoffreedom
MultipleR-squared:
0.9923,AdjustedR-squared:
0.9889
F-statistic:
298.9on9and21DF,p-value:
<2.2e-16
>pre=fitted.values(lm)
>res=residuals(lm)
>sd(res)
[1]325.7967
>res=residuals(lm)
>dy=step(lm)
Start:
AIC=377.73
y~x1+x2+x3+x4+x5+x6+x7+x8+x9
DfSumofSqRSSAIC
-x41213184326375.73
-x91171493201454375.90
-x71177003202005375.90
-x81542953238599376.26
-x61895863273891376.59
3184305377.73
-x3126625935846898394.57
-x2145610567745361403.29
-x51937750012561805418.28
-x112331454726498852441.42
Step:
AIC=375.73
y~x1+x2+x3+x5+x6+x7+x8+x9
DfSumofSqRSSAIC
-x91174283201754373.90
-x71185633202889373.91
-x81544373238763374.26
-x61918133276139374.61
3184326375.73
-x3129361306120456393.99
-x2154679418652267404.72
-x51939334512577671416.32
-x112588608629070412442.29
Step:
AIC=373.9
y~x1+x2+x3+x5+x6+x7+x8
DfSumofSqRSSAIC
-x71346343236387372.24
-x61748003276554372.62
-x81821503283904372.69
3201754373.90
-x3130553536257107392.67
-x2157258368927590403.69
-x51938262412584378414.33
-x112586883229070586440.29
Step:
AIC=372.24
y~x1+x2+x3+x5+x6+x8
DfSumofSqRSSAIC
-x81708133307201370.91
-x611527773389165371.67
3236387372.24
-x3155012848737672401.02
-x21889504912131436411.20
-x51945809812694485412.60
-x112773309830969486440.25
Step:
AIC=370.91
y~x1+x2+x3+x5+x6
DfSumofSqRSSAIC
-x611375403444741370.17
3307201370.91
-x3157710639078264400.21
-x21887119312178394409.32
-x51947352112780722410.81
-x112824816231555363438.83
Step:
AIC=370.17
y~x1+x2+x3+x5
DfSumofSqRSSAIC
3444741370.17
-x3157178839162624398.50
-x211024981513694556410.95
-x511099831314443054412.60
-x113325863736703378441.52
>summary(dy)
Call:
lm(formula=y~x1+x2+x3+x5,data=h)
Residuals:
Min1QMedian3QMax
-943.18-161.0512.74250.93566.25
Coefficients:
EstimateStd.ErrortvaluePr(>|t|)
(Intercept)-1694.6269562.9773-3.0100.00574**
x11.36420.086115.8447.11e-15***
x21.76790.20108.7962.86e-09***
x32.28940.34856.5695.76e-07***
x51.74240.19129.1111.42e-09***
---
Signif.codes:
0‘***’0.001‘**’0.01‘*’0.05‘.’0.1‘’1
Residualstandarderror:
364on26degreesoffreedom
MultipleR-squared:
0.9916,AdjustedR-squared:
0.9903
F-statistic:
769.2on4and26DF,p-value:
<2.2e-16
>newdata=data.frame(x1=5200,x2=2000,x3=1100,x4=1000,x5=1300,x6=45000,x7=34000,x8=115.0,x9=3.8)
>predict(dy,newdata,interval="confidence")
fitlwrupr
113718.6713468.9813968.36
>
>h=ts(read.csv("F:
//3.csv",header=TRUE))
>h
TimeSeries:
Start=1
End=56
Frequency=1
X78
[1,]-58
[2,]53
[3,]-63
[4,]13
[5,]-6
[6,]-16
[7,]-14
[8,]3
[9,]-74
[10,]89
[11,]-48
[12,]-14
[13,]32
[14,]56
[15,]-86
[16,]-66
[17,]50
[18,]26
[19,]59
[20,]-47
[21,]-83
[22,]2
[23,]-1
[24,]124
[25,]-106
[26,]113
[27,]-76
[28,]-47
[29,]-32
[30,]39
[31,]-30
[32,]6
[33,]-73
[34,]18
[35,]2
[36,]-24
[37,]23
[38,]-38
[39,]91
[40,]-56
[41,]-58
[42,]1
[43,]14
[44,]-4
[45,]77
[46,]-127
[47,]97
[48,]10
[49,]-28
[50,]-17
[51,]23
[52,]-2
[53,]48
[54,]-131
[55,]65
[56,]-17
>plot(h,type="o")
>local({pkg<-select.list(sort(.packages(all.available=TRUE)),graphics=TRUE)
+if(nchar(pkg))library(pkg,character.only=TRUE)})
Warningmessage:
程辑包‘urca’是用R版本3.4.4来建造的
>adf=ur.df(as.vector(h),type=c("drift"),selectlags=c("AIC"))
>summary(adf)
###############################################
#AugmentedDickey-FullerTestUnitRootTest#
###############################################
Testregressiondrift
Call:
lm(formula=z.diff~z.lag.1+1+z.diff.lag)
Residuals:
Min1QMedian3QMax
-96.191-23.390-0.58118.446133.241
Coefficients:
EstimateStd.ErrortvaluePr(>|t|)
(Intercept)-9.43817.0489-1.3390.187
z.lag.1-1.78370.2386-7.4769.65e-10***
z.diff.lag0.19560.13791.4180.162
---
Signif.codes:
0‘***’0.001‘**’0.01‘*’0.05‘.’0.1‘’1
Residualstandarderror:
50.89on51degreesoffreedom
MultipleR-squared:
0.7589,AdjustedR-squared:
0.7494
F-statistic:
80.25on2and51DF,p-value:
<2.2e-16
Valueoftest-statisticis:
-7.476127.9471
Criticalvaluesforteststatistics:
1pct5pct10pct
tau2-3.51-2.89-2.58
phi16.704.713.86
>acf(h)
>pacf(h)
>ar=sarima(h,1,0,4,details=F)
>ar
$fit
Call:
stats:
:
arima(x=xdata,order=c(p,d,q),seasonal=list(order=c(P,D,
Q),period=S),xreg=xmean,include.mean=FALSE,optim.control=list(trace=trc,
REPORT=1,reltol=tol))
Coefficients:
ar1ma1ma2ma3ma4xmean
-0.0957-0.7605-0.051-0.25910.0706-5.0886
s.e.0.73180.72440.6370.20130.19390.4252
sigma^2estimatedas1850:
loglikelihood=-291.97,aic=597.95
$degrees_of_freedom
[1]50
$ttable
EstimateSEt.valuep.value
ar1-0.09570.7318-0.13080.8965
ma1-0.76050.7244-1.04980.2988
ma2-0.05100.6370-0.08000.9365
ma3-0.25910.2013-1.28750.2038
ma40.07060.19390.36410.7173
xmean-5.08860.4252-11.96680.0000
$AIC
[1]8.73734
$AICc
[1]8.814721
$BIC
[1]7.954342
>ma=sarima(h,0,1,1,details=F)
>ma
$fit
Call:
stats:
:
arima(x=xdata,order=c(p,d,q),seasonal=list(order=c(P,D,
Q),period=S),xreg=constant,optim.control=list(trace=trc,REPORT=1,
reltol=tol))
Coefficients:
ma1constant
-1.00000.1275
s.e.0.04520.4833
sigma^2estimatedas3412:
loglikelihood=-303.77,aic=613.53
$degrees_of_freedom
[1]53
$ttable
EstimateSEt.valuep.value
ma1-1.00000.0452-22.13900.000
constant0.12750.48330.26380.793
$AIC
[1]9.206399
$AICc
[1]9.250355
$BIC
[1]8.278733
>arma=sarima(h,1,1,1,details=F)
>arma
$fit
Call:
stats:
:
arima(x=xdata,order=c(p,d,q),seasonal=list(order=c(P,D,
Q),period=S),xreg=constant,optim.control=list(trace=trc,REPORT=1,
reltol=tol))
Coefficients:
ar1ma1constant
-0.4893-1.00000.1052
s.e.0.11610.04690.2858
sigma^2estimatedas2548:
loglikelihood=-296.27,aic=600.53
$degrees_of_freedom
[1]52
$ttable
EstimateSEt.valuep.value