R Language time series function collation __ function

Source: Internet
Author: User
Tags abs constant diff mathematical functions sort square root time interval
Package
Library (Zoo) #时间格式预处理
Library (XTS) #同上
Library (timeseires) #同上
Library (Urca) #进行单位根检验
Library (tseries) #arma模型
Library (funitroots) #进行单位根检验
Library (fints) #调用其中的自回归检验函数
Library (Fgarch) #GARCH模型
Library (Nlme) #调用其中的gls函数
Library (Farma) #进行拟合和检验

"Basic Functions"
Mathematical functions
Abs,sqrt: Absolute, square root log, log10, LOG2, Exp: Logarithmic and exponential function sin,cos,tan,asin,acos,atan,atan2: trigonometric Sinh,cosh,tanh,asinh,acosh, Atanh: Hyperbolic function
Simple statistics
Sum, mean, Var, sd, Min, max, range, median, IQR (four cent spacing) are statistics, Sort,order,rank are related to sorting, and others are ave,fivenum,mad,quantile,stem.


Data processing
#具体说明见文档1
#转成时间序列类型
x = Rnorm (2)
Charvec = C ("2010-01-01", "2010-02-01")
Zoo (X,as. Date (Charvec)) #包zoo
XTS (x, as. Date (Charvec)) #包xts
TimeSeries (X,as. Date (Charvec)) #包timeSeries
#规则的时间序列, the data appears within the specified time interval
TM = ts (X,start = C (2010,1), frequency=12) #12为按月份, 4 per quarter, 1 for year
ZM = Zooreg (X,start = C (2010,1), frequency=12) #包zoo
XM = As.xts (tm) #包xts
SM = As.timeseries (tm) #包timeSeries
#判断是否为规则时间序列
Is.regular (x)

#排序
Zoo () and XTS () are forced to convert to positive order (by time name)
TimeSeries does not force sorting, the result can be sorted according to the sort function, or can be reversed by the Rev () function, parameter recordids, you can mark each element (line) an ID, so that you can retrieve the original order

#预设的时间有重复的时间点时
The zoo will make an error
XTs in ascending order
TimeSeries Place the repeating part in the tail;

#行合并和列合并
#都是按照列名进行合并, different parts of the column are replaced with NA
Cbind ()
Rbind ()
Merge () column merging

#取子集
XTS () The vectors are made into matrices by default, and the others are no different from regular vectors or matrices.

#缺失值处理
Na.omit (x)
X[is.na (x)] = 0
X[is.na (x)] = mean (x,na.rm=true)
X[is.na (x)] = median (x,na.rm=true)
Na.approx (x) #对缺失值进行线性插值
Na.spline (x) #对缺失值进行样条插值
NA.LOCF (x) #末次观测值结转法
Na.trim (x, sides= "left") #去掉最后一个缺失值
#对timeSreies数据
Na.omit (x, "IR") #去掉首末位置的缺失值
Na.omit (x, "iz") #用替换首末位置的缺失值
Na.omit (x, "ie") #对首末位置的缺失值进行插值
Na.omit (x, method= "ie", interp= C ("Before", "linear", "after")) #可以选择插值方法, before last observation method, after next observation closing method

As.contiguous (x) #返回x中最长的连续无缺失值的序列片段, if there are two sequence fragments of equal length, the first one is returned.

#时间序列数据的显示
#zoo和xts都只能按照原来的格式显示, TimeSeries can set the display format
Print (x, format= "%m/%d/%y%h:%m") #%m represents the month,%d represents the day,%y represents the year,%H represents the minute, the%m represents the minutes, and the is the week, and the%a represents the number of days
#timeSeries也可以按照ts的格式显示
Print (x, style= "ts")
Print (x, style= "ts", by= "quarter")

"Graphic Display"
Plot.zoo (x)
Plot.xts (x)
Plot.zoo (x, plot.type= "single") #支持多个时间序列数据在一个图中展示
Plot (x, plot.type= "single") #支持多个时间序列数据在一个图中展示, only for XTS

"Basic statistical operations"
1, self-correlation coefficient, partial autocorrelation coefficient, etc.
Example 2.1
D=scan ("Sha.csv")
Sha=ts (D,start=1964,freq=1)
Plot.ts (SHA) #绘制时序图
ACF (sha,22) #绘制自相关图, number of lag 22
PACF (sha,22) #绘制偏自相关图, number of lag periods 22
CORR=ACF (sha,22) #保存相关系数
COV=ACF (Sha,22,type = "covariance") #保存协方差

2, simultaneously draw two sets of data sequence diagram
D=read.csv ("Double.csv", header=f)
Double=ts (D,start=1964,freq=1)
Plot (double, plot.type = "multiple") #两组数据两个图
Plot (double, plot.type = "single") #两组数据一个图
Plot (double, plot.type = "single", Col=c ("Red", "green"), Lty=c ()) #设置每组数据图的颜色, curve type)

3. Pure Randomness test
Example 2.3 continued
D=scan ("Temp.csv")
Temp=ts (D,freq=1,start=c (1949))
Box.test (temp, type= "Ljung-box", lag=6)

4. Differential operation and LAG operation
Diff
Lag

