forked from ThomasGrund/nwcommands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwaddnodes.ado
executable file
·104 lines (89 loc) · 2.48 KB
/
nwaddnodes.ado
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
*! Date : 24aug2014
*! Version : 1.0
*! Author : Thomas Grund, Linkoping University
*! Email : [email protected]
capture program drop nwaddnodes
program nwaddnodes
syntax [anything(name=netname)], newnodes(integer) [vars(string) labs(string) generate(string)]
_nwsyntax `netname', max(1)
if "`generate'" != "" {
nwduplicate `netname', name(`generate')
_nwsyntax `generate', max(1)
}
if "`stub'" == "" {
local stub = "new"
}
if ("`vars'" == "") {
// Generate temporary varlist and check for each variable if it already exists.
local vars = ""
local invalid = 0
forvalues i=1/`newnodes' {
local vars "`vars' `stub'`i'"
capture confirm variable `stub'`i'
if !_rc {
local invalid = `invalid' + 1
}
}
// Finds valid Stata variable names to store network.
if `invalid' > 0 {
local stub_add = 0
while `invalid' > 0 {
local vars = ""
local stub_add = `stub_add' + 1
local invalid = 0
forvalues i=1/`newnodes' {
local vars "`vars' `stub'`stub_add'_`i'"
capture confirm variable `stub'`stub_add'_`i'
if !_rc {
local invalid = `invalid' + 1
}
}
}
}
}
else {
preserve
drop _all
nwload `netname'
foreach onevar in `vars' {
capture confirm variable `onevar'
if (_rc == 0) {
di "{err}Node variable `onevar' already in use. Choose option vars() differently."
error 6070
}
}
local vars_count : word count `vars'
if (`vars_count' != `newnodes') {
di "{err}Wrong number of new variables in option vars()."
error 6070
}
restore
}
local newnodes_orig + `newnodes'
local newnodes = `nodes' + `newnodes'
nwtomata `netname', mat(oldmat)
mata: newmat = J(`newnodes',`newnodes', 0)
mata: newmat[|1,1 \ `nodes',`nodes'|] = oldmat
scalar onevars = "\$nw_`id'"
local oldvars `=onevars'
capture drop `oldvars'
scalar onelabs = "\$nwlabs_`id'"
local oldlabs `=onelabs'
//mata: mata drop nw_mata`id'
mata: nw_mata`id' = newmat
global nwsize_`id' = `newnodes'
global nw_`id' "`oldvars' `vars'"
local wc : word count `labs'
local overlap : list labs & oldlabs
local oc : word count `overlap'
if (`wc' == `newnodes_orig' & `oc' == 0) {
global nwlabs_`id' "`oldlabs' `labs'"
}
else {
global nwlabs_`id' "`oldlabs' `vars'"
}
nwload `netname'
mata: mata drop newmat
end
*! v1.5.0 __ 17 Sep 2015 __ 13:09:53
*! v1.5.1 __ 17 Sep 2015 __ 14:54:23