PC session 2

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/83

encourage image

There's no tags or description

Looks like no tags are added yet.

84 Terms

1
New cards

[Liquor] Clear the workspace

rm(list=ls())

2
New cards

[Liquor] Read the data file (decimal point)

mydata <- read.table(file=file.choose(),header=TRUE)

3
New cards

[Liquor] Use variable names directly

attach(mydata)

4
New cards

[Liquor] Declare monthly time series starting July 1985

liquor_ts <- ts(liquor,frequency=12,start=c(1985,7))

5
New cards

[Liquor] Plot the series

ts.plot(liquor_ts)

6
New cards

[Liquor] Log-transform the series

logliquor_ts <- log(liquor_ts)

7
New cards

[Liquor] Plot the log series

ts.plot(logliquor_ts)

8
New cards

[Liquor] Log-differences (growth)

dlogliquor_ts <- diff(log(liquor_ts))

9
New cards

[Liquor] Plot the log-differenced series

ts.plot(dlogliquor_ts)

10
New cards

[Liquor] Seasonal plot of the log-differenced series

monthplot(dlogliquor_ts)

11
New cards

[Liquor] Log, first difference and seasonal difference (lag 12)

dlogsliquor_ts <- diff(diff(log(liquor_ts), lag = 12))

12
New cards

[Liquor] Plot the log series in first and seasonal differences

ts.plot(dlogsliquor_ts)

13
New cards

[Liquor] Load the unit root test package

library(CADFtest)

14
New cards

[Liquor] Maximum lag = square root of sample size of dlogliquor

max.lag <- round(sqrt(length(dlogliquor_ts)))

15
New cards

[Liquor] Unit root test with intercept, type with drift, lag selection by BIC, max.lag.y

CADFtest(dlogsliquor_ts, type="drift", criterion="BIC", max.lag.y=max.lag)

16
New cards

[Liquor] Put two plots above each other

par(mfrow=c(2,1))

17
New cards

[Liquor] Correlogram

acf(dlogsliquor_ts)

18
New cards

[Liquor] Partial correlogram of dlogsliquor

pacf(dlogsliquor_ts)

19
New cards

[Liquor] Ljung-Box (Q) test for white noise

Box.test(dlogsliquor_ts, lag=max.lag, type="Ljung-Box")

20
New cards

[Liquor] Fit ARIMA(3,1,0)(0,1,0) on the log series

fit_ar <- arima(logliquor_ts, order=c(3,1,0), seasonal=list(order=c(0,1,0)))

21
New cards

[Liquor] Look at the model output of fit_ar

fit_ar

22
New cards

[Liquor] Absolute t-values of all coefficients (significant if > 1.96)

