-
Notifications
You must be signed in to change notification settings - Fork 4
/
05-programming.qmd
1616 lines (1215 loc) · 53.6 KB
/
05-programming.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Programming & Advanced Features
Stata features the ability to create user-written commands. These can range from
simple data manipulation commands to completely new statistical models. This is
an advanced feature that not many users will need.
However, there are several components of the programming capabilities which are
very useful even without writing your own commands. Here we'll discuss several.
Let's open a fresh version of "auto":
```stata
. sysuse auto, clear
(1978 automobile data)
```
## Macros
While variables stored as strings aren't of much use to us, strings stored as
other strings can be quite useful. Imagine the following scenario: You have a
collection of 5 variables that you want to perform several different operations
on. You might have code like this:
```stata
list var1 var2 var3 var4 var5 in 1/5
summarize var1 var2 var3 var4 var5
label define mylab 0 "No" 1 "Yes"
label values var1 var2 var3 var4 var5 mylab
duplicates list var1 var2 var3 var4 var5
```
This can get extremely tedious as the number of variables and commands
increases. You could copy and paste a lot, but even that takes a lot of effort.
Instead, we can store the list of variables (strictly speaking, the string which
contains the list of variables) in a shorter key string, and refer to that
instead!
```stata
local vars = "var1 var2 var3 var4 var5"
list `vars' in 1/5
summarize `vars'
label define mylab 0 "No" 1 "Yes"
label values `vars' mylab
duplicates list `vars'
```
The first command, `local`, defines what is known as a "local macro". Whenever
it is referred to, wrapped in a backtick (to the left of the 1 key at the
top-left of the keyboard) and a single quote, Stata replaces it with the
original text. So when you enter
```stata
list `vars' in 1/5
```
Stata immediately replaces `` `vars'`` with `var1 var2 var3 var4 var5`, then
executes
```stata
list var1 var2 var3 var4 var5 in 1/5
```
**Important**: Local macros are deleted as soon as code finishes executing! That
means that you must use them in a do-file, and **you must run all lines which
create and access the macro at the same time**, by highlighting them all.
If your macro contains text that should be quoted, you still need to quote it
when accessing. For example, if you had
```stata
label variable price1 "Price (in dollars) at Time Point 1"
label variable price2 "Price (in dollars) at Time Point 2"
```
you could instead write
```stata
local pricelab = "Price (in dollars) at Time Point"
label variable price1 "`pricelab' 1"
label variable price2 "`pricelab' 2"
```
You can use `display` to print the content of macros to the output to preview them.
```stata
. local test = "abc"
. display "`test'"
abc
```
You may occasionally see code that excludes the `=` in defining a macro (e.g.
`local vars "var1 var2"`). This matters when working with numeric macros. Using
an `=` forces the evaluates the macro, exlcuding it doesn't. For example,
```stata
. local x 1 + 3
. display "`x'"
1 + 3
. local y = 1 + 3
. display "`y'"
4
```
Note the use of quotations there. `display` would evaluate numerics regardless
of `=` or not, so by treating it as a string, we can see the difference.
### Global macros
`local` defines a "local macro"; `global` defines a "global macro". Global
macros persist between runs - whereas a local macro is removed after the code
finishing executing, the global stays around.
Global macros are accessesd with `$`:
```stata
. global dog "Black Lab"
. display "$dog"
Black Lab
```
### Class and Return
Every command in Stata is of a particular type. One major aspect of the type is
what the command "returns". Some commands are n-class, which means they don't
return anything. Some are c-class, which are only used by programmers and rarely
useful elsewhere. The two common ones are e-class and r-class. The distinction
between the two is inconsequential, besides that they store their "returns" in
different places.
Here, `summarize` is a r-class command, so it stores its returns in "return". We
can see them all by `return list`. On the other hand, `mean` (which we haven't
discussed, but basically displays summary statistics similar to `summarize` but
provides some additional functionality) is an e-class command, storing its
results in `ereturn`:
```stata
. summarize price
Variable | Obs Mean Std. dev. Min Max
-------------+---------------------------------------------------------
price | 74 6165.257 2949.496 3291 15906
. return list
scalars:
r(N) = 74
r(sum_w) = 74
r(mean) = 6165.256756756757
r(Var) = 8699525.974268788
r(sd) = 2949.495884768919
r(min) = 3291
r(max) = 15906
r(sum) = 456229
. mean price
Mean estimation Number of obs = 74
--------------------------------------------------------------
| Mean Std. err. [95% conf. interval]
-------------+------------------------------------------------
price | 6165.257 342.8719 5481.914 6848.6
--------------------------------------------------------------
. ereturn list
scalars:
e(df_r) = 73
e(N_over) = 1
e(N) = 74
e(k_eq) = 1
e(rank) = 1
macros:
e(cmdline) : "mean price"
e(cmd) : "mean"
e(vce) : "analytic"
e(title) : "Mean estimation"
e(estat_cmd) : "estat_vce_only"
e(varlist) : "price"
e(marginsnotok) : "_ALL"
e(properties) : "b V"
matrices:
e(b) : 1 x 1
e(V) : 1 x 1
e(sd) : 1 x 1
e(_N) : 1 x 1
e(error) : 1 x 1
functions:
e(sample)
```
Rather than try and keep track of what gets stored where, if you look at the
very bottom of any help file, it will say something like "`summarize` stores the
following in `r()`:" or "`mean` stores the following in `e()`:", corresponding
to `return` and `ereturn` respectively.
Along with the [One Data](01-the-basics-of-stata.qmd#one-data) principal, Stata
also follows the One _-class principal - meaning you can only view the `return`
or `ereturn` for the most recent command of that class. So if you run a
`summarize` command, then do a bunch of n-class calls (`gsort` for example), the
`return list` call will still give you the returns for that first `summarize`.
However, as soon as you run another r-class command, you lose access to the
first one. You can save any piece of it using a
[macro](05-programming.qmd#macros). For example, to calculate the average
difference in price between foreign and domestic cars^[There are obviously other
ways to compute this, but this gives a flavor of the use.]:
```stata
. summarize price if foreign == 1
Variable | Obs Mean Std. dev. Min Max
-------------+---------------------------------------------------------
price | 22 6384.682 2621.915 3748 12990
. local fprice = r(mean)
. summarize price if foreign == 0
Variable | Obs Mean Std. dev. Min Max
-------------+---------------------------------------------------------
price | 52 6072.423 3097.104 3291 15906
. local dprice = r(mean)
. display `dprice' - `fprice'
-312.25874
```
## Variable Lists
Introduced in Stata 16, variable lists solves a common technique used in
previous versions of Stata to define a `global` containing a list of variables
to be used later in the document. For example, you might see something like this
at the top of a Do file:
```stata
global predictors x1 x2 x3 x4
```
then further down the document something like
```stata
regress y $predictors
logit z $predictors
```
Stata has formalized this concept with the addition of the `vl` command
(**v**ariable **l**ist). It works similarly to the use of globals: lists of
variables are defined, then later reference via the `$name` syntax. However,
using `vl` has the benefits of improved organization, customizations unique to
variable lists, error checking, and overall convenience.
### Initialization of Variable Lists
To begin using variable lists, `vl set` must be run.
```stata
. sysuse auto
(1978 automobile data)
. vl set
-------------------------------------------------------------------------------
| Macro's contents
|------------------------------------------------------------
Macro | # Vars Description
------------------+------------------------------------------------------------
System |
$vlcategorical | 2 categorical variables
$vlcontinuous | 2 continuous variables
$vluncertain | 7 perhaps continuous, perhaps categorical variables
$vlother | 0 all missing or constant variables
-------------------------------------------------------------------------------
Notes
1. Review contents of vlcategorical and vlcontinuous to ensure they are
correct. Type vl list vlcategorical and type vl list vlcontinuous.
2. If there are any variables in vluncertain, you can reallocate them
to vlcategorical, vlcontinuous, or vlother. Type
vl list vluncertain.
3. Use vl move to move variables among classifications. For example,
type vl move (x50 x80) vlcontinuous to move variables x50 and x80 to
the continuous classification.
4. vlnames are global macros. Type the vlname without the leading
dollar sign ($) when using vl commands. Example: vlcategorical not
$vlcategorical. Type the dollar sign with other Stata commands to
get a varlist.
```
This produces a surprisingly large amount of output. When you initialize the use
of variable lists, Stata will automatically create four variable lists, called
the "System variable lists". Every *numeric* variable in the current data set is
automatically placed into one of these four lists:
- `vlcategorical`: Variables which Stata thinks are categorical. These generally
have to be non-negative, integer valued variables with less than 10 unique
values.
- `vlcontinuous`: Variables which Stata thinks are continuous. These generally
are variables which have negative values, have non-integer values, or are
non-negative integers with more than 100 unique values.
- `vluncertain`: Variables which Stata is unsure whether they are continuous or
categorical. These generally are non-negative integer valued variables with
between 10 and 100 unique values.
- `vlother`: Any numeric variables that aren't really useful - either all
missing or constant variables.
There is a potential fifth system variable list, `vldummy`, which is created
when option `dummy` is passed. Unsurprisingly, this will take variables
containing only values 0 and 1 out of `vlcategorical` and into this list.
The "Notes" given below the output are generic; they appear regardless of how
well Stata was able to categorize the variables. They can be suppressed with the
`nonotes` option to `vl set`.
The two thresholds given above, 10 and 100, can be adjusted by the `categorical`
and `uncertain` options. For example,
```stata
vl set, categorical(20) uncertain(50)
```
Running `vl set` on an already `vl`-set data set will result in an error, unless
the `clear` option is given, which will re-generate the lists.
```stata
. vl set, dummy nonotes
one or more already classified variables specified
You requested that variables be added to vl's system classifications, but
you specified 11 variables that were already classified.
r(110);
. vl set, dummy nonotes clear
-------------------------------------------------------------------------------
| Macro's contents
|------------------------------------------------------------
Macro | # Vars Description
------------------+------------------------------------------------------------
System |
$vldummy | 1 0/1 variable
$vlcategorical | 1 categorical variable
$vlcontinuous | 2 continuous variables
$vluncertain | 7 perhaps continuous, perhaps categorical variables
$vlother | 0 all missing or constant variables
-------------------------------------------------------------------------------
```
In the above, we changed our minds and wanted to include the `vldummy` list, but
since we'd already `vl`-set, we had the `clear` the existing set.
### Viewing lists
When initializing the variable lists, we're treated to a nice table of all
defined lists. We can replay it via
```stata
. vl dir
-------------------------------------------------------------------------------
| Macro's contents
|------------------------------------------------------------
Macro | # Vars Description
------------------+------------------------------------------------------------
System |
$vldummy | 1 0/1 variable
$vlcategorical | 1 categorical variable
$vlcontinuous | 2 continuous variables
$vluncertain | 7 perhaps continuous, perhaps categorical variables
$vlother | 0 all missing or constant variables
-------------------------------------------------------------------------------
```
To see the actual contents of the variable lists, we'll need to sue `vl list`.
```stata
. vl list
----------------------------------------------------
Variable | Macro Values Levels
-------------+--------------------------------------
foreign | $vldummy 0 and 1 2
rep78 | $vlcategorical integers >=0 5
headroom | $vlcontinuous noninteger
gear_ratio | $vlcontinuous noninteger
price | $vluncertain integers >=0 74
mpg | $vluncertain integers >=0 21
trunk | $vluncertain integers >=0 18
weight | $vluncertain integers >=0 64
length | $vluncertain integers >=0 47
turn | $vluncertain integers >=0 18
displacement | $vluncertain integers >=0 31
----------------------------------------------------
```
This output produces one row for each variable *in each variable list it is in*.
We haven't used this yet, but variables can be in multiple lists.
We can list only specific lists:
```stata
. vl list vlcategorical
------------------------------------------------
Variable | Macro Values Levels
---------+--------------------------------------
rep78 | $vlcategorical integers >=0 5
------------------------------------------------
```
or specific variables
```stata
. vl list (turn weight)
------------------------------------------------
Variable | Macro Values Levels
---------+--------------------------------------
turn | $vluncertain integers >=0 18
weight | $vluncertain integers >=0 64
------------------------------------------------
```
If `turn` was in multiple variable lists, each would appear as a row in this
output.
There's a bit of odd notation which can be used to sort the output by variable
name, which makes it easier to identify variables which appear in multiple
lists.
```stata
. vl list (_all), sort
----------------------------------------------------
Variable | Macro Values Levels
-------------+--------------------------------------
displacement | $vluncertain integers >=0 31
foreign | $vldummy 0 and 1 2
gear_ratio | $vlcontinuous noninteger
headroom | $vlcontinuous noninteger
length | $vluncertain integers >=0 47
mpg | $vluncertain integers >=0 21
price | $vluncertain integers >=0 74
rep78 | $vlcategorical integers >=0 5
trunk | $vluncertain integers >=0 18
turn | $vluncertain integers >=0 18
weight | $vluncertain integers >=0 64
----------------------------------------------------
```
The `(_all)` tells Stata to report on all variables, and sorting (when you
specify at least one variable) orders by variable name rather than variable list
name.
This will also list any numeric variables which are not found in **any** list.
#### Moving variables in system lists
After initializing the variable lists, if you plan on using the system lists,
you may need to move variables around (e.g. classifying the `vluncertain`
variables into their proper lists). This can be done via `vl move` which has the
syntax
```stata
vl move (<variables to move>) <destination list>
```
For example, all the variables in `vluncertain` are actually continuous:
```stata
. vl list vluncertain
----------------------------------------------------
Variable | Macro Values Levels
-------------+--------------------------------------
price | $vluncertain integers >=0 74
mpg | $vluncertain integers >=0 21
trunk | $vluncertain integers >=0 18
weight | $vluncertain integers >=0 64
length | $vluncertain integers >=0 47
turn | $vluncertain integers >=0 18
displacement | $vluncertain integers >=0 31
----------------------------------------------------
. vl move (price mpg trunk weight length turn displacement) vlcontinuous
note: 7 variables specified and 7 variables moved.
------------------------------
Macro # Added/Removed
------------------------------
$vldummy 0
$vlcategorical 0
$vlcontinuous 7
$vluncertain -7
$vlother 0
------------------------------
. vl dir
-------------------------------------------------------------------------------
| Macro's contents
|------------------------------------------------------------
Macro | # Vars Description
------------------+------------------------------------------------------------
System |
$vldummy | 1 0/1 variable
$vlcategorical | 1 categorical variable
$vlcontinuous | 9 continuous variables
$vluncertain | 0 perhaps continuous, perhaps categorical variables
$vlother | 0 all missing or constant variables
-------------------------------------------------------------------------------
```
Alternatively, since we're moving all variables in `vluncertain`, we can see our
first use of the variable list!
```stata
. vl set, dummy nonotes clear
-------------------------------------------------------------------------------
| Macro's contents
|------------------------------------------------------------
Macro | # Vars Description
------------------+------------------------------------------------------------
System |
$vldummy | 1 0/1 variable
$vlcategorical | 1 categorical variable
$vlcontinuous | 2 continuous variables
$vluncertain | 7 perhaps continuous, perhaps categorical variables
$vlother | 0 all missing or constant variables
-------------------------------------------------------------------------------
. vl move ($vluncertain) vlcontinuous
note: 7 variables specified and 7 variables moved.
------------------------------
Macro # Added/Removed
------------------------------
$vldummy 0
$vlcategorical 0
$vlcontinuous 7
$vluncertain -7
$vlother 0
------------------------------
```
Note that variable lists are essentially just global macros so can be referred
to via `$name`. Note, however, that the `$` is only used when we want to
actually use the variable list as a macro - in this case, we wanted to expand
`vluncertain` into it's list of variables. When we're referring to a variable
list in the `vl` commands, we *do not* use the `$`.
### User Variable Lists
In addition to the System variable lists, you can define your own User variables
lists, which I imagine will be used far more often. These are easy to create
with `vl create`:
```stata
. vl create mylist1 = (weight mpg)
note: $mylist1 initialized with 2 variables.
. vl create mylist2 = (weight length trunk)
note: $mylist2 initialized with 3 variables.
. vl dir, user
-------------------------------------------------------------------------------
| Macro's contents
|------------------------------------------------------------
Macro | # Vars Description
------------------+------------------------------------------------------------
User |
$mylist1 | 2 variables
$mylist2 | 3 variables
-------------------------------------------------------------------------------
. vl list, user
------------------------------------------------
Variable | Macro Values Levels
---------+--------------------------------------
weight | $mylist1 integers >=0 64
mpg | $mylist1 integers >=0 21
weight | $mylist2 integers >=0 64
length | $mylist2 integers >=0 47
trunk | $mylist2 integers >=0 18
------------------------------------------------
```
Note the addition of the `user` option to `vl list` and `vl dir` to show only
User variable lists and suppress the System variable lists. We can also
demonstrate the odd sorting syntax here:
```stata
. vl list (_all), sort user
----------------------------------------------------
Variable | Macro Values Levels
-------------+--------------------------------------
displacement | not in vluser 31
foreign | not in vluser 2
gear_ratio | not in vluser
headroom | not in vluser
length | $mylist2 integers >=0 47
mpg | $mylist1 integers >=0 21
price | not in vluser 74
rep78 | not in vluser 5
trunk | $mylist2 integers >=0 18
turn | not in vluser 18
weight | $mylist1 integers >=0 64
weight | $mylist2 integers >=0 64
----------------------------------------------------
```
You can refer to variable lists in all the usual shortcut ways:
```stata
vl create mylist = (q x1-x100 z*)
```
We can add labels to variable lists:
```stata
. vl label mylist1 "Related to gas consumption"
. vl label mylist2 "Related to size"
. vl dir, user
-------------------------------------------------------------------------------
| Macro's contents
|------------------------------------------------------------
Macro | # Vars Description
------------------+------------------------------------------------------------
User |
$mylist1 | 2 Related to gas consumption
$mylist2 | 3 Related to size
-------------------------------------------------------------------------------
```
#### Modifying User Variable Lists
First, note that with User Variable Lists, the `vl move` command **does not
work**. It only works with system variable lists.
We can create new user variable lists which build off old lists with `vl
create`. To add a new variable:
```stata
. vl create mylist3 = mylist2 + (gear_ratio)
note: $mylist3 initialized with 4 variables.
. vl list, user
--------------------------------------------------
Variable | Macro Values Levels
-----------+--------------------------------------
weight | $mylist1 integers >=0 64
mpg | $mylist1 integers >=0 21
weight | $mylist2 integers >=0 64
length | $mylist2 integers >=0 47
trunk | $mylist2 integers >=0 18
weight | $mylist3 integers >=0 64
length | $mylist3 integers >=0 47
trunk | $mylist3 integers >=0 18
gear_ratio | $mylist3 noninteger
--------------------------------------------------
. vl create mylist4 = mylist2 - (turn)
note: $mylist4 initialized with 3 variables.
. vl list, user
--------------------------------------------------
Variable | Macro Values Levels
-----------+--------------------------------------
weight | $mylist1 integers >=0 64
mpg | $mylist1 integers >=0 21
weight | $mylist2 integers >=0 64
length | $mylist2 integers >=0 47
trunk | $mylist2 integers >=0 18
weight | $mylist3 integers >=0 64
length | $mylist3 integers >=0 47
trunk | $mylist3 integers >=0 18
gear_ratio | $mylist3 noninteger
weight | $mylist4 integers >=0 64
length | $mylist4 integers >=0 47
trunk | $mylist4 integers >=0 18
--------------------------------------------------
```
Instead of adding (or removing) single variables at a time, we can instead add
or remove lists. Keeping with the comment above, you do *not* use `$` here to
refer to the list.
```stata
. vl create mylist5 = mylist2 - mylist1
note: $mylist5 initialized with 2 variables.
. vl list mylist5
------------------------------------------------
Variable | Macro Values Levels
---------+--------------------------------------
length | $mylist5 integers >=0 47
trunk | $mylist5 integers >=0 18
------------------------------------------------
```
However, if we want to simply modify an existing list, a better approach would
be the `vl modify` command. `vl create` and `vl modify` are similar to
`generate` and `replace`; the former creates a new variable list while the later
changes an existing variable list, but the syntax right of the `=` is the same.
```stata
. vl modify mylist3 = mylist3 + (headroom)
note: 1 variable added to $mylist3.
. vl modify mylist3 = mylist3 - (weight)
note: 1 variable removed from $mylist3.
```
### Dropping variable list
Variable lists can be dropped via `vl drop`
```stata
. vl dir, user
-------------------------------------------------------------------------------
| Macro's contents
|------------------------------------------------------------
Macro | # Vars Description
------------------+------------------------------------------------------------
User |
$mylist1 | 2 Related to gas consumption
$mylist2 | 3 Related to size
$mylist3 | 4 variables
$mylist4 | 3 variables
$mylist5 | 2 variables
-------------------------------------------------------------------------------
. vl drop mylist4 mylist5
. vl dir, user
-------------------------------------------------------------------------------
| Macro's contents
|------------------------------------------------------------
Macro | # Vars Description
------------------+------------------------------------------------------------
User |
$mylist1 | 2 Related to gas consumption
$mylist2 | 3 Related to size
$mylist3 | 4 variables
-------------------------------------------------------------------------------
```
System lists cannot be dropped; if you run `vl drop vlcontinuous` it just
removes all the variables from it.
### Using Variable Lists
To be explicit, we can use variable lists in any command which would take the
variables in that list. For example,
```stata
. describe $mylist3
Variable Storage Display Value
name type format label Variable label
-------------------------------------------------------------------------------
length int %8.0g Length (in.)
trunk int %8.0g Trunk space (cu. ft.)
gear_ratio float %6.2f Gear ratio
headroom float %6.1f Headroom (in.)
. describe $vlcategorical
Variable Storage Display Value
name type format label Variable label
-------------------------------------------------------------------------------
rep78 int %8.0g Repair record 1978
```
We can also use them in a modeling setting.
```stata
. regress mpg $mylist3
Source | SS df MS Number of obs = 74
-------------+---------------------------------- F(4, 69) = 30.77
Model | 1565.65298 4 391.413244 Prob > F = 0.0000
Residual | 877.806484 69 12.7218331 R-squared = 0.6408
-------------+---------------------------------- Adj R-squared = 0.6199
Total | 2443.45946 73 33.4720474 Root MSE = 3.5668
------------------------------------------------------------------------------
mpg | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
length | -.1837962 .0327629 -5.61 0.000 -.2491564 -.1184361
trunk | -.0103867 .1627025 -0.06 0.949 -.3349693 .3141959
gear_ratio | 1.526952 1.27546 1.20 0.235 -1.017521 4.071426
headroom | .0136375 .6602514 0.02 0.984 -1.303528 1.330803
_cons | 51.33708 8.300888 6.18 0.000 34.77727 67.8969
------------------------------------------------------------------------------
```
However, we'll run into an issue here - how to specify categorical variables or
interactions? The `vl substitute` command creates "factor-variable lists" that
can include factor variable indicators (`i.`), continuous variable indicators
(`c.`), and interactions (`#` or `##`). (The name "factor-variable list" is
slightly disingenuous; you could create a "factor-variable list" that includes
no actual factors, for example, if you wanted to interact two continuous
variables.)
Creating a factor-varible list via `vl substitute` can be done by specifying
variables or variable lists.
```stata
. vl substitute sublist1 = mpg mylist3
. display "$sublist1"
mpg length trunk gear_ratio headroom
. vl dir
-------------------------------------------------------------------------------
| Macro's contents
|------------------------------------------------------------
Macro | # Vars Description
------------------+------------------------------------------------------------
System |
$vldummy | 1 0/1 variable
$vlcategorical | 1 categorical variable
$vlcontinuous | 9 continuous variables
$vluncertain | 0 perhaps continuous, perhaps categorical variables
$vlother | 0 all missing or constant variables
User |
$mylist1 | 2 Related to gas consumption
$mylist2 | 3 Related to size
$mylist3 | 4 variables
$sublist1 | factor-variable list
-------------------------------------------------------------------------------
```
Note the use of `display "$listname"` instead of `vl list`. Factor-variable
lists are not just lists of vairables, they also can include the features above,
so must be displayed. Note that in the `vl dir`, "sublist1" has no number of
variables listed, making it stand apart.
We can make this more interesting by actually including continuous/factor
indicatores and/or interactions.
```stata
. vl substitute sublist2 = c.mylist1##i.vldummy
. display "$sublist2"
weight mpg i.foreign i.foreign#c.weight i.foreign#c.mpg
```
Note the need to specify that mylist1 is continuous (with `c.`). It follows the
normal convention that Stata assumes predictors in a model are continuous by
default, unless they're invloved in an interaction, in which case it assumes
they are factors by default.
```stata
. regress price $sublist2
Source | SS df MS Number of obs = 74
-------------+---------------------------------- F(5, 68) = 16.82
Model | 351163805 5 70232760.9 Prob > F = 0.0000
Residual | 283901591 68 4175023.4 R-squared = 0.5530
-------------+---------------------------------- Adj R-squared = 0.5201
Total | 635065396 73 8699525.97 Root MSE = 2043.3
------------------------------------------------------------------------------
price | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
weight | 4.415037 .8529259 5.18 0.000 2.71305 6.117024
mpg | 237.691 125.0383 1.90 0.062 -11.81907 487.201
|
foreign |
Foreign | 8219.603 7265.713 1.13 0.262 -6278.902 22718.11
|
foreign#|
c.weight |
Foreign | .7408054 1.647504 0.45 0.654 -2.546738 4.028348
|
foreign#|
c.mpg |
Foreign | -257.4683 155.426 -1.66 0.102 -567.616 52.67938
|
_cons | -13285.44 5149.648 -2.58 0.012 -23561.41 -3009.481
------------------------------------------------------------------------------
```
#### Updating factor-variable Lists
Factor-variable lists cannot be directly modified.
```stata
. display "$sublist1"
mpg length trunk gear_ratio headroom
. vl modify sublist1 = sublist1 - mpg
sublist1 not allowed
vlusernames containing factor variables not allowed in this context
r(198);
```
However, if you create a factor-variable list using only other variable lists,
if those lists get updated, so does the factor-variable list!
```stata
. vl create continuous = (turn trunk)
note: $continuous initialized with 2 variables.
. vl create categorical = (rep78 foreign)
note: $categorical initialized with 2 variables.
. vl substitute predictors = c.continuous##i.categorical
. display "$predictors"
turn trunk i.rep78 i.foreign i.rep78#c.turn i.foreign#c.turn i.rep78#c.trunk i.
> foreign#c.trunk
. vl modify continuous = continuous - (trunk)
note: 1 variable removed from $continuous.
. quiet vl rebuild
. display "$predictors"
turn i.rep78 i.foreign i.rep78#c.turn i.foreign#c.turn
```
Note the call to `vl rebuild`. Among other things, it will re-generate the
factor-variable lists. (It produces a `vl dir` output without an option to
suppress it, hence the use of
[`quiet`](05-programming.qmd#quieting-the-output).)
### Stored Statistics
You may have noticed that certain characteristics of the variable are reported.
```stata
. vl list mylist3
--------------------------------------------------
Variable | Macro Values Levels
-----------+--------------------------------------
headroom | $mylist3 noninteger
trunk | $mylist3 integers >=0 18
length | $mylist3 integers >=0 47
gear_ratio | $mylist3 noninteger
--------------------------------------------------
```
This reports some characteristics of the variables (integer, whether it's