-
Notifications
You must be signed in to change notification settings - Fork 2
/
EviewsR-2022.qmd
585 lines (367 loc) · 15.6 KB
/
EviewsR-2022.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
---
title: "EviewsR: A Seamless Integration of EViews and R"
date: "2022/06/04"
author: Sagiru Mati
format: pdf
bibliography: references.bib
execute:
echo: fenced
cache: false
link-citations: true
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
echo = T,
eval=T,
out.width = "45%",
out.height = "20%",
fig.show="hold",
fig.align = "center",
dpi = 300,
tidy = TRUE,
tidy.opts = list(blank = FALSE, width.cutoff = 60),
fig.ncol= 2,
fig.path = 'inst/figures/'
)
library(EviewsR)
library(magrittr)
# set_eviews_path('EViews12_X64')
library(knitr)
library(ggplot2)
```
# About the Author
The author of this package, **Sagiru Mati**, obtained his PhD in
Economics from the Near East University, North Cyprus. He works at the
Department of Economics, Yusuf Maitama Sule (Northwest) University,
Kano, Nigeria. Please visit his [website](https://smati.com.ng) for more
details.
Please follow his publications on [**ORCID: 0000-0003-1413-3974**](https://orcid.org/0000-0003-1413-3974)
# About EviewsR
EviewsR is an R package that can run EViews program in R. It also adds `eviews` as a knit-engine to `knitr` package, so that users can embed EViews codes in R Markdown and Quarto document.
# Why EviewsR?
While the ecosystem of R is great, it cannot run EViews codes, not talk of handling EViews objects dynamically and reproducibly. Even though, EViews can communicate with R, users still need to switch to type-setting application to embed the EViews outputs. Specifically:
* I wish I could embed EViews codes in R Markdown or Quarto document
* I wish I could dynamically import the EViews outputs (graphs, tables, equation and series) individually or at once into R, R Markdown or Quarto document without switching between these applications back and forth.
* I wish I could use an R function in R, R Markdown or Quarto to:
* graph EViews series objects.
* graph an R dataframe using EViews.
* import data from external sources such as `csv`, `xlsx` as a new EViews workfile or into an existing workfile.
* create an EViews workfile from an R dataframe
* save an EViews workfile page as a workfile or another file format.
* execute EViews codes.
* export an R dataframe as a new EViews workfile or to an existing EViews workfile.
* save an EViews workfile as a workfile or another file format.
* import EViews table object as `kable`.
* import EViews series objects as a dataframe or `xts` object
* import equation data members such as coefficients, standard errors, $R^2$ and so on.
* import EViews graph objects
* import equation data members, graph, series and table objects all at once.
* simulate a random walk process using EViews.
* I wish I could do all of the above without opening the EViews!!!
# Installation
EviewsR can be installed using the following commands in R.
```{r installation,eval=F}
install.packages("EviewsR")
OR
devtools::install_github('sagirumati/EviewsR')
```
# Setup
To run the package successfully, you need to do one of the following
* Don't do anything if the name of EViews executable is one of the following: `EViews13_x64`, `EViews13_x86`, `EViews12_x64`, `EViews12_x86`, `EViews11_x64`, `EViews11_x86`, `EViews10_x64`, `EViews10_x86`, `EViews9_x64`, `EViews9_x86`, `EViews10`. The package will find the executable automatically.
* Rename the Eviews executable to `eviews` or one of the names above.
* Alternatively, you can use `set_eviews_path()` function to set the path the EViews executable as follows:
```{r eval=F}
set_eviews_path("C:/Program Files (x86)/EViews 10/EViews10.exe")
```
# Usage
Please load the EviewsR package as follows:
```{r} .
library(EviewsR)
```
# Ways to use EviewsR
The package can work with base R, R Markdown or Quarto document.
The package has been used in @Mati2019, @Mati2021, @Mati2023b, @Mati2023c and @Mati2019.
## EviewsR along with R Markdown or Quarto document
After loading the package, a chunk for Eviews can be created by supplying `eviews` as the engine name in R Markdown or Quarto document as shown below :
```{eviews} `r ''`
#| label: fig-EviewsR
#| eval: true
#| fig.subcap: ["X graph","Y graph"]
#| fig.cap: "EViews graphs imported automatically by fig-EviewsR chunk"
'This program is created in R Markdown with the help of EviewsR package
wfcreate(page=EviewsRPage,wf=EviewsR_workfile) m 2000 2022
for %y EviewsR package page1 page2
pagecreate(page={%y}) EviewsR m 2000 2022
next
pageselect EviewsRPage
rndseed 123456
genr y=@cumsum(nrnd)
genr x=@cumsum(nrnd)
equation ols.ls y c x
freeze(OLSTable,mode=overwrite) ols
freeze(EviewsR_Plot,mode=overwrite) y.line
wfsave EviewsR_workfile
```
```{eviews}
#| label: fig-EviewsR
#| eval: true
#| fig.subcap: ["X graph","Y graph"]
#| fig.cap: "EViews graphs imported automatically by fig-EviewsR chunk"
#| echo: fenced
'This program is created in R Markdown with the help of EviewsR package
wfcreate(page=EviewsRPage,wf=EviewsR_workfile) m 2000 2022
for %y EviewsR package page1 page2
pagecreate(page={%y}) EviewsR m 2000 2022
next
pageselect EviewsRPage
rndseed 123456
genr y=@cumsum(nrnd)
genr x=@cumsum(nrnd)
equation ols.ls y c x
freeze(OLSTable,mode=overwrite) ols
freeze(yy,mode=overwrite) y.line
freeze(xx,mode=overwrite) x.line
wfsave EviewsR_workfile
```
The above chunk creates an Eviews program with the chunk's content, then automatically open Eviews and run the program, which will create an Eviews workfile with pages containing monthly sample from 2000 to 2022. The program will also save an EViews workfile named `EviewsR_workfile` in the current directory.
The `eviews` chunk automatically returns the outputs of each equation object as a dataframe, accessible via `chunkLabel$pageName_equationName`. For example, The $R^2$ of the `ols` equation object is 0.044951, which can be accessed using `` `r knitr::inline_expr('EviewsR$eviewsrpage_ols$r2')` ``. We can obtain the table object by `chunkLabel$pageName_tableName`. Therefore, `EviewsR$eviewsrpage_olstable` will give us the `OLSTable` object as dataframe. Note the underscore (`_`) between the `pageName` and `equationName`, and between the `pageName` and `tableName`.
```{r}
EviewsR$eviewsrpage_ols$r2
EviewsR$eviewsrpage_ols$aic
K=EviewsR$eviewsrpage_olstable[c(6,8,9),1:5]
colnames(K)=NULL
knitr::kable(K,row.names = F,caption = 'Selected cells of EViews table object')
```
The EViews series objects are also imported automatically as dataframe (by default) or `xts` objects (if we use chunk option `class="xts"`). They are accessed via `chunkLabel$pageName`.
```{r}
EviewsR$eviewsrpage %>% head()
```
## EviewsR along with base R
### The create_object() function
The function `create_object()` can be used to create an Eviews object in the existing EViews workfile.
```{r object}
create_object(wf="EviewsR_workfile",action="equation",action_opt="",object_name="eviews_equation",view_or_proc="ls",options_list="",arg_list="y ar(1)")
```
```{r object1}
create_object(wf="EviewsR_workfile",object_name="x1",
object_type="series",expression="y^2")
```
### The eviews_graph() function
EViews graphs can be included in R Markdown or Quarto document by `eviews_graph()` function.
To create graph from existing EViews series objects:
```{r}
#| label: fig-eviewsGraph
#| fig.cap: Graphs of existing EViews series objects imported by fig-eviewsGraph chunk
#| out.width: 45%
#| out.height: 30%
#| layout-ncol: 2
eviews_graph(wf="EviewsR_workfile",page = "EviewsRPage",series="x y",mode = "overwrite",graph_procs = "setelem(1) lcolor(red) lwidth(4)",
graph_options = "m")
```
We can also create graph objects from an R dataframe
```{r}
#| label: fig-eviewsGraph1
#| fig.cap: Graphs of an R dataframe imported by fig-eviewsGraph1 chunk
#| out.width: 90%
#| out.height: 70%
Data=data.frame(x=cumsum(rnorm(100)),y=cumsum(rnorm(100)))
eviews_graph(series=Data,group=TRUE,start_date="1990Q4",frequency="Q")
```
To plot a scatter graph and histogram on the same frame:
```{r}
#| label: fig-eviewsGraph2
#| fig.cap: Scatter graph along with histogram
#| out.width: 90%
#| out.height: 80%
eviews_graph(wf="EviewsR_workfile",page = "EviewsRPage",series="x y",group=T,graph_command="scat(ab=histogram) linefit()",mode = "overwrite",graph_procs = "setelem(1) lcolor(green) lwidth(2)")
```
### The eviews_import() function
Data can be imported from external sources by `eviews_import()` function.
```{r,echo=FALSE}
write.csv(Data,"eviews_import.csv",row.names = FALSE)
```
```{r eviewsImport}
eviews_import(source_description = "eviews_import.csv",start_date = "1990",frequency = "m",
rename_string = "x ab",smpl_string = "1990m10 1992m10")
```
Alternatively, use the dataframe as the `source_description`.
```{r}
eviews_import(source_description = Data,wf="eviews_import1",start_date = "1990",
frequency = "m",rename_string = "x ab",smpl_string = "1990m10 1992m10")
```
### The eviews_pagesave() function
Similar to Eviews workfile, an Eviews page can be saved in various formats by `eviews_pagesave()` function.
```{r pagesave}
eviews_pagesave(wf="eviewsr_workfile",page="EviewsRPage",source_description = "pagesave.csv",drop_list = "y")
```
### The eviews_wfcreate() function
An Eviews workfile can be created using `eviews_wfcreate()` function in R.
```{r wfcreate}
eviews_wfcreate(wf="eviews_wfcreate",page="EviewsRPage",frequency = "m",
start_date = "1990",end_date = "2022")
```
Create a workfile from a dataframe
```{r}
eviews_wfcreate(source_description=Data,wf="eviews_wfcreate1",page="EviewsR_page",frequency="m",
start_date="1990")
```
### The eviews_wfsave() function
An EViews workfile can be saved various output formats using `eviews_wfsave()` in function in R.
```{r wfsave}
eviews_wfsave(wf="eviewsr_workfile",source_description = "wfsave.csv")
```
### The exec_commands() function
A set of Eviews commands can be executed with the help of `exec_commands()` function in R.
```{r execCommands}
exec_commands(c("wfcreate(wf=exec_commands,page=eviewsPage) m 2000 2022"))
```
```{r}
eviewsCommands='pagecreate(page=eviewspage1) 7 2020 2022
for %page eviewspage eviewspage1
pageselect {%page}
genr y=@cumsum(nrnd)
genr x=@cumsum(nrnd)
equation ols.ls y c x
graph x_graph.line x
graph y_graph.area y
freeze(OLSTable,mode=overwrite) ols
next'
exec_commands(commands=eviewsCommands,wf="exec_commands")
```
### The export_dataframe() function
Use `export_dataframe()` function to export dataframe object to Eviews.
```{r exportDataframe}
export_dataframe(wf="export_dataframe",source_description=Data,start_date = '1990',frequency = "m")
```
### The import_equation() function
Import EViews equation data members into R, R Markdown or Quarto.
```{r importEquation}
import_equation(wf="EviewsR_workfile",page="EviewsRPage",equation="OLS")
```
To access the imported equation in base R:
```{r,eval=FALSE,echo=FALSE}
eviews$eviewspage_ols
```
### The import_graph() function
Import EViews graph objects(s) into R, R Markdown or Quarto.
```{r}
#| label: fig-importGraph
#| fig.cap: EViews graphs imported using import\_graph() function
import_graph(wf="eviewsr_workfile")
```
To import only graphs that begin with x:
```{r}
#| label: fig-importGraph1
#| fig.cap: EViews graphs that begin with X imported using import\_graph() function
import_graph(wf="exec_commands",graph="x*")
```
### The import_kable() function
Eviews tables can be imported as `kable` object by `import_kable()` function. Therefore, we can include the
```{r importKable}
import_kable(wf="EViewsR_workfile",page="EviewsRPage",table = "OLSTable",format = "html",
caption = "Selected cells of EViews table imported using import_kable() function",
range = "r7c1:r10c5",digits=3)
```
### The import_series() function
Use `import_series()` function to import data from EViews to R as a dataframe. The function creates a new environment `eviews`, whose objects can be accessed via `eviews$pageName`.
```{r importSeries}
import_series(wf="eviewsr_workfile")
```
To access the series in base R:
```{r,eval=FALSE,echo=TRUE}
eviews$eviewspage %>% head()
```
To import the series as an `xts` object:
```{r importSeries1}
import_series(wf="eviewsr_workfile",series = c("x","y"),class='xts')
```
### The import_table() function
Import EViews table objects(s) into R, R Markdown or Quarto.
To import all table objects across all pages
```{r importTable}
import_table(wf="EviewsR_workfile")
```
To import specific table objects, for example `OLSTable`
```{r importTable1}
import_table(wf="EviewsR_workfile",table="OLStable")
```
To import table objects on specific pages
```{r importTable2}
import_table(wf="EviewsR_workfile",page=" EviewsRPage")
```
To access the table in base R (`eviews$pageName_tableName`)
```{r,echo=TRUE,eval=FALSE}
eviews$eviewspage_olstable
```
### The import_workfile() function
Import EViews equation data members, graph, series and table objects(s) into R, R Markdown or Quarto. This function is a blend of `import_equation()`, `import_graph()`, `import_series()` and `import_table()` functions.
To import all equation, graph, series and table objects across all pages
```{r}
#| label: fig-importWorkfile
#| fig.cap: EViews graphs automatically imported by import\_workfile() function
import_workfile(wf="EviewsR_workfile")
```
To import specific objects
```{r importWorfile1,eval=FALSE,echo=TRUE}
import_workfile(wf="exec_commands",equation="ols",graph="x*",series="y*",table="ols*")
```
To import objects on specific page(s)
```{r importWorfile2,eval=FALSE,echo=TRUE}
import_workfile(wf="exec_commands",page="eviewspage eviewspage1")
```
To access the objects in base R:
```{r,eval=FALSE,echo=TRUE}
eviews$eviewspage_ols # equation
# eviewspage-x_graph # graph saved in "figure/" folder
eviews$eviewspage %>% head() # series
eviews$eviewspage_olstable # table
```
### The rwalk() function
A set of random walk series can be simulated in R using EViews engine, thanks to `rwalk()` function.
```{r rwalk}
rwalk(wf="eviewsr_workfile",series="X Y Z",page="",rndseed=12345,frequency="M",num_observations=100,class='xts')
```
```{r}
#| label: fig-rwalk
#| fig.cap: Plots of imported EViews random walk series objects
#| dpi: 300
#| out.width: 45%
#| fig.dim: [7,4]
#| fig.show: hold
xts::plot.xts(rwalk$xyz,type="l",main = "")
ggplot2::autoplot(rwalk$xyz)
```
### Demo
The demo files are included and can be accessed via `demo(package="EviewsR")`
```{r fig-eviews,eval=F,fig.dim=c(7,4),dpi=300,out.width="45%"}
demo(create_object())
demo(eviews_graph())
demo(eviews_import())
demo(eviews_pagesave())
demo(eviews_wfcreate())
demo(eviews_wfsave())
demo(exec_commands())
demo(export_dataframe())
demo(import_equation())
demo(import_graph())
demo(import_kable())
demo(import_series())
demo(import_table())
demo(import_workfile())
demo(rwalk())
demo(set_eviews_path())
```
# Template
Template for R Markdown is created. Go to `file->New File->R Markdown-> From Template->EviewsR`.
# Similar Packages
Similar packages include [DynareR](https://github.com/sagirumati/DynareR) [@Mati2020;@mati2022dynarer], [gretlR](https://github.com/sagirumati/gretlR) [@Mati2020b;@mati2022gretlr], and [URooTab](https://github.com/sagirumati/URooTab) [@Mati2023a;@mati2023urootab]
For further details, consult @mati2022eviewsr, @Mati2020a and @Mati2023.
<br><br><br><br>
Please download the example files from [Github](https://github.com/sagirumati/EviewsR/tree/master/inst/examples/).
```{r eval=TRUE,echo=FALSE}
x = dir(pattern = "\\.(csv|wf1|inp)")
unlink(x)
```
# References