abs(fit_arcoef/sqrt(diag(fitarcoef/sqrt(diag(fit_arvar.coef)))

23
New cards

[Liquor] Plot the residuals of ar

ts.plot(fit_ar$residuals)

24
New cards

[Liquor] Correlogram of the residuals

acf(fit_ar$residuals)

25
New cards

[Liquor] Q-test on the residuals

Box.test(fit_ar$residuals, lag=max.lag, type="Ljung-Box")

26
New cards

[Liquor] Fit alternative ARIMA(0,1,4)(0,1,0)

fit_ma <- arima(logliquor_ts, order=c(0,1,4), seasonal=list(order=c(0,1,0)))

27
New cards

[PPI] Clear the workspace

rm(list=ls())

28
New cards

[PPI] Read the data file (decimal comma)

mydata <- read.table(file=file.choose(),header=TRUE,dec=",")

29
New cards

[PPI] Use variable names directly

attach(mydata)

30
New cards

[PPI] Declare monthly time series starting January 1981

ppi_ts <- ts(PPI,frequency=12,start=c(1981,1))

31
New cards

[PPI] Log-transform the series

logppi_ts <- log(ppi_ts)

32
New cards

[PPI] Plot the log series

ts.plot(logppi_ts)

33
New cards

[PPI] Check the number of rows and columns

dim(mydata)

34
New cards

[PPI] Create a deterministic trend variable

TREND <- 1:372

35
New cards

[PPI] Regress log(PPI) on a constant and trend

fit <- lm(logppi_ts ~ TREND)

36
New cards

[PPI] Look at the regression output

summary(fit)

37
New cards

[PPI] Plot the regression residuals

ts.plot(fit$residuals)

38
New cards

[PPI] Load the unit root test package

library(CADFtest)

39
New cards

[PPI] Maximum lag = square root of sample size

max.lag <- round(sqrt(372))

40
New cards

[PPI] Unit root test with intercept and trend

CADFtest(logppi_ts, type="trend", criterion="BIC", max.lag.y=max.lag)

41
New cards

[PPI] Log-differences

dlogppi_ts <- diff(log(ppi_ts))

42
New cards

[PPI] Plot the log-differenced series

ts.plot(dlogppi_ts)

43
New cards

[PPI] Take subsample from 1997 to 2011

ppi_cut_ts <- window(ppi_ts,start=1997,end=2011)

44
New cards

[PPI] Log-differences of the subsample

dlogppi_cut_ts <- diff(log(ppi_cut_ts))

45
New cards

[PPI] Plot the subsample

ts.plot(dlogppi_cut_ts)

46
New cards

[PPI] Seasonal plot of the subsample

monthplot(dlogppi_cut_ts)

47
New cards

[PPI] Subsample in first and seasonal differences

dlogsppi_cut_ts <- diff(diff(log(ppi_cut_ts),lag=12))

48
New cards

[PPI] Maximum lag for the subsample

max.lag <- round(sqrt(length(dlogsppi_cut_ts)))

49
New cards

[PPI] Unit root test with intercept on the subsample, type is drift

CADFtest(dlogsppi_cut_ts, type="drift", criterion="BIC", max.lag.y=max.lag)

50
New cards

[PPI] Two plots above each other

par(mfrow=c(2,1))

51
New cards

[PPI] Correlogram of the subsample

acf(dlogsppi_cut_ts)

52
New cards

[PPI] Partial correlogram of the subsample

pacf(dlogsppi_cut_ts)

53
New cards

[PUNEW] Clear the workspace

rm(list=ls())

54
New cards

[PUNEW] Read the data file (decimal point)

mydata <- read.table(file=file.choose(),header=TRUE,dec=".")

55
New cards

[PUNEW] Use variable names directly

attach(mydata)

56
New cards

[PUNEW] Declare quarterly time series starting 1989 Q1

PUNEW_ts <- ts(PUNEW,frequency=4,start=c(1989,1))

57
New cards

[PUNEW] Log-transform the series

logPU_ts <- log(PUNEW_ts)

58
New cards

[PUNEW] Plot the log series

ts.plot(logPU_ts)

59
New cards

[PUNEW] Load the unit root test package

library(CADFtest)

60
New cards

[PUNEW] Check the number of rows

dim(mydata)

61
New cards

[PUNEW] Maximum lag = square root of sample size

max.lag <- round(sqrt(65))

62
New cards

[PUNEW] Unit root test with intercept and trend

CADFtest(logPU_ts, type="trend", criterion="BIC", max.lag.y=max.lag)

63
New cards

[PUNEW] Log-differences

dlogPU_ts <- diff(log(PUNEW_ts))

64
New cards

[PUNEW] Plot the log-differenced series

plot.ts(dlogPU_ts)

65
New cards

[PUNEW] Seasonal plot

monthplot(dlogPU_ts)

66
New cards

[PUNEW] Unit root test with intercept on the differenced series

CADFtest(dlogPU_ts, type="drift", criterion="BIC", max.lag.y=max.lag)

67
New cards

[PUNEW] Two plots above each other

par(mfrow=c(2,1))

68
New cards

[PUNEW] Correlogram (suggests MA order)

acf(dlogPU_ts)

69
New cards

[PUNEW] Partial correlogram (suggests AR order)

pacf(dlogPU_ts)

70
New cards

[PUNEW] Fit ARIMA(3,1,0) without seasonal part

fit_ar <- arima(logPU_ts, order=c(3,1,0), seasonal=c(0,0,0))

71
New cards

[PUNEW] Look at the model output

fit_ar

72
New cards

[PUNEW] Absolute t-values of all coefficients

abs(fit_arcoef/sqrt(diag(fitarcoef/sqrt(diag(fit_arvar.coef)))

73
New cards

[PUNEW] Plot the residuals

plot.ts(fit_ar$residuals)

74
New cards

[PUNEW] Correlogram of the residuals

acf(fit_ar$residuals)

75
New cards

[PUNEW] Q-test on the residuals

Box.test(fit_ar$residuals, lag=max.lag, type="Ljung-Box")

76
New cards

[PUNEW] Fit ARIMA(3,1,1)

fit_arma <- arima(logPU_ts, order=c(3,1,1), seasonal=c(0,0,0))

77
New cards

[PUNEW] Correlogram of the ARMA residuals

acf(fit_arma$residuals)

78
New cards

[PUNEW] Partial correlogram of the ARMA residuals

pacf(fit_arma$residuals)

79
New cards

[PUNEW] Q-test on the ARMA residuals

Box.test(fit_arma$residuals, lag=max.lag, type="Ljung-Box")

80
New cards

[PUNEW] AIC of the AR(3) model

AIC(fit_ar)

81
New cards

[PUNEW] AIC of the ARMA(3,1) model

AIC(fit_arma)

82
New cards

[PUNEW] SIC of the AR(3) model (n = 64)

AIC(fit_ar,k=log(64))

83
New cards

[PUNEW] SIC of the ARMA(3,1) model (n = 64)

AIC(fit_arma,k=log(64))

84
New cards