[131] | urates.dta | |||
[142] | urates.dta | |||
[227] | urates.dta | |||
master.do:15 | ||||
→ | ch13.01.do | ↘ | ||
calcextremamata |
[8] | |||
centervmata |
[80] | |||
avgpermata |
[130] | |||
aggregmata |
[188] | |||
gbpvalmata |
[240] | |||
gbpval2mata |
[244] | |||
gbpval3mata |
[248] |
ch13.01.do open
file:script:do
# | content |
---|---|
1 [+] | // itsp 13.00- |
3 [+] | capt program drop _all |
4 [+] | mata: mata clear |
6 [+] | type varextrema.ado |
8 [+] | do calcextrema.mata |
10 [+] | type calcextrema.mata |
12 [+] | sysuse auto , |
13 [+] | varextrema price if foreign |
14 [+] | return list |
/* *! centervars 1.0.1 CFBaum 08oct2007 program centervars, rclass version 10.1 syntax varlist(numeric) [if] [in] marksample touse quietly count if `touse' if `r(N)' == 0 error 2000 mata: centerv("`varlist'", "`touse'") end |
|
28 [+] | mata block (8 lines) |
39 [+] | sysuse auto , |
40 [+] | centervars price mpg |
41 [+] | summarize price mpg |
42 [+] | */ |
44 [+] | program drop _all |
45 [+] | mata: mata clear |
47 [+] | type centervars.ado |
/* version 10.1 |
|
67 [+] | mata block (10 lines) |
*/ |
|
78 [+] | type centerv.mata |
80 [+] | do centerv.mata |
82 [+] | sysuse auto , |
83 [+] | centervars price mpg , |
84 [+] | centervars price mpg if foreign , |
85 [+] | summarize c* |
88 [+] | program drop _all |
89 [+] | mata: mata clear |
90 [+] | type avgper.ado |
93 [+] | type avgper.mata |
95 [+] | /* *! avgper 1.0.0 CFBaum 08oct2007 program avgper, rclass version 10.1 syntax varlist(max=1 numeric) [if] [in], per(integer) // honor if and in conditions if provided marksample touse quietly count if `touse' if `r(N)' == 0 { error 2000 } local newvar = "`varlist'" + "A" + string(`per') quietly generate `newvar' = . * pass the varname and newvarname to Mata mata: avgper("`varlist'", "`newvar'", `per', "`touse'") end */ |
/* version 10.1 |
|
115 [+] | mata block (13 lines) |
128 [+] | */ |
130 [+] | do avgper.mata |
131 [+] | use urates , |
132 [+] | tsset |
133 [+] | avgper tenn , // calculate quarterly averages |
134 [+] | avgper tenn , // calculate annual averages |
135 [+] | summarize tenn* |
137 [+] | tsmktim quarter , // create quarterly calendar var |
138 [+] | tsmktim year , // create annual calendar var |
139 [+] | list t tenn quarter tennA3 year tennA12 in 1/12 , |
142 [+] | use urates , |
143 [+] | foreach v of varlist tenn-arkansas { |
144 [+] | avgper `v' , |
145 [+] | } |
146 [+] | summarize tenn-arkansasA3 , |
148 [+] | type aggreg.ado |
/* program aggreg, rclass version 10.1 syntax varlist(max=1 numeric) [if] [in], per(integer) [func(string)] marksample touse quietly count if `touse' if `r(N)' == 0 { error 2000 } * validate per versus selected sample if `per' <= 0 | `per' >= `r(N)' { display as error "per must be > 0 and < N of observations." error 198 } if mod(`r(N)',`per' != 0) { display as error "N of observations must be a multiple of per." error 198 } * validate func option; default is average (code A) local ops A S F L local opnames average sum first last if "`func'" == "" { local op "A" } else { local nop : list posof "`func'" in opnames if !`nop' { display as err "Error: func must be chosen from `opnames'" error 198 } local op : word `nop' of `ops' } * validate the new varname local newvar = "`varlist'`op'`per'" quietly generate `newvar' = . * pass the varname and newvarname to Mata mata: aggreg("`varlist'", "`newvar'", `per', "`op'", "`touse'") end */ |
|
188 [+] | do aggreg.mata |
190 [+] | type aggreg.mata |
/* version 10.1 |
|
194 [+] | mata block (28 lines) |
222 [+] | */ |
* webuse urates, clear |
|
225 [+] | clear |
227 [+] | use urates , |
228 [+] | aggreg tenn , // calculate quarterly averages |
229 [+] | aggreg tenn , // calculate quarterly sum |
230 [+] | aggreg tenn , // extract first month's value |
231 [+] | aggreg tenn , // extract first month's value |
232 [+] | tsmktim quarter , // create quarterly calendar var |
233 [+] | list t tenn quarter tennA3 tennS3 tennF3 tennL3 in 1/12 , |
235 [+] | /* // save the compiled averageper function mata: mata mosave averageper(), replace */ |
240 [+] | do gbpval.mata |
242 [+] | type gbpval.mata |
244 [+] | do gbpval2.mata |
246 [+] | type gbpval2.mata |
248 [+] | do gbpval3.mata |
250 [+] | type gbpval3.mata |
// itsp 13.00- capt program drop _all mata: mata clear type varextrema.ado do calcextrema.mata type calcextrema.mata sysuse auto, clear varextrema price if foreign return list /* *! centervars 1.0.1 CFBaum 08oct2007 program centervars, rclass version 10.1 syntax varlist(numeric) [if] [in] marksample touse quietly count if `touse' if `r(N)' == 0 error 2000 mata: centerv("`varlist'", "`touse'") end version 10.1 mata: void centerv(string scalar varlist, /// string scalar touse) { st_view(X=., ., tokens(varlist), touse) X[ , ] = X:- mean(X) } end sysuse auto, clear centervars price mpg summarize price mpg */ program drop _all mata: mata clear type centervars.ado /* *! centervars 1.0.0 CFBaum 08oct2007 program centervars, rclass version 10.1 syntax varlist(numeric) [if] [in], GENerate(string) [DOUBLE] marksample touse quietly count if `touse' if `r(N)' == 0 error 2000 foreach v of local varlist { confirm new var `generate'`v' generate `double' `generate'`v' = . local newvars "`newvars' `generate'`v'" } mata: centerv("`varlist'", "`newvars'", "`touse'") end */ /* version 10.1 mata: void centerv(string scalar varlist, /// string scalar newvarlist, string scalar touse) { st_view(X=., ., tokens(varlist), touse) st_view(Z=., ., tokens(newvarlist), touse) Z[ , ] = X:- mean(X) } end */ type centerv.mata do centerv.mata sysuse auto, clear centervars price mpg, gen(c_) centervars price mpg if foreign, gen(cf_) double summarize c* program drop _all mata: mata clear type avgper.ado type avgper.mata /* *! avgper 1.0.0 CFBaum 08oct2007 program avgper, rclass version 10.1 syntax varlist(max=1 numeric) [if] [in], per(integer) // honor if and in conditions if provided marksample touse quietly count if `touse' if `r(N)' == 0 { error 2000 } local newvar = "`varlist'" + "A" + string(`per') quietly generate `newvar' = . * pass the varname and newvarname to Mata mata: avgper("`varlist'", "`newvar'", `per', "`touse'") end */ /* version 10.1 mata: void avgper(string scalar vname, string scalar newvname, real scalar per, string scalar touse) { real matrix v1, v3 st_view(v1=., ., vname, touse) st_view(v2=., ., newvname) v3 = colshape(v1',per) * J(per, 1, 1/per) v2[(1::rows(v3)),] = v3 } end */ do avgper.mata use urates, clear tsset avgper tenn, per(3) // calculate quarterly averages avgper tenn, per(12) // calculate annual averages summarize tenn* tsmktim quarter, start(1978q1) // create quarterly calendar var tsmktim year, start(1978) // create annual calendar var list t tenn quarter tennA3 year tennA12 in 1/12, sep(3) use urates, clear foreach v of varlist tenn-arkansas { avgper `v', per(3) } summarize tenn-arkansasA3, sep(6) type aggreg.ado /* program aggreg, rclass version 10.1 syntax varlist(max=1 numeric) [if] [in], per(integer) [func(string)] marksample touse quietly count if `touse' if `r(N)' == 0 { error 2000 } * validate per versus selected sample if `per' <= 0 | `per' >= `r(N)' { display as error "per must be > 0 and < N of observations." error 198 } if mod(`r(N)',`per' != 0) { display as error "N of observations must be a multiple of per." error 198 } * validate func option; default is average (code A) local ops A S F L local opnames average sum first last if "`func'" == "" { local op "A" } else { local nop : list posof "`func'" in opnames if !`nop' { display as err "Error: func must be chosen from `opnames'" error 198 } local op : word `nop' of `ops' } * validate the new varname local newvar = "`varlist'`op'`per'" quietly generate `newvar' = . * pass the varname and newvarname to Mata mata: aggreg("`varlist'", "`newvar'", `per', "`op'", "`touse'") end */ do aggreg.mata type aggreg.mata /* version 10.1 mata: void aggreg(string scalar vname, string scalar newvname, real scalar per, string scalar op, string scalar touse) { real colvector mult if (op=="A") { mult = J(per, 1, 1/per) } else if (op=="S") { mult = J(per, 1, 1) } else if (op=="F") { mult = J(per, 1, 0) mult[1] = 1 } else if (op=="L") { mult = J(per, 1, 0) mult[per] = 1 } st_view(v1=., ., vname, touse) st_view(v2=., ., newvname) v3 = colshape(v1', per) * mult v2[(1::rows(v3)),] = v3 } end */ * webuse urates, clear clear use urates, clear aggreg tenn, per(3) // calculate quarterly averages aggreg tenn, per(3) func(sum) // calculate quarterly sum aggreg tenn, per(3) func(first) // extract first month's value aggreg tenn, per(3) func(last) // extract first month's value tsmktim quarter, start(1978q1) // create quarterly calendar var list t tenn quarter tennA3 tennS3 tennF3 tennL3 in 1/12, sep(3) /* // save the compiled averageper function mata: mata mosave averageper(), replace */ do gbpval.mata type gbpval.mata do gbpval2.mata type gbpval2.mata do gbpval3.mata type gbpval3.mata