5. Simulation of ARIMA Model results
Arima.sim (n = +, list (AR = 0.8))
Plot.ts (Arima.sim (n = +, list (AR = 0.8))) #会随机产生一个包含100个随机数的时序图
Plot.ts (Arima.sim (n = +, list (AR = -1.1)) #非平稳, the timing diagram cannot be obtained.
Plot.ts (Arima.sim (n = +, list (AR = C (1,-0.5)))
Plot.ts (Arima.sim (n = +, list (AR = C (1,0.5)))
Arima.sim (n = +, list (ar = 0.5, MA =-0.8))
ACF (Arima.sim (n = +, list (ar = 0.5, MA =-0.8)), 20)
PACF (Arima.sim (n = 0.8, list (AR = 0.5, MA =-)), 20)

"Unit root Test"
#方法1
B=ts (Read.csv ("6_1.csv", header=t))
x=b[,1]
y=b[,1]
Summary (UR.DF (x,type= "trend", selectlags= "AIC"))
#方法2: unit root test better function, add paint function
Library (Funitroots)
Urdftest (x)
#方法3: A self-coding function of the ADF test
Library (Urca)
#...
Ur.df.01=function (x,lags=8) {
#将三种ADF检验形式汇总的函数 (results inconsistent with eviews)
Res=matrix (0,5,3)
Colnames (RES) =c ("None", "with constant term", "with constant term and trend item")
Rownames (RES) =c ("Tau statistic", "1% Critical Value", "5% threshold",
"10% threshold", "stable (1/0)")
Types=c ("None", "drift", "trend")
For (i in 1:3) {
X.ADF=UR.DF (x,type=types[i],lags=lags,selectlags= "AIC")
X.adf.1=x.adf@teststat #统计量
X.adf.2=x.adf@cval #临界值
Res[1,i] =x.adf.1[1]
Res[2:4,i]=x.adf.2[1,]
Res[5,i]=if (ABS (Res[1,i]) > abs (res[3,i)) 1 Else 0
}
Return (RES)
}
#...
ur.df.01 (x) #对原序列进行判断

"General Arima Model"
D=scan ("A1.5.txt") #导入数据
Prop=ts (d,start=1950,freq=1) #转化为时间序列数据
Plot (prop) #作时序图
ACF (prop,12) #作自相关图, trailer
PACF (prop,12) #作偏自相关图, 1-Step truncated
Box.test (prop, type= "Ljung-box", lag=6)
#纯随机性检验, p value is less than 5%, sequence is non-white noise
Box.test (prop, type= "Ljung-box", lag=12)
(M1=arima (prop, order = C (1,0,0), method= "ML")) #用AR (1) model fitting, such as parameter method= "CSS", is estimated to be the conditional least squares method, and the AIC is not displayed when the conditional least squares is used.
(M2=arima (prop, order = C (1,0,0), method= "ML", Include.mean = F)) #用AR (1) model fits without intercept items.
TSDIAG (M1) #对估计进行诊断 to determine if the residuals are white noise
Summary (M1)
R=m1$residuals #用r来保存残差
Box.test (r,type= "Ljung-box", Lag=6, Fitdf=1) #对残差进行纯随机性检验, FITDF indicates the degree of freedom to reduce residuals
Autocortest (M1$RESID) #加载FinTS包 for self-correlation test
Prop.fore = Predict (M1, N.ahead =5) #将未来5期预测值保存在prop. Fore variable
U = prop.fore$pred + 1.96* prop.fore$se #会自动产生方差
L = prop.fore$pred–1.96* prop.fore$se #算出95% confidence interval
Ts.plot (prop, prop.fore$pred, Col=1:2) #作时序图, with projections.
Lines (U, col= "Blue", lty= "dashed")
Lines (L, col= "Blue", lty= "dashed") #在时序图中作出95% confidence interval

--Description: After running the command Arima (prop, order = C (1,0,0), method= "ML"), the display:
Call:
Arima (x = prop, order = C (1, 0, 0), method = "ML")
Coefficients:
AR1 Intercept
0.6914 81.5509
S.E. 0.0989 1.7453
Sigma^2 estimated as 15.51:log likelihood = -137.02, AIC = 280.05
Note: Intercept the following 81.5509 is the mean, not the intercept. Although intercept is the meaning of intercept, it is better to use mean here. (The mean and the intercept are the same, and the mean and intercept are the same for the only time there is no AR, except when there are no AR entries)
If you want the intercept, use the formula to calculate. Int= (1-0.6914) *81.5509= 25.16661.

--Description: Box.test (r,type= "Ljung-box", lag=6,fitdf=1)
FITDF represents p+q,number of degrees of freedom to being subtracted if X is a series of residuals, and when the test sequence is the residuals then it is necessary to add the command FITDF, which represents the subtracted degrees of freedom.
After you run Box.test (r,type= "Ljung-box", lag=6,fitdf=1), the results are displayed:
Box.test (r,type= "Ljung-box", lag=6,fitdf=1)
Box-ljung Test
Data:r
x-squared = 5.8661, df = 5, P-value = 0.3195
"DF = 5" means that the degree of freedom is 5, because the parameter is lag=6, so it is the test of Lag 6 period.

#另一个参数估计与检验的方法 (Load Farma package)
Ue=ts (Scan ("Unemployment.txt"), start=1962,f=4) #读取数据
DUE=DIFF (UE)
Ddue=diff (due,lag=4)
Fit2=armafit (~arima (4,0,0), include.mean=f,data=ddue,method= "ML") #另一种拟合函数
Summary (FIT2)
Fit3=armafit (~arima (4,0,0), Data=ddue,transform.pars=f,fixed=c (Na,0,0,na), include.mean=f,method= "CSS")
Summary (FIT3)

"Some special models."
#固定某些系数的值
Arima (Dw,order=c (4,0,0), Fixed=c (na,0,0,na,0), method= "CSS")

#乘积季节模型
Wue=ts (Scan ("Wue.txt"), start=1948,f=12)
Arima (Wue,order=c (1,1,1), Seasonal=list (Order=c (0,1,1), period=12), Include.mean=f,method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.