Time series function language finishing R

[Package]
library (zoo) # pretreatment time format
library (xts) # supra
library (timeSeires) # supra
library (urca) # unit root test
library (tseries) #arma model
library (fUnitRoots) # unit root test
library (FinTS) # call the function in which the auto-regression test
library (fGarch) #GARCH model
library (nlme) # call them gls function
library (fArma) # fitting and testing

[Function] The basic
mathematical functions
abs, sqrt: absolute value, square root of the log, log10, log2, exp: exponential and logarithmic functions sin, cos, tan, asin, acos, atan, atan2: trigonometric functions sinh, cosh, tanh, asinh , acosh, atanh: hyperbolic
simple statistics
sum, mean, var, sd, min, max, range, median, IQR ( interquartile range) as the statistic, sort, order, rank and sort relevant, but also other there ave, fivenum, mad, quantile, stem and so on.

[Data Processing]
# 1 described in detail see profile
# rpm time-series type
X = rnorm (2)
charvec = C ( "2010-01-01", "2010-02-01")
Zoo (X, as.Date ( charvec)) # packet Zoo
XTS (X, as.Date (charvec)) # packet XTS
timeSeries (X, as.Date (charvec)) # timeSeries packet
# regular time series data appears within a predetermined time interval
tm = ts (x, start = c ( 2010,1), frequency = 12) # 12 is by month, quarter by 4 to 1 for the annual
zm = zooreg (x, start = c (2010,1), frequency = 12 ) # packet Zoo
XM = as.xts packet # ™ XTS
SM = as.timeSeries ™ # timeSeries packet
if it is determined at regular time sequence #
is.regular (x)

# Sort
Zoo () and XTS () will be converted to force the positive sequence (in a time name)
timeSeries does not force order; results can be sorted according to the sort function, may be used Rev () function reverse; parameter recordIDs, can give each element (lines) labeled an ID, which can recover the original sequence

# When a predetermined time has duplicate time points
zoo being given
xts ascending order
timeSeries the overlapping portion placed at the end;

The combined rows and columns into #
# are merged in accordance with the column names, column names with different parts instead of NA
cbind ()
of rbind ()
Merge () columns into

# Take subset
XTS () The default matrix vector made; there is no difference with other conventional vector or matrix

# Missing values
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) # missing values by linear interpolation
na.spline (x) # of missing values spline interpolation
na.locf (x) # Last observation carried forward method
na.trim (x, sides = "left ") # remove the last missing value
# of timeSreies data
na.omit (x, "ir") # remove the missing values of the beginning and end
na.omit (x, "iz" ) # replace missing values of the beginning and end
na.omit (x, "ie") # of missing the beginning and end values are interpolated
na.omit (x, method = "ie ", interp = c ( "before", "linear", "after") ) # can select the interpolation method, before last observation method, after the next observation carried forward

as.contiguous (x) # x returns the longest continuous sequence fragments without missing values, if there are two sequence segments of equal length, the first is returned.

# Display time-series data
#zoo xts and can only follow the original format, timeSeries can set the display format
print (x, format = "% m /% d /% y% H:% M") #% m represents month,% d for days,% y is the year,% H represents time,% M indicates min,% a represents a week,% j represents the number of days
#timeSeries ts may be displayed according to the format of
print (x, style = "ts ")
Print (X, style =" TS ", by =" Quarter ")

[Display pattern]
plot.zoo (X)
plot.xts (X)
plot.zoo (X, plot.type = "SINGLE") # support multiple time-series data displayed in a figure
plot (x, plot.type = "single") # support in a plurality of time-series data is shown in FIG, not only xts

Basic statistical operation] [
1, autocorrelation coefficient, the autocorrelation coefficient partial
examples 2.1
D = Scan ( "sha.csv")
SHA = TS (D, Start = 1964, FREQ = 1)
plot.ts (SHA) Drawing # timing diagram
acf (sha, 22) # drawing FIG autocorrelation lag periods 22 is
pAcF (SHA, 22 is) # drawing FIG partial autocorrelation lag periods 22 is
Corr = acf (sha, 22) # stored correlation coefficients
cov = acf (sha, 22, type = " covariance") # save covariance

2, while drawing a timing diagram of two data sets
D = read.csv to ( "double.csv", header = F.)
Double = TS (D, Start = 1964, = FREQ. 1)
Plot (Double, plot.type = "Multiple ") # two sets of data in FIG
plot (double, plot.type =" single ") # FIGS two sets of data a
plot (double, plot.type =" single ", col = c (" red "," green " ), lty = c (1,2) ) # set the color data of each graph, the 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 = "Box-Ljung", lag = 6)

4, differential operation, and the hysteresis calculating
the diff
LAG

5, simulation results ARIMA model
arima.sim (= n-100, List (Ar = 0.8))
plot.ts (arima.sim (= n-100, List (Ar = 0.8))) # randomly generates a 100 comprising the timing chart of the random number
plot.ts (arima.sim (n = 100, list (ar = -1.1))) # non-stationary, the timing can not be obtained in FIG.
plot.ts (arima.sim (= n-100, List (Ar = C (. 1, -0.5))))
plot.ts (arima.sim (= n-100, List (Ar = C (1,0.5))) )
arima.sim (= n-1000, List (Ar = 0.5, = -0.8 mA))
ACF (arima.sim (= n-1000, List (Ar = 0.5, = -0.8 mA)), 20 is)
pAcF (ARIMA. sim (n = 1000, list ( ar = 0.5, ma = -0.8)), 20)

Unit root test] [
# method. 1
B = TS (read.csv to ( "6_1.csv", header = T))
X = B [,. 1]
Y = B [,. 1]
Summary (ur.df (X, type = "Trend", selectlags = "the AIC"))
# 2: test unit better root function, add function drawing
library (fUnitRoots)
urdfTest (X)
# method 3: a self test function ADF
library ( Urca)
# ...
ur.df.01 = function (X, = LAGs. 8) {
# ADF test summary form three kinds of functions (result of inconsistent and EVIEWS)
RES = Matrix (0,5,3)
colnames (RES) = C ( "no", "contains a constant term," "containing a constant term and trend")
rownames (RES) = C ( "of tau statistics", "1% threshold", "5% threshold",
"10 % threshold "," is stable (1/0) ")
types = C (" none "," Drift "," Trend ")
for (I in. 1:. 3) {
x.adf ur.df = (X, = types of the type [i], = LAGs LAGs, selectlags = "AIC")
x.adf.1=x.adf@teststat # statistics
x.adf.2=x.adf@cval # thresholds
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 the else 0
}
return (RES)
}
# ...
ur.df.01 (X) of the original sequence determination #

General ARIMA model []
d = scan ( "a1.5.txt") # Import data
prop = ts (d, start = 1950, freq = 1) # is converted to time-series data
plot (prop) # timing diagram for
acf (prop, 12) # autocorrelation plot for trailing
pacf (prop, 12) # as partial autocorrelation FIG, 1 order truncated
Box.test (prop, type = "Box-Ljung", = LAG. 6)
# of pure randomness test, p less than 5% non-white noise sequence
Box.test (prop, type = "Box-Ljung", = 12 is LAG)
(M1 = ARIMA (prop, Order C = (1,0,0) , method = "ML")) # with AR (1) model fitting, such as the parameter method = "CSS", conditional least squares estimation method, the conditional least squares method, does not display the AIC.
(M2 = arima (prop, order = c (1,0,0), method = "ML", include.mean = F)) # with AR (1) model fitting, free of intercept.
tsdiag (m1) # estimated diagnosis, determines whether a residual white noise
Summary (M1)
r = M1 Katex the parse error: the Expected 'the EOF', GOT '#' AT position 12 is: # residuals stored by the r residual ... Box.tes RESID) # load FinTS package, autocorrelation test
prop.fore = predict (m1, n.ahead = 5) # 5 predict the future value stored in prop.fore variable
U = prop.fore p r e d + 1.96 p r o p . f o r e pred + 1.96* prop.fore SE # automatically generate variance
L = prop.fore p r e d 1.96 p r o p . f o r e pred – 1.96* prop.fore SE # 95% confidence interval was calculated
ts.plot (prop, prop.fore $ pred,timing diagram for containing prediction.
Lines (the U-, COL = "Blue", LTY = "Dashed")
Lines (L, COL = "Blue", LTY = "Dashed") to # 95% confidence intervals in the timing diagrams

- Description: Run command arima (prop, order = c ( 1,0,0), method = "ML") Thereafter, the display:
Call:
ARIMA (X = prop, Order C = (. 1, 0, 0), = Method, "ML")
Coefficients:
AR1 intercept
.6914 81.5509
SE 0.0989 1.7453
Sigma ^ 2 Estimated AS 15.51: log Likelihood = -137.02, AIC = 280.05
Note: intercept below the mean of 81.5509, rather than intercept! Although the intercept intercept is the meaning here would be better if the mean. (The mean and the intercept are the same only when there is no AR term, mean and intercept are the same, only in the absence of AR items)
If you want a intercept, calculated using the formula. int = (1-0.6914) * 81.5509 = 25.16661.

- Description: Box.test (R & lt, type = "Box-Ljung", LAG. 6 =, = fitdf. 1)
fitdf represents p + q, number of degrees of freedom to be subtracted if x is a series of residuals, when tested the sequence of residuals to time, need to add command fitdf, represents the minus degrees of freedom.
Run Box.test (r, type = "Ljung -Box", lag = 6, fitdf = 1) , the results shown:
Box.test (R & lt, type = "Box-Ljung", LAG =. 6, fitdf. 1 = )
Box-Ljung test
Data: R & lt
X--Squared = 5.8661, DF = 5, P-value = 0.3195
"DF = 5" indicates 5 degrees of freedom, because the parameters lag = 6, so a lag of six tests.

# Another test method and parameter estimation (load fArma package)
UE TS = (Scan ( "unemployment.txt"), Start = 1962, F =. 4) reads the data #
Due the diff = (UE)
ddue the diff = ( Due, LAG =. 4)
FIT2 = armaFit (~ ARIMA (4,0,0), include.mean = F., Data = ddue, Method = "ML") # another fitting functions
Summary (FIT2)
FIT3 = armaFit (~ ARIMA (4,0,0), Data = ddue, transform.pars = F., Fixed = C (NA, 0,0, NA), include.mean = F., Method = "the CSS")
Summary (FIT3)

[Special] Model
# some fixed values of coefficients
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=“CSS”)

# Autoregressive model fitting, the dependent variable with respect to time regression model
EG1 = TS (Scan ( "582.txt"))
ts.plot (EG1)
fit.gls = GLS (EG1 + ~ -1 Time (EG1), Correlation = corARMA (p = 1), method = "ML") # See nlme packet
Summary (fit.gls2)
# or
fit = arima (eg1, c ( 1,0,0), xreg = time (eg1), include. F = Mean, Method, = "ML")
AutocorTest (Fit $ RESID) # residual white noise test

# Regression model dependent variable delay
LEG1 = LAG (EG1, -1)
Y = cbind (EG1, LEG1)
Fit = ARIMA (Y [,. 1], C (0,0,0), XREG = Y [, 2], include.mean = F)

Fitting GARCH model #
Library (TSERIES)
Library (fGarch)
Library (FinTS)
A TS = (Scan ( "583.txt"))
ts.plot (A)
Fit LM = (A + ~ -1 Time (A))
RESID = R & lt (Fit)
Summary (Fit)
pAcF (R & lt ^ 2)
acf®
ACF (R & lt ^ 2)
AutocorTest® # whether the residual serial correlation
ArchTest® # ARCH effect if there
fit1 = garchFit (~ arma (2,0 ) + GARCH (1,1), Data = R & lt, algorithm = "nlminb nm +",
the trace = F., include.mean = F.)
Summary (FIT1)

#协整检验
fit=arima(b[,2],xreg=b[,1],method=“CSS”)
r=resid(fit)
summary(ur.df(r,type=“drift”,lag=1))
Box.test(r,lag=6,fitdf=1)

[Autorun] self function
acf.3 (x) # 3 while drawing a correlation diagram, the function expansion acf
ur.df.01 (x) # unit root test, obtained results more comfortable
tsdiag2 (x) # returns x
arma.choose (x, ari = 3, mai = 3) # select the appropriate AR and MA, arma function of packet-based tseries

#########################附属自编函数
#…
acf.3=function(x,lag.max=10,…){
ol=par(mfrow=c(3,1),mar=c(2,4,1,1))
acf(x,lag.max=lag.max,type=“correlation”)
acf(x,lag.max= lag.max,type=“covariance”)
acf(x,lag.max= lag.max,type=“partial”)
par(ol)
}
#…
#…类似于tsgiag函数的扩展
tsdiag2=function(xx.model,fitdf=0,testlag=10){
t1=xx.armaKaTeX parse error: Expected 'EOF', got '}' at position 522: …),lty=2,col=2) }̲ #... ur.df.01=…aic
res.rss[i,j]=sum(ssKaTeX parse error: Expected 'EOF', got '}' at position 16: residuals^2) }̲ for(j in 2:t…aic
res.rss[i,j]=sum(ssThe parse error Katex: the Expected 'the EOF', GOT '}' AT 16 position: residuals ^ 2)} for (I in 2: T ... AIC
res.rss [I, J] = SUM (SS $ residuals ^ 2)
} }
RES = List ()
RES [[ " tt.ar "]] TTI =
RES [[ " tt.ma "]] = tTJ
temp1 = which.min (res.aic) # find the smallest position, the res.aic as a column vector by a row
temp2 = temp1 %% ti #ti is the number of rows, is taken after the I (temp2 of) line number
#AR arma may be directly invoked, MA Similarly
res [[ "AR"]] = if (temp2 0 ==) arma.choose.04 (Ti, ARI, TTI) the else arma.choose.04 (temp2 of, ARI, TTI)
RES [[ "MA"]] = arma.choose.04 (ceiling (temp1 / Ti) , Mai, tTJ)
RES [[ "AIC"]] = res.aic
RES [[ "RSS"]] = res.rss
RES
}
#.

Guess you like

Origin blog.csdn.net/heirenmin/article/details/93459850