Setup initial file structure

This commit is contained in:
2024-03-05 11:20:30 +01:00
parent 6183a6391b
commit 70e53e7760
708 changed files with 277486 additions and 0 deletions

View File

@ -0,0 +1,58 @@
* !galbr.ado AT v2.0, Marc 2000 (STB-56: sbe20.1)
* galbr.ado AT v1.15, June/October 1997
program def galbr
version 6.0
syntax varlist(min=2 max=2 numeric) [if] [in] [, id(varname) `options' *]
tokenize `varlist'
local E `1'
local SE `2'
preserve
if ("`if'"!="") {
qui keep `if'
}
if ("`in'"!="") {
qui keep `in'
}
if "`id'"=="" {
local id "O"
}
else {
local id "[`id']"
}
tempvar x y p ulp llp
qui gen `x'=1/`SE'
qui gen `y'=`E'/`SE'
label var `x' "1/se(b)"
label var `y' "b/se(b)"
qui reg `y' `x', noconstant
qui predict `p'
qui gen `llp'=`p'-2
qui gen `ulp'=`p'+2
qui sum `x', detail
local mxx=r(max)
qui sum `llp', detail
if r(min)>=-2 {
local mny=-2
}
else {
local mny=r(min)
}
qui sum `ulp', detail
if _result(6)<=2 {
local mxy=2
}
else {
local mxy=r(max)
}
local new=_N+1
qui set obs `new'
qui replace `y'=0 in l
qui replace `x'=0 in l
qui replace `p'=0 in l
qui replace `llp'=-2 in l
qui replace `ulp'=2 in l
graph `y' `p' `ulp' `llp' `x', ylab(`mny',-2,0,2,`mxy') /*
*/ xscale(0,`mxx') ysca(`mny',`mxy') pen(1922) /*
*/ sy(`id'iii) c(.lll) sort `options'
end

View File

@ -0,0 +1,57 @@
/*
*! galbr dialog version 1.0.0, 13 May 2003, T. J. Steichen, steichen@triad.rr.com
*! for galbr version 2.0, Mar 2000, Aurelio Tobias, atobias@isciii.es
Galbraith Plot for Heterogeneity
--------------------------------
galbr theta setheta [if exp] [in range] [ , id(strvar) graph_options ]
To install in User Statistics menu via Stata commands:
. window menu append item "stUserStatistics" "Galbraith Plot for Heterogeneity (&galbr)" "db galbr"
. window menu refresh
To permanently install, place the commands in your -profile.do- file.
*/
VERSION 8.0
INCLUDE _std_small
INCLUDE header
HELP hlp1, view("help galbr")
RESET res1, label("Reset")
DIALOG main, label("galbr 2.0 - Galbraith Plot for Heterogeneity") tabtitle("Main")
BEGIN
TEXT tx_se 10 10 330 ., ///
label("Vars for theta, se(theta), in that order")
VARLIST vl_se @ _ss @ ., ///
label("Vars for theta, se(theta)")
CHECKBOX cb_id 10 70 100 ., ///
label("ID Variable:") ///
onclickon(main.vn_id.enable) ///
onclickoff(main.vn_id.disable)
VARNAME vn_id 110 70 230 ., ///
label("ID Variable") ///
option("id")
GROUPBOX gb_gopts7 10 155 330 _ht1h, ///
label("Allowed Graph7 Options:")
EDIT ed_gopts7 15 175 320 ., ///
label("Graph7 Options")
END
INCLUDE ifin
PROGRAM command
BEGIN
put "galbr "
varlist main.vl_se
INCLUDE _ifin_pr
beginoptions
optionarg main.vn_id
put main.ed_gopts7
endoptions
END

View File

@ -0,0 +1,74 @@
.-
help for ^galbr^ (STB-41: sbe20; STB-56: sbe20.1)
.-
Assessing heterogeneity in meta-analysis: the Galbraith plot
------------------------------------------------------------
^galbr^ theta setheta [^if^ exp] [^in^ range] [, ^id(^strvar^)^ graph_options]
Description
-----------
^galbr^ provides a graphical display to get a visual impression of the amount of
heterogeneity from a meta-analysis. For each trial, the z statistic
^theta/setheta^ is plotted against the reciprocal standard error ^1/setheta^.
The (unweighted) regression line constrained through the origin, with its 95%
confidence interval, has a slope equal to the overall log rate ratio,
or log odds ratio, or log hazard ratio in a fixed effects meta-analysis.
The position of each trial on the horizontal axis gives an indication of the
weight allocated to it in a meta-analysis. The position on the vertical axis
gives the contribution of each trial to the Q statistic for heterogeneity.
In the absence of heterogeneity we could expect all the points to lie within
the confidence bounds (positioned 2 units over and below the regression line).
^theta^ is the effect estimated from the individual study, and ^setheta^ is
its standard error. For example theta might be a difference in means, a
log rate ratio, a log odds ratio or a log hazard ratio.
If you have a dataset which contains data for all studies, then the @byvar@
command can be used to derive the effect estimates and standard errors for the
individual studies. For example:
. ^sort study^
. ^byvar study, coef(group) se(group) generate:^
. ^quietly poisson cases group, e(pyrs)^
. ^sort study^
. ^qui by study: keep if _n==1^
. ^rename _C_1 logrr^
. ^rename _S_1 se^
. ^galbr logrr se, id(study) yline(0)^
Alternatively, the @collapse@ command may be useful.
Options
-------
Graph options are allowed, but ^ylabel()^, ^yscale()^, ^xscale()^, ^symbol()^
are not suggested.
^id(^labelvar^)^ supplied any variable, which is used to label the studies.
If the data contains a labeled numeric variable, it can also be used.
^yline(^0^)^ is a useful to check it with the direction and intensity of the
overall effect estimated in a fixed effects meta-analysis by the slope
of the (unweighted) regression line constrained through the origin.
Author
-------
Aurelio Tobias
Universidad Miguel Hernandez, Alicante, Spain
email: bledatobias@@ctv.es
Also see
--------
STB: STB-41 sbe20, STB-38 sbe16
On-line: help for @graph@, @byvar@, @collapse@, @for@, @meta@ (if installed),
@metareg@ (if installed), @metabias@ (if installed), @metacum@ (if
installed), @metainf@ (if installed)

View File

@ -0,0 +1,38 @@
*! Version 2.0, October 2002. (SJ4-1: st0057)
program define ghquad
version 7.0
syntax [varlist(min=2 max=2)] [, N(integer 10)]
tokenize `varlist'
local x "`1'"
local w "`2'"
if `n' + 2 > _N {
di in red /*
*/ "`n' + 2 observations needed to compute quadrature points"
exit 2001
}
tempname xx ww
local i 1
local m = int((`n' + 1)/2)
while `i' <= `m' {
if `i' == 1 {
scalar `xx' = sqrt(2*`n'+1)-1.85575*(2*`n'+1)^(-1/6)
}
else if `i' == 2 { scalar `xx' = `xx'-1.14*`n'^0.426/`xx' }
else if `i' == 3 { scalar `xx' = 1.86*`xx'-0.86*`x'[1] }
else if `i' == 4 { scalar `xx' = 1.91*`xx'-0.91*`x'[2] }
else { scalar `xx' = 2*`xx'-`x'[`i'-2] }
hermite `n' `xx' `ww'
qui replace `x' = `xx' in `i'
qui replace `w' = `ww' in `i'
local i = `i' + 1
}
if mod(`n', 2) == 1 { qui replace `x' = 0 in `m' }
qui replace `x' = -`x'[`n'+1-_n] in `i'/`n'
qui replace `w' = `w'[`n'+1-_n] in `i'/`n'
end
exit

View File

@ -0,0 +1,85 @@
program define ghquadm
* stolen from gllamm6 who stole it from rfprobit (Bill Sribney)
version 4.0
parse "`*'", parse(" ")
local n = `1'
if `n' + 2 > _N {
di in red /*
*/ "`n' + 2 observations needed to compute quadrature points"
exit 2001
}
tempname x w xx ww a b
local i 1
local m = int((`n' + 1)/2)
matrix x = J(1,`m',0)
matrix w = x
while `i' <= `m' {
if `i' == 1 {
scalar `xx' = sqrt(2*`n'+1)-1.85575*(2*`n'+1)^(-1/6)
}
else if `i' == 2 { scalar `xx' = `xx'-1.14*`n'^0.426/`xx' }
else if `i' == 3 { scalar `xx' = 1.86*`xx'-0.86*x[1,1] }
else if `i' == 4 { scalar `xx' = 1.91*`xx'-0.91*x[1,2] }
else {
local im2 = `i' -2
scalar `xx' = 2*`xx'-x[1,`im2']
}
hermite `n' `xx' `ww'
matrix x[1,`i'] = `xx'
matrix w[1,`i'] = `ww'
local i = `i' + 1
}
if mod(`n', 2) == 1 { matrix x[1,`m'] = 0}
/* start in tails */
matrix `b' = (1,1)
matrix w = w#`b'
matrix w = w[1,1..`n']
matrix `b' = (1,-1)
matrix x = x#`b'
matrix x = x[1,1..`n']
/* other alternative (left to right) */
/*
above: matrix x = J(1,`n',0)
while ( `i'<=`n'){
matrix x[1, `i'] = -x[1, `n'+1-`i']
matrix w[1, `i'] = w[1, `n'+1-`i']
local i = `i' + 1
}
*/
matrix `2' = x
matrix `3' = w
end
program define hermite /* integer n, scalar x, scalar w */
* stolen from gllamm6 who stole it from rfprobit (Bill Sribney)
version 4.0
local n "`1'"
local x "`2'"
local w "`3'"
local last = `n' + 2
tempname i p
matrix `p' = J(1,`last',0)
scalar `i' = 1
while `i' <= 10 {
matrix `p'[1,1]=0
matrix `p'[1,2] = _pi^(-0.25)
local k = 3
while `k'<=`last'{
matrix `p'[1,`k'] = `x'*sqrt(2/(`k'-2))*`p'[1,`k'-1] /*
*/ - sqrt((`k'-3)/(`k'-2))*`p'[1,`k'-2]
local k = `k' + 1
}
scalar `w' = sqrt(2*`n')*`p'[1,`last'-1]
scalar `x' = `x' - `p'[1,`last']/`w'
if abs(`p'[1,`last']/`w') < 3e-14 {
scalar `w' = 2/(`w'*`w')
exit
}
scalar `i' = `i' + 1
}
di in red "hermite did not converge"
exit 499
end

View File

@ -0,0 +1,22 @@
.-
help for ^ghquadm^
.-
Gauss-Hermite weights and absisae
---------------------------------
^ghquadm^ n a w
Description
-----------
^ghquadm^ produces the weights (w) and absisae (a) -- stored as 2 matrices --
to perform Gauss-Hermite quadrature with n quadrature points. This is a
modification of the @gllamm6@ modification of Bill Sribney's ^ghquad^.
Examples
--------
. ^ghquadm 12 abs weight^

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,497 @@
.-
help for ^gllamm^
.-
Generalised linear latent and mixed models
-------------------------------------------
^gllamm^ depvar [varlist] [^if^ exp] [^in^ range] ^,^ ^i(^varlist^)^
[ ^nocons^tant ^o^ffset^(^varname^)^ ^nr^f^(^#^,^...^,^#^)^
^e^qs^(^eqnames^)^ ^frload^(^#^,^...^,^#^)^
^ip(^string^)^ ^ni^p^(^#^,^...^,^#^)^ ^pe^qs^(^eqname^)^
^bmat^rix^(^matrix^)^ ^ge^qs^(^eqnames^)^ ^nocor^rel
^c^onstraints^(^clist^)^ ^we^ight^(^varname^)^ ^pwe^ight^(^varname^)^
^f^amily^(^family^)^ ^fv(^varname^)^ ^de^nom^(^varname^)^
^s(^eqname^)^ ^l^ink^(^link^)^ ^lv(^varname^)^
^expa^nded^(^varname varname string^)^ ^b^asecategory^(^#^)^
^comp^osite^(^varnames^)^ ^th^resh^(^eqnames^)^ ^eth^resh^(^eqnames^)^
^fr^om^(^matrix^)^ ^copy^ ^skip^ ^long^
^lf0(^#^ ^#^)^ ^ga^teaux^(^#^ ^#^ ^#^)^ ^se^arch^(^#^)^
^noe^st ^ev^al ^in^it ^it^erate^(^#^)^ ^adoonly^ ^adapt^
^rob^ust ^clu^ster^(^varname^)^
^l^evel^(^#^)^ ^eform^ ^allc^ ^tr^ace ^nolo^g ^nodis^play ^do^ts
]
where family is and link is
^gau^ssian ^id^entity
^poi^sson ^log^
^gam^ma ^rec^iprocal
^bin^omial ^logi^t
^pro^bit
^cll^ (complementary log-log)
^ll^ (log-log)
^olo^git (o stands for ordinal)
^opr^obit
^ocl^l
^mlo^git
^spr^obit (scaled probit)
^sop^robit
and clist is of the form #[^-^#][^,^ #[^-^#] ...]
^gllamm^ shares the features of all estimation commands; see help @est@.
^gllamm^ typed without arguments redisplays previous results.
Predictions of the latent variables or random effects (and many other
quantities) can be obtained using @gllapred@ and the models can be
simulated using @gllasim@
Description
-----------
^gllamm^ estimates ^G^eneralized ^L^inear ^L^atent ^A^nd ^M^ixed ^M^odels.
These models include multilevel (hierarchical) regression models
with an arbitrary number of levels, generalized linear mixed models,
multilevel factor models and some types of latent class models.
We refer to the random effects (random intercepts, slopes or coefficients),
factors, etc. as latent variables or random effects.
If the latent variables are assumed to be multivariate normal,
^gllamm^ uses Gauss-Hermite quadrature, or adaptive quadrature
if the ^adapt^ option is also specified. Adaptive quadrature
can be considerably more accurate than ordinary quadrature,
see first reference at the bottom of this help file.
With the ^ip(^f^)^ option, the latent variables are specified
as discrete with freely estimated probabilities (masses) and locations.
More information on the models is available from
http://www.gllamm.org
Options
--------
(a) Structure of the model
---------------------------------------------------------------------------
^i(^varlist^)^ gives the variables that define the hierarchical, nested
clusters, from the lowest level (finest clusters) to the highest level,
e.g. i(pupil class school).
^noconstant^ omits the constant term from the fixed effects equation.
^offset(^varname^)^ specifies a variable to be added to the linear predictor
without estimating a corresponding regression coefficient (e.g. log
exposure for Poisson regression).
^nrf(^#^,^...^,^#^)^ specifies the number of random effects for each level,
i.e., for each variable in ^i(^varlist^)^. The default is nrf(1,...,1).
^eqs(^eqnames^)^ specifies equation names (defined before running gllamm)
for the linear predictors multiplying the latent variables; see help @eq_g@.
If required, constants should be explicitly included in the equation
definitions using variables equal to 1. If the option is not used, the
latent variables are assumed to be random intercepts and only one random
effect is allowed per level. The first lambda coefficient is set to one
unless the ^frload()^ option is specified. The other coefficients are
estimated together with the (co)variance(s) of the random effect(s).
^frload(^#^,^...^,^#^)^ lists the latent variables for which the first
factor loading should be freely estimated along with the other
factor loadings. It is up to the user to define appropriate constraints
to identify the model. Here the latent variables are referred to
as 1 2 3 etc. in the order in which they are defined by the ^eqs()^
option.
^ip(^sting^)^ if string is g, Gaussian quadrature points are used and if
string is f, the mass-points are freely estimated. The default is
Gaussian quadrature. The ^ip(^f^)^ option causes nip-1 locations to
be estimated, the nipth mass being determined by setting the mean
location to 0 so that an intercept can be included in the fixed
effects equation. The ^ip(^fn^)^ option can be used to set the last mass
to 0 instead of to the mean. If string is m, spherical quadrature rules
are used for multidimensional integrals.
^nip(^#^,^...^,^#^)^ specifies the number of integration points or masses
to be used for each integral or summation. When quadrature is used,
a value may be given for each random effect. When freely estimated masses
are used, a value may be given for each level of the model. If only one
argument is given, the same number of integration points will be used for
each summation. Combined with the ^ip(m)^ option, ^nip()^ specifies
the degree of the approximation instead of the number of points. Only the
following degrees are available: for two random effects, 5, 7, 9, 11, 15
and for more than two random effects 5, 7.
^peqs(^eqname^)^ can be used with the ^ip(^f^)^ or ^ip(^fn^)^ options
to allow the (prior) latent class probabilities to depend on covariates.
The model for the latent class probabilities is a multinomial logit model
with the last latent class as reference category. A constant is
automatically included in addition to the covariates specified in the
equation command; see help @eq_g@.
^geqs(^eqnames^)^ specifies regressions of latent variables on explanatory variables.
The second character of the equation name indicates which latent
variable is regressed on the variables used in the equation definition, e.g.
eq f1: a b means that the first latent variable is regressed on a and b (without
a constant); see help @eq_g@.
^bmatrix(^matrix^)^ specifies a matrix B of regression coefficients for the
dependence of the latent variables on other latent variables. The matrix
must be upper diagonal and have number of rows and columns equal to the
total number of random effects.
^nocorrel^ may be used to constrain all correlations to zero
if there are several random effects at any of the levels and if these are
modeled as multivariate normal.
^constraint(^clist^)^ specifies the constraint numbers of the constraints to
be applied. Constraints are defined using the ^constraint^ command; see
help @constraint@. To find out the equation names needed to specify the
constraints, run gllamm with the noest option.
^weight(^varname^)^ specifies that variables varname1, varname2, etc. contain
frequency weights. The suffixes determine at what level each weight applies.
For example, if the level 1 units are subjects, the level 2 units are
families, and the result is binary, we can collapse dataset A into
dataset B as follows:
A B
family subject result family subject result wt1 wt2
1 1 0 1 1 0 2 1
1 2 0 2 3 1 1 2
2 3 1 2 4 0 1 2
2 4 0
3 5 1
3 6 0
The level 1 weight, wt1, indicates that subject 1 in dataset B
represents two subjects within family 1 in dataset A, whereas subjects
3 and 4 in dataset B represent single subjects within family 2 in
dataset A. The level 2 weight wt2 indicates that family 1 in dataset B
represents one family and family 2 represents two families, i.e. all
the data for family 2 are replicated once. Collapsing the data in this
way can make gllamm run considerably faster.
^pweight(^varname^)^ specifies that variables varname1, varname2, etc. contain
sampling weights for levels 1, 2, etc. As far as the estimates and
log-likelihood are concerned, the effect of specifying these
weights is the same as for frequency weights, but the standard errors
will be different. Robust standard errors will automatically be provided.
This should be used with caution if the sampling weights apply
to units at a lower level than the highest level in the multilevel model.
The weights are not rescaled; scaling is the responsibility of the user.
(b) Densities, links, etc. for the response model
------------------------------------------------------------------------------
^family(^families^)^ specifies the families to be used for the conditional
densities. The default is ^family(^gauss^)^. Several families may be given
in which case the variable allocating families to observations must be
given using ^fv(^varname^)^.
^fv(^varname^)^ is required if mixed responses requiring more than a single
family of conditional distributions are analyzed. The variable indicates
which family applies to which observation. A value of one refers to the
first family etc.
^denom(^varname^)^ gives the variable containing the binomial denominator for
the responses whose family is specified as binomial. The default
denominator is 1.
^s(^eqname^)^ specifies that the log of the standard deviation (or coefficient
of variation) at level 1 for normally (or gamma) distributed responses
should be given by the linear predictor defined by eqname. This is
necessary if the level-1 variance is heteroscedastic. For example, if
dummy variables for groups are used, different variances are estimated
for different groups.
^link(^link^)^ specifies the links to be used for the conditional densities. If
a single family is specified, the default link is the canonical link.
Several links may be given in which case the variable allocating links to
observations must be given using ^lv(^varname^)^. This option is currently
not available if the ordinal or mlogit links are used. Numerically
feasible choices of link depend upon the distributions of the covariates
and choice of conditional error and random effects distributions. The
sprobit link is only identified in special cases; it may be used for
Heckman-type selection models or to model floor or ceiling effects.
^lv(^varname^)^ is the variable whose values indicate which link applies to
which observation.
^expanded(^varname varname string^)^ is used together with the mlogit
link and specifies that the data have been expanded as illustrated
below:
A B
choice response altern selected
1 1 1 1
2 1 2 0
1 3 0
2 1 0
2 2 1
2 3 0
where the variable "choice" is the multinomial response
(possible values 1,2,3), the "response" labels the original lines
of data, "altern" gives the possible responses or alternatives
and "selected" is an indicator for the option that was selected.
The syntax would be expanded(response selected m) and the variable
"altern" would be used as the dependent variable. This expanded
form allows the user to use different random effects etc. for
different categories of the multinomial response. The third
argument is o if one set of coefficients should be estimated
for the explanatory variables and m if one set of coefficients
is to be estimated for each category of the response except the
reference category.
^basecategory^(^#^)^ When the mlogit link is used, this specifies the
value of the response to be used as the reference category. This option is
ignored if the expanded() option is used with the third argument equal
to m.
^composite^(varname varname varname [more varnames]) specifies that a
composite link is used. The first variable is a cluster identifier
("cluster" below) so that linear predictors within the cluster can
be combined into a single composite link. The second variable
("ind" below) indicates to which response the composite links defined
by the susequent weight variables belong. Observations with ind=0
have a missing link. The remaining variables ("c1" and "c2" below)
specify weights for the composite links. The composite link based on
the first weight variable will go to where ind=1, etc.
Example:
Data setup with form of inverse link Interpretation of
h_i determined by link() and lv(): composite(cluster ind c1 c2)
cluster ind c1 c2 inverse link cluster composite link
1 1 1 0 h_1 1 h_1 - h_2
1 2 -1 1 h_2 1 n_2 + h_3
1 0 0 1 h_3 ==> 1 missing
2 1 1 0 h_4 2 h_4 + h_5
2 2 1 1 h_5 2 h_5 + 2*h_6
2 0 0 2 h_6 2 missing
^thresh(^eqnames^)^ specifies equation(s) for the thresholds for ordinal
response(s); see help @eq_g@. One equation is specified for each
ordinal response. The purpose of this option is to allow the effects of some
covariates to be different for different categories of the ordinal variable
rather than assumming a constant effect - the proportional odds assumption
if the ologit link is used. Variables used in the model for the
thresholds cannot appear in the fixed part of the linear predictor.
^ethresh(^eqnames^)^ is the same as ^thresh(^eqnames^)^ except that
a different parameterization is used for the threshold model. To
ensure that k_{s-1} <= k_{s}, the model is k_{s} = k_{s-1} + exp(xb),
for response categories s=2,...,S.
(c) Starting values
-----------------------------------------------------------------------------
^from(^matrix^)^ specifies the matrix to be used for the initial values.
Note that the column-names and equation-names have to be correct
(see help @matrname@, @matrix@), unless the ^copy^ option is specified.
The matrix may be obtained from a previous estimation command using e(b).
This is useful if the number of quadrature points needs to be increased
or of a new explanatory variable is added. Use the ^skip^ option if
the matrix of has extra parameters.
^copy^ and ^skip^ see above.
^long^ may be used with the from(matrix) option when constraints are used
to indicate that the matrix of initial values has as many elements
as would be needed for the unconstrained model, i.e. more elements
than will be estimated.
^lf0(^# #^)^ gives the number of parameters and the log-likelihood for a
likelihood ratio test to compare the model to be estimated with a simpler
model. A likelihood ratio chi-squared test is only performed if the
^lf0(^# #^)^ option is used.
^gateaux(^min^,^max^,^n^)^ may be used with method ip(f) to increase the
number of mass-points by one from a previous solution with parameter
estimates specified using from(matrix) and number of parameters and
log-likelihood specified by lf0(# #). The program searches for the
location of the new mass-point by placing a very small mass at the
location given by the first argument and moving it to the second argument
in the number of steps specified by the third argument. (If there are
several random effects, this search is done in each dimension resulting
in a regular grid of search points.) If the maximum increase in likelihood
is greater than 0, the location corresponding to this maximum is used as
the initial value of the new location, otherwise the program stops. When
this happens, it can be shown that for certain models the current solution
represents the non-parametric maximum likelihood estimate.
^search(^#^)^ causes the program to search for initial values for the random
effects at level 2 (in range 0 to 3). The argument specifies the number
of random searches. This option may only be used with ^ip(^g^)^ and when
^fr^om^(^matrix^)^ is not used.
(d) Estimation and output options
------------------------------------------------------------------------------
^noest^ is used to prevent the program from carrying out the estimation. This
may be used with the trace option to check that the model is correct and
get the information needed to set up a matrix of initial values. Global
macros are available that are normally deleted. Particularly useful may
be M_initf and M_initr, matrices for the parameters (fixed part and
random part respectively).
^eval^ causes the program to simply evaluate the loglikelihood for values passed
to it using the from(matrix) option.
^init^ causes the program to compute initial estimates of fixed effects
only, setting all latent variables to zero. gllamm will be used for
estimating initial values even if a Stata command is available for the
model (without the init option, gllamm uses Stata commands for initial values
whenever they are available).
^iterate(^#^)^ specifies the (maximum) number of iterations. With the ^adapt^
option, use of the ^iterate(^#^)^ option will cause ^gllamm^ to skip the
"Newton Raphson" iterations usually performed at the end without updating
the quadrature locations. ^iterate(^0^)^ is like ^eval^ except that standard
errors are computed.
^adoonly^ causes all gllamm to use only ado-code. Gllamm will be faster if
if it uses internalised versions of some of the functions available in
Stata 7 if updated on or after 26oct2001
^nip(^#^,^...^,^#^)^ when quadrature is used, this specifies the number
of quadrature points (integration points) to be used. A value may be
given for each random effect. If only one argument is given, the
same number of quadrature points will be used for each summation.
^adapt^ causes adaptive quadrature to be used instead of ordinary quadrature.
This option cannot be used with the ^ip(^f^)^ or ^ip(^f0^)^ options.
^robust^ specifies that the Huber/White/sandwich estimator of the covariance
matrix of the parameter estimates is to be used. If a model has been
estimated without the ^robust^ option, the robust standard errors can be
obtained by simply typing ^gllamm, robust^.
^cluster(^varname^)^ specifies that the highest level units of the GLLAMM
model are nested in even higher level clusters where ^varname^ contains
the cluster identifier. Robust standard errors will be provided that
take this clustering into account. If a model has been estimated without
this option, the robust standard errors for clustered data can be obtained
using the command ^gllamm, cluster(varname)^.
^level(^#^)^ specifies the confidence level in percent for confidence
intervals of the coefficients.
^eform^ causes the expnentiated coefficients and confidence intervals to be
displayed.
^allc^ causes all estimated parameters to be displayed in a regression table
(including the raw parameters for the random effects) in addition to the
usual output.
^trace^ causes more output to be displayed. Before estimation begins,
details of the specified model are displayed. In addition, a
detailed iteration log is shown including parameter estimates
and log-likelihood values for each iteration.
^nolog^ suppresses output for maximum likelihood iterations.
^nodisplay^ suppresses output of the estimates but still shows iteration log
unless ^nolog^ is used.
^dots^ causes a dot to be printed (if used together with trace) every time the
likelihood evaluation program is called by ml. This helps to assess how long
gllamm is likely to take to run and reassures the user that it is making
some progress when it is very slow.
Examples
--------
(a) 3-level random intercept model
----------------------------------
Some response "resp" and covariate "x" are available for pupils
in different schools. "id" is the identifier or label for the pupils
and "school" is the identifier for the schools. A linear model
with random intercepts at the pupil and school levels can be specified
as follows:
. ^gllamm resp x, i(id school) adapt trace^
(b) 2-level random coefficient model - growth curve model
---------------------------------------------------------
subjects identified by "id" have been measured repeatedly over
time giving responses in "resp". "cons" is a variable equal to 1
and "time" contains the time-points. A model with a random
intercept and slope for time is specified as follows:
. ^eq int: cons^
. ^eq slope: time^
. ^gllamm resp time, i(id) nrf(2) eqs(int slope) adapt trace ^
(c) two-parameter logistic item-response model
----------------------------------------------
variable "resp" contains responses to 5 items (e.g. 5 test questions)
for each subject. The subject identifier is "id". There are five
dummy variables "i1" to "i5" for the items, e.g. "i1" is equal
to 1 if the item is item 1 and 0 otherwise.
. ^eq discrim: i1 i2 i3 i4 i5^
. ^gllamm resp i1 i2 i3 i4 i5, link(logit) fam(binom) nocons /*^
^*/ i(id) eqs(discrim) adapt trace^
Author
------
Sophia Rabe-Hesketh (sophiarh@@berkeley.edu)
as part of joint work with Andrew Pickles and Anders Skrondal.
We would like to acknowledge Colin Taylor for helping in the
early stages of gllamm development. We are also very grateful
to Stata Corporation for helping us to speed up gllamm.
Web-page
--------
http://www.gllamm.org
References (available from sophiarh@@berkeley.edu)
----------
Rabe-Hesketh, S. and Skrondal, A. (2005). Multilevel and Longitudinal
Modeling using Stata. College Station, TX: Stata Press.
Rabe-Hesketh, S., Pickles, A. and Skrondal, S. (2004).
GLLAMM Manual. U.C. Berkeley Division of Biostatistics Working
Paper Series. Working Paper 160.
see http://www.bepress.com/ucbbiostat/paper160/
Rabe-Hesketh, S., Skrondal, A. and Pickles, A. (2005). Maximum
likelihood estimation of limited and discrete dependent variable
models with nested random effects. Journal of Econometrics 128, 301-323.
Rabe-Hesketh, S., Skrondal, A. and Pickles, A. (2002).
Reliable estimation of generalized linear mixed models
using adaptive quadrature. The Stata Journal 2, 1-21.
Rabe-Hesketh, S., Skrondal, A. and Pickles, A. (2004).
Generalised multilevel structural equation modelling.
Psychometrika 69 , 167-190.
Also see
--------
Manual: ^[U] 23 Estimation and post-estimation commands^
^[U] 29 Overview of model estimation in Stata^
On-line: help for @gllapred@, @gllasim@, @ml@, @glm@, @xtreg@,
@xtlogit@, @xtpois@, @quadchk@, @test@

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,252 @@
.-
help for ^gllapred^
.-
Predict command for gllamm
---------------------------
^gllapred^ varname [^if^ exp] [^in^ range] [,^u^ ^fac^ ^p^ ^xb^
^ustd^ ^co^oksd ^li^npred ^mu^ ^ma^rginal ^us(^varname^)^
^out^come^(^#^)^ ^ab^ove^(^#^,^...^,^#^)^
^pe^arson ^d^eviance ^a^nscombe
^s^ ^ll^ ^fsample^ ^nooff^set
^adapt^ ^adoonly^ ^fr^om^(^matrix^)^ ]
where only one of ^xb^ ^u^ ^fac^ ^p^ ^li^npred ^mu^
^pe^arson ^d^eviance ^a^nscombe ^s^ ^ll^
may be specified at a time.
Description
-----------
^gllapred^ is a prediction command for @gllamm@. It computes
-- Posterior means (empirical Bayes predictions) and standard
deviations of the latent variables or random effects for models
estimated using gllamm (see ^u^ and ^fac^ options).
-- Posterior probabilities for two level models with discrete
latent variables or random effects (see ^p^ option).
-- The fixed part of the linear predictor (^xb^ option) or the
entire linear predictor (^linpred^ option) with empirical Bayes
estimates substituted for the latent variables or random effects.
-- The expectation of the response (see ^mu^ option). By default,
the expectation is with respect to the posterior distribution
of the latent variables, but the ^marginal^ option gives the
expectation with respect to the prior distribution.
The ^us()^ option can be used to get the conditional expectation
for specified values of the latent variables.
--- Pearson, Deviance or Anscombe residuals. By default,
the posterior expectation, but the ^us^ option gives the
residuals for specified values of the latent variables.
-- The level 1 standard deviation (see ^s^ option).
-- Log-likelihood contributions of the highest level clusters
(see ^ll^ option).
In some cases the log-likelihood is also returned.
By default prediction is restricted to the estimation sample.
In this case (and if the ^if^ and ^in^ options are not specified),
the log-likelihood returned by gllapred should be the same
as that previously returned by gllamm.
Options
--------
^u^ the posterior means and standard deviation of the latent
variables or random effects are returned in "varname"m1,
"varname"m2, etc., and "varname"s1, "varname"s2, etc.,
respectively, where the order of the latent variables is
the same as in the call to gllamm (in the order of the
equations in the eqs() option). In the case of continuos
latent variables, the number of quadrature points used
is also the same as in the previous call to gllamm. If
the gllamm model includes equations for the latent variables
(geqs and/or bmatrix), the posterior means and standard
deviations of the disturbances are returned.
^corr^ the posterior correlations of the random effects
or latent variables are returned in "varname"c21, etc.
This option only works together with the ^u^ option. If
the model includes equations for the latent variables,
posterior correlations of the disturbances are calculated.
^fac^ If the gllamm model includes equations for the latent
variables (^geqs()^ and/or ^bmatrix()^), ^fac^ causes
predictions of the latent variables (e.g. factors) to be
returned in "varname"m1, "varname"m2, etc. instead of the
disturbances. In other words, predictions of the latent
variables on the left-hand side of the equations are returned.
^p^ can only be used for two-level models estimated using the
ip(f) option. gllapred returns the posterior probabilities
in "varname"1, "varname"2, etc., giving the probabilities
of classes 1,2, etc. gllapred also prints out the (prior)
probability and location matrices to help interpret the
posterior probabilities.
^xb^ the linear predictor for the fixed effects is returned. This
includes the offset (if there is one in the gllamm model)
unless the ^nooffset^ option is specified.
^ustd^ standardized posterior mean - approximate sampling
standard deviation is used, sqrt(prior var. - posterior var.)
^cooksd^ Cook's distances for the top-level units.
^linpred^ returns the linear predictor including both the fixed
and random parts where posterior means are substituted
for the latent variables or random effects in the random
part. The offset is included (if there is one in the
gllamm model) unless the ^nooffset^ option is specified.
^mu^ returns the expecation of the response, for example
the predicted probability in the case of dichotomous
responses. By default, the expectation is with respect
to the posterior distribution of the latent variables,
but see ^marginal^ and ^us()^ options. The offset is included
(if there is one in the gllamm model) unless the
^nooffset^ option is specified.
^marginal^ together with the ^mu^ option gives the
expectation of the response with respect to the prior
distribution of the latent variables. This is useful
for looking at the 'marginal' or population average
effects of covariates.
^us(^varname)^ can be used to specify values for the latent
variables to calculate conditional quantities, such as
the conditional mean of the responses (^mu^ option)
given the values of the latent variables. Here varname
specifies the stub-name (prefex) for the variables and
^gllapred^ will look for "varname"1 "varname"2, etc.
^outcome(^#^)^ specifies the outcome for which the predicted
probability should be returned (^mu^ option) if there
is a nominal response. This option is not necessary if the
^expanded()^ option was used in ^gllamm^ since in this
case predicted probabilities are returned for all outcomes.
^above(^#^,^...^,^#^)^ specifies the events for which the
predicted probabilities should be returned (^mu^ option)
if there are ordinal responses. The probability of
a value higher than that specified is returned for each
ordinal response. A single number can be given for all
ordinal responses.
^pearson^ returns Pearson residuals. By default, the posterior
expectation with respect to the latent variables is
returned. The ^us()^ option can be used to obtain the
conditional residual when certain values are substituted
for the latent variables.
^deviance^ returns deviance residuals. By default, the posterior
expectation with respect to the latent variables is
returned. The ^us()^ option can be used to obtain the
conditional residual when certain values are substituted
for the latent variables.
^anscombe^ returns Anscombe residuals. By default, the posterior
expectation with respect to the latent variables is
returned. The ^us()^ option can be used to obtain the
conditional residual when certain values are substituted
for the latent variables.
^s^ returns the scale or standard deviation. This is useful
if the ^s()^ option was used in gllamm to specify level 1
heteroscedasticity.
^ll^ returns the log-likelihood contributions of the highest
level (level L) units.
^adapt^ if the gllamm command did not use the adapt option,
gllapred will use ordinary quadrature for computing the
posterior means and standard deviations unless the adapt
option is used in the gllapred command.
^fsample^ causes gllapred to return predictions for the
full sample (except observations exluded due to the
if and in options), not just the estimation sample.
The returned log-likelihood may be missing since
gllapred will not exclude observations with missing
values on any of the variables used in the likelihood
calculation. It is up to the user to exclude these
observations using if or in.
^nooffset^ can be used together with the ^xb^, ^linpred^ or
^mu^ options to exclude the offset from the prediction.
It will only make a difference if the offset option
was used in gllamm.
^adoonly^ causes all gllamm to use only ado-code. This option
is not necessary if gllamm was run with the adoonly option.
^from(^matrix^)^ specifies a matrix of parameters for which
the predictions should be made. The column and equation
names will be ignored. Without this option, the parameter
estimates from the last gllamm model will be used.
Examples
--------
Estimate parameters of a three level logistic regression model:
. ^gllamm resp x, i(id school) adapt trace family(binom)^
Predict random intercepts using empirical Bayes:
. ^gllapred int, u^
Predict marginal probability that resp=1 (with respect to random effects):
. ^gllapred prob, mu marginal^
Predict conditional probability that resp==1 if random intercepts are 0:
. ^gen z1 = 0^
. ^gen z2 = 0^
. ^gllapred prob, us(z)^
Predict posterior mean of Pearson residual
. ^gllapred res, pearson^
Predict Pearson residual when random effects are equal to their posterior
means (note that ^gllapred int, u^ above produced empirical Bayes
predictions in intm1 intm2):
. ^gllapred res, pearson us(intm)^
Author
------
Sophia Rabe-Hesketh (sophiarh@@berkeley.edu)
as part of joint work with Andrew Pickles and Anders Skrondal.
Web-page
--------
http://www.gllamm.org
References
----------
Rabe-Hesketh, S., Skrondal, A. and Pickles, A. (2004). GLLAMM Manual.
U.C. Berkeley Division of Biostatistics Working Paper Series.
Working Paper 160.
Also see
--------
On-line: help for @gllamm@, @gllasim@

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,662 @@
*! version 1.0.4 SRH 12 September 2003
program define gllas_yu
version 6.0
* simulates y given u
args y lpred mu what
if "`what'"==""{
local what = 0
}
* what=0: simulate y
* what=1: mu and y
* what=5: get mu, return in y
* what=6: Pearson residual
* what=7: Deviance residual
* what=8: Anscombe residual
tempvar zu xb /* linear predictor and zu: r.eff*design matrix for r.eff */
/* ----------------------------------------------------------------------------- */
*quietly{
* matrix list M_znow
* disp "ML_y1: $ML_y1 " $ML_y1[$which]
* matrix list M_ip
* disp " xb1 = " $HG_xb1[$which]
* disp " zu = " `zu'[$which]
if $HG_mlog>0{
qui gen double `zu' = `lpred' - $HG_xb1
if `what'<=1{
simnom `y' `zu'
}
if `what'==1|`what'==5{
nominal `mu' `zu' 5
if `what'==5{
qui replace `y' = `mu'
}
}
if `what'>5{
nominal `y' `zu' `what'
}
}
if $HG_oth{
if "$HG_lv"~=""&($HG_nolog>0|$HG_mlog>0){
local myand $HG_lvolo~=1
}
*quietly gen double `mu' = 0
link "$HG_link" `mu' `lpred' $HG_s1
if $HG_comp>0{
compos `mu' "`myand'"
}
if `what'==5{
local ifs
if "`myand'"~=""{
local ifs if `myand'
}
qui replace `y' = `mu' `ifs'
}
else{
family "$HG_famil" `y' `mu' "`myand'" `what'
}
}
if $HG_nolog>0{
if `what'<=1{
simord `y' `lpred'
}
if `what'==1|`what'==5{
ordinal `mu' `lpred' 5
if `what'==5{
qui replace `y' = `mu' if $HG_lvolo==1
}
}
if `what'>5{
ordinal `y' `lpred' `what'
}
}
*} /* qui */
end
program define compos
version 6.0
args mu und
tempvar junk mu2
local ifs
if "`und'"~=""{
local ifs if `und'
}
qui gen double `junk'=0
qui gen double `mu2'=.
local i = 1
*disp in re "in compos: HG_clus is: $HG_clus"
while `i'<= $HG_comp{
*disp in re "in compos: variable HG_co`i' is: ${HG_co`i'}"
qui replace `junk' = `mu'*${HG_co`i'}
qui by $HG_clus: replace `junk' = sum(`junk')
qui by $HG_clus: replace `mu2' = `junk'[_N] if $HG_ind==`i'
local i = `i' + 1
}
qui replace `mu' = `mu2' `ifs'
end
program define nominal
version 6.0
args res zu what
tempvar mu den
if $HG_smlog{
local s $HG_s1
}
else{
local s = 1
}
local and
if "$HG_lv"~=""{
local and & $HG_lv == $HG_mlog
local mlif if $HG_lv == $HG_mlog
}
* disp "mlogit link `mlif'"
if $HG_exp==1&$HG_expf==0{
qui gen double `mu' = exp(`zu'/`s') if $ML_y1==M_respm[1,1] `and'
local n=rowsof(M_respm)
local i=2
while `i'<=`n'{
local prev = `i' - 1
* disp "xb`prev':" ${HG_xb`prev'}[$which]
qui replace `mu' = exp( (${HG_xb`prev'} + `zu')/`s') if $ML_y1==M_respm[`i',1] `and'
local i = `i' + 1
}
sort $HG_clus $HG_ind
qui by $HG_clus: gen double `den'=sum(`mu') `mlif'
qui by $HG_clus: replace `mu' = `mu'/`den'[_N] `mlif'
if `what'>5{
res_b `res' `mu' $HG_ind "`mlif'" " " `what'
* res_b res mu y if and what
}
}
else if $HG_exp==1&$HG_expf==1{
qui gen double `mu' = exp(($HG_xb1 + `zu')/`s') `mlif'
sort $HG_clus $HG_ind
qui by $HG_clus: gen double `den'=sum(`mu') `mlif'
qui by $HG_clus: replace `mu' = `mu'/`den'[_N] `mlif'
if `what'>5{
res_b `res' `mu' $HG_ind "`mlif'" " " `what'
}
}
else{
tempvar den tmp
local n=rowsof(M_respm)
local i = 2
qui gen double `mu' = 1 if $HG_outc==M_respm[1,1] `mlif'
qui gen double `den' = 1
qui gen double `tmp' = 0
while `i'<= `n'{
local prev = `i' - 1
qui replace `tmp' = exp((${HG_xb`prev'} + `zu')/`s') `mlif'
qui replace `mu' = `tmp' if $HG_outc==M_respm[`i',1] `mlif'
qui replace `den' = `den' + `tmp' `mlif'
local i = `i' + 1
}
qui replace `mu' = `mu'/`den' `mlif'
if `what'>5{
tempname y
qui gen `y' = $ML_y1==$HG_outc
res_b `res' `mu' `y' "`mlif'" " " `what'
}
}
if `what'==5{
qui replace `res' = `mu' `mlif'
}
end
program define simnom
version 6.0
args y zu
tempvar mu
tempvar r
qui gen double `r' = uniform()
if $HG_smlog{
local s $HG_s1
}
else{
local s = 1
}
local and
if "$HG_lv"~=""{
local and & $HG_lv == $HG_mlog
local mlif if $HG_lv == $HG_mlog
}
* disp "mlogit link `mlif'"
if $HG_exp==1&$HG_expf==0{
qui gen double `mu' = `zu'/`s' -ln(-ln(`r')) if $ML_y1==M_respm[1,1] `and'
local n=rowsof(M_respm)
local i=2
while `i'<=`n'{
local prev = `i' - 1
* disp "xb`prev':" ${HG_xb`prev'}[$which]
qui replace `mu' = (${HG_xb`prev'} + `zu')/`s' -ln(-ln(`r')) if $ML_y1==M_respm[`i',1] `and'
local i = `i' + 1
}
sort $HG_clus `mu'
qui by $HG_clus: replace `y'=_n==_N `mlif'
}
else if $HG_exp==1&$HG_expf==1{
qui gen double `mu' = ($HG_xb1 + `zu')/`s' -ln(-ln(`r')) `mlif'
sort $HG_clus `mu'
qui by $HG_clus: replace `y'=_n==_N `mlif'
}
else{
tempvar den tmp1 tmp2
local n=rowsof(M_respm)
local i = 2
qui gen double `mu' = 1 if $ML_y1==M_respm[1,1] `mlif'
qui gen double `den' = 1
while `i'<= `n'{
local prev = `i' - 1
qui replace `den' = `den' + exp((${HG_xb`prev'} + `zu')/`s') `mlif'
local i = `i' + 1
}
qui gen double `tmp1' = 1/`den'
qui replace `y' = M_respm[1,1] if `r'<`tmp1' `mlif'
qui gen double `tmp2' = `tmp1'
local i = 2
while `i'< `n'{
local prev = `i' - 1
qui replace `tmp2' = `tmp1' + exp((${HG_xb`prev'} + `zu')/`s')/`den' `mlif'
qui replace `y' = M_respm[`i',1] if `r'<`tmp2' & `r'>=`tmp1' `mlif'
qui replace `tmp1' = `tmp2'
local i = `i' + 1
}
qui replace `y' = M_respm[`n',1] if `r'>=`tmp2' `mlif'
}
end
program define ordinal
version 6.0
args res lpred what
local no = 1
local xbind = 2
tempvar mu mu1 l y
qui gen double `l' = 0
qui gen double `mu' = 0
qui gen double `mu1' = 0
qui gen `y' = 0
local nxt = 0
while `no' <= $HG_nolog{
local olog = M_olog[1,`no']
local lnk: word `no' of $HG_linko
if "`lnk'"=="ologit"{
local func logitl
}
else if "`lnk'"=="oprobit"{
local func probitl
}
else if "`lnk'"=="ocll"{
local func cll
}
else if "`lnk'"=="soprobit"{
local func sprobitl
}
local if
if "$HG_lv"~=""&$HG_nolog>0{
local if if $HG_lv == `olog'
}
local where = `nxt' + M_above[1,`no'] + 1
* disp in re "no = `no', where = `where' if = `if' "
qui replace `l' = -`lpred'+${HG_xb`where'}
`func' `l' `mu1'
qui replace `mu' = 1-`mu1' `if'
if `what'>5{
local ab = M_above[1,`no']
local ab = M_resp[`ab',`no']
* disp in re "replace y = y > `ab' `if' "
qui replace `y' = $ML_y1 > `ab' `if'
}
local n=M_nresp[1,`no']
local nxt = `nxt' + `n' - 1
local no = `no' + 1
} /* next ordinal response */
if `what'==5{
qui replace `res' = `mu' if $HG_lvolo==1
}
else{
res_b `res' `mu' `y' "if $HG_lvolo==1" " " `what'
}
end
program define simord
version 6.0
args y lpred
local no = 1
local xbind = 2
tempvar mu p1 p2 r
qui gen double `p1' = 0
qui gen double `p2' = 0
qui gen double `mu' = 0
qui gen double `r' = uniform()
while `no' <= $HG_nolog{
local olog = M_olog[1,`no']
local lnk: word `no' of $HG_linko
if "`lnk'"=="ologit"{
local func logitl
}
else if "`lnk'"=="oprobit"{
local func probitl
}
else if "`lnk'"=="ocll"{
local func cll
}
else if "`lnk'"=="soprobit"{
local func sprobitl
}
local and
if "$HG_lv"~=""&$HG_nolog>0{
local and & $HG_lv == `olog'
}
* disp "ordinal link is `lnk', and = `and'"
local n=M_nresp[1,`no']
* disp "HG_xb1: " $HG_xb1
* disp "xbind = " `xbind'
* disp ${HG_xb`xbind'}[$which]
qui replace `mu' = -`lpred'+${HG_xb`xbind'}
`func' `mu' `p1'
qui replace `y' = M_resp[1,`no'] if `r'<`p1' `and'
qui replace `p2' = `p1'
local i = 2
while `i' < `n'{
local nxt = `xbind' + `i' - 1
*disp "response " M_resp[`i',`no']
*disp "HG_xb`nxt' " ${HG_xb`nxt'}[$which]
qui replace `mu' = -`lpred'+${HG_xb`nxt'}
*disp "mu " `mu'[$which]
`func' `mu' `p2'
*disp "p1 and p2: " `p1'[$which] " " `p2'[$which]
qui replace `y' = M_resp[`i',`no'] if `r'<`p2' & `r'>=`p1' `and'
qui replace `p1' = `p2'
local i = `i' + 1
}
local xbind = `xbind' + `n' -1
qui replace `y' = M_resp[`n',`no'] if `r'>=`p2' `and'
local no = `no' + 1
} /* next ordinal response */
*tab $ML_y1 if `y'==. `and'
end
program define logitl
version 6.0
args mu p
qui replace `p' = 1/(1+exp(-`mu'))
end
program define cll
version 6.0
args mu p
qui replace `p' = 1-exp(-exp(`mu'))
end
program define probitl
version 6.0
args mu p
qui replace `p' = normprob(`mu')
end
program define sprobitl
version 6.0
args mu p
qui replace `p' = normprob(`mu'/$HG_s1)
end
program define link
version 6.0
* returns mu for requested link
args which mu lpred s1
* disp " in link, which is `which' "
tokenize "`which'"
local i=1
local ifs
while "`1'"~=""{
if "$HG_lv" ~= ""{
local oth = M_oth[1,`i']
local ifs if $HG_lv==`oth'
}
* disp "`1' link `ifs'"
if ("`1'" == "logit"){
quietly replace `mu' = 1/(1+exp(-`lpred')) `ifs'
}
else if ("`1'" == "probit"){
* disp "doing probit "
quietly replace `mu' = normprob((`lpred')) `ifs'
}
else if ("`1'" == "sprobit"){
quietly replace `mu' = normprob((`lpred')/`s1') `ifs'
}
else if ("`1'" == "log"){
* disp "doing log "
quietly replace `mu' = exp(`lpred') `ifs'
}
else if ("`1'" == "recip"){
* disp "doing recip "
quietly replace `mu' = 1/(`lpred') `ifs'
}
else if ("`1'" == "cll"){
* disp "doing cll "
quietly replace `mu' = 1 - exp(-exp(`lpred')) `ifs'
}
else if ("`1'" == "ident"){
quietly replace `mu' = `lpred' `ifs'
}
local i = `i' + 1
mac shift
}
end
program define family
version 6.0
args which y mu und what
tokenize "`which'"
local i=1
* disp "in family, und = `und'"
if "$HG_fv" == ""{
local ifs
if "`und'"~=""{local und if `und'}
}
else{
if "`und'"~=""{local und & `und'}
}
while "`1'"~=""{
if "$HG_fv" ~=""{
local ifs if $HG_fv == `i'
}
if ("`1'" == "binom"){
if `what'==0{
sim_b `y' `mu' "`ifs'" "`und'"
}
else{
res_b `y' `mu' $ML_y1 "`ifs'" "`und'" `what'
}
}
else if ("`1'" == "poiss"){
if `what'==0{
sim_p `y' `mu' "`ifs'" "`und'"
}
else{
res_p `y' `mu' "`ifs'" "`und'" `what'
}
}
else if ("`1'" == "gauss") {
if `what'==0{
sim_g `y' `mu' $HG_s1 "`ifs'" "`und'"
}
else{
res_g `y' `mu' $HG_s1 "`ifs'" "`und'" `what'
}
}
else if ("`1'" == "gamma"){
if `what'==0{
sim_ga `y' `mu' $HG_s1 "`ifs'" "`und'"
}
else{
res_ga `y' `mu' $HG_s1 "`ifs'" "`und'" `what'
}
}
else{
disp in re "unknown family in gllas_yu"
exit 198
}
local i = `i' + 1
mac shift
}
end
program define res_g
version 6.0
* stolen from glim_p and glim_v1
args res mu s1 if and what
if `what'==6{ /* Pearson */
qui replace `res' = ($ML_y1-`mu')/ `s1' `if' `and'
exit
}
else if `what'==7{ /* Deviance */
*qui replace `res'= sign($ML_y1-`mu')*sqrt(($ML_y1-`mu')^2/ `s1'^2) `if' `and'
qui replace `res' = ($ML_y1-`mu')/ `s1' `if' `and'
exit
}
else if `what'==8{ /* Anscombe */
qui replace `res' = ($ML_y1-`mu')/ `s1' `if' `and'
}
end
program define res_b
version 6.0
* stolen from glim_p and glim_v2
args res mu y if and what
tempvar mu_n
gen double `mu_n' = `mu'*$HG_denom
if `what'==6{ /* Pearson */
qui replace `res' = (`y'-`mu_n')/sqrt(`mu_n'*(1-`mu')) `if' `and'
exit
}
else if `what'==7{ /* Deviance */
*if $HG_denom == 1 {
* qui replace `res' = cond(`y', /*
* */ -2*ln(`mu_n'), -2*ln(1-`mu_n')) `if' `and'
*}
*else{
qui replace `res' = cond(`y'>0 & `y'<$HG_denom, /*
*/ 2*`y'*ln(`y'/`mu_n') + /*
*/ 2*($HG_denom-`y') * /*
*/ ln(($HG_denom-`y')/($HG_denom-`mu_n')), /*
*/ cond(`y'==0, 2*$HG_denom * /*
*/ ln($HG_denom/($HG_denom-`mu_n')), /*
*/ 2*`y'*ln(`y'/`mu_n')) ) `if' `and'
*}
qui replace `res'= sign(`y'-`mu_n')*sqrt(`res') `if' `and'
exit
}
else if `what'==8{ /* Anscombe */
tempname b23
scalar `b23' = exp(2*lngamma(2/3)-lngamma(4/3))
qui replace `res' = /*
*/ 1.5*(`y'^(2/3)*_hyp2f1(`y'/$HG_denom) - /*
*/ `mu_n'^(2/3)*_hyp2f1(`mu')) / /*
*/ ((`mu_n'*(1-`mu'))^(1/6)) `if' `and'
}
end
program define res_p
version 6.0
* stolen from glim_p and glim_v3
args res mu if and what
if `what'==6{ /* Pearson */
qui replace `res' = ($ML_y1-`mu')/sqrt(`mu') `if' `and'
exit
}
else if `what'==7{ /* Deviance */
qui replace `res' = cond($ML_y1==0, 2*`mu', /*
*/ 2*($ML_y1*ln($ML_y1/`mu')-($ML_y1-`mu'))) `if' `and'
qui replace `res'= sign($ML_y1-`mu')*sqrt(`res') `if' `and'
exit
}
else if `what'==8{ /* Anscombe */
qui replace `res' = 1.5*($ML_y1^(2/3)-`mu'^(2/3)) / `mu'^(1/6) `if' `and'
}
end
program define res_ga
version 6.0
* stolen from glim_p and glim_v4
args res mu s1 if and what
if `what'==6{ /* Pearson */
qui replace `res' = ($ML_y1-`mu')/`mu' `if' `and'
exit
}
else if `what'==7{ /* Deviance */
qui replace `res' = -2*(ln($ML_y1/`mu') - ($ML_y1-`mu')/`mu')
qui replace `res'= sign($ML_y1-`mu')*sqrt(`res') `if' `and'
exit
}
else if `what'==8{ /* Anscombe */
qui replace `res' = 3*($ML_y1^(1/3)-`mu'^(1/3))/`mu'^(1/3) `if' `and'
}
end
program define sim_g
version 6.0
* returns conditionally normally distributed y
args y mu s1 if and
* disp "running famg `if' `and'"
* disp "s1 = " `s1'[$which] ", mu = " `mu'[$which] " and Y = " $ML_y1[$which]
quietly replace `y' = invnorm(uniform())*`s1' + `mu' `if' `and'
end
program define sim_b
version 6.0
* returns y with binomial distribution conditional on r.effs
* $HG_denom is denominator
args y mu if and
* disp "running famb `if' `and'"
* disp "mu = " `mu'[$which] " and Y = " $ML_y1[$which]
qui replace `y' = uniform()<`mu' `if' `and'
qui summ $HG_denom `if' `and'
if r(N)>0{
local max = r(max)
if `max'>1{
tempvar left
qui gen int `left' = $HG_denom - 1
local i = 1
while `i'<`max'{
qui replace `y' = `y' + (uniform()<`mu'&`left'>0) `if' `and'
qui replace `left' = `left' - 1
local i = `i' + 1
}
}
}
end
program define sim_p
version 6.0
* simulates counts from Poisson distribution
args y mu if and
*!! disp "running famp `if'"
* disp in re "if and: `if' `and'"
tempvar t p
qui gen double `t' = 0 `if' `and'
qui gen int `p' = 0 `if' `and'
local n = 1
while `n' >0 {
qui replace `t' = `t' -ln(1-uniform())/`mu' `if' `and'
qui replace `p' = `p' + 1 if `t'<1
qui count if `t' < 1
local n = r(N)
}
quietly replace `y' = `p' `if' `and'
* disp "done famp"
end
program define sim_ga
version 6.0
* returns log of gamma density conditional on r.effs
args y mu s1 if and
*!! disp "running famg `if'"
*!! disp "mu = " `mu'[$which]
*!! disp "s1 = " `s1'[$which]
qui replace `mu' = 0.0001 if `mu' <= 0
tempvar nu
qui gen double `nu' = `s1'^(-2)
quietly replace `y' = /*
*/ `nu'*(ln(`nu')-ln(`mu')) - lngamma(`nu')/*
*/ + (`nu'-1)*ln($ML_y1) - `nu'*$ML_y1/`mu' `if' `and'
end

View File

@ -0,0 +1,825 @@
*! version 1.0.6 SRH 1 June 2003
program define gllasim
version 6.0
if "`e(cmd)'" ~= "gllamm" {
di in red "gllamm was not the last command"
exit 301
}
*syntax anything(name=pref id="prefix for variable(s)") [if] [in]
syntax newvarname [if] [in] /*
*/ [,Y U LInpred FAC FSAMPLE MU OUTcome(int -99) ABove(numlist integer) /*
*/ noOFFset ADOONLY FRom(string) US(string)]
local nopt = ("`u'"!="") + ("`linpred'"!="") + ("`fac'"!="") + ("`mu'"!="")
local pref `varlist'
if `nopt'>1 {
disp in re "only one of these option allowed: u, fac, linpred or mu"
exit 198
}
local tplv = e(tplv)
local tprf = e(tprf)
if `nopt'==0 {
local y "y"
}
if "`e(weight)'"~=""&"`y'"~=""{
disp in re "weight option used in gllamm: It doesn't normally make sense to simulate responses for collapsed data"
}
if `tplv' < 2{
if "`u'"~="" | "`fac'"~="" {
disp in re "u and fac options not valid for 1 level model"
exit 198
}
if "`linpred'"~="" & "`y'"=="" {
*disp in re "nothing to simulate. use gllapred, xb"
*exit 198
}
}
local vars
if "`y'"~=""{
local vars `pref'
local ysim `pref'
tempvar musim
local what=0
disp in gr "(simulated responses will be stored in `pref')"
/* check if gamma family used */
local found = 0
local fm "`e(famil)'"
local num: word count `fm'
local k = 1
while `k' < `num'{
local ll: word `k' of `fm'
if "`ll'" == "gamma"{
local found = 1
}
local k = `k' + 1
}
if `found' == 1{
disp in re "cannot simulate from gamma yet"
exit 198
}
}
if "`u'"~=""|"`fac'"~=""{
local i = 1
while `i' < `tprf'{
local vars `vars' `pref'p`i'
local i = `i' + 1
}
disp in gr "(simulated scores will be stored in `vars')"
}
if "`linpred'"~=""{
local vars `vars' `pref'p
local lpred `pref'p
disp in gr "(linear predictor will be stored in `pref'p)"
}
if "`mu'"~=""{
local vars `vars' `pref'p
local musim `pref'p
tempvar lpred
tempvar ysim
if "`y'"~=""{
local what = 1
}
else{
local what = 5
}
disp in gr "(mu will be stored in `pref'p)"
}
else if "`y'"~=""{
tempvar lpred
}
if "`offset'"~=""{
if "`linpred'"==""&"`mu'"==""{
disp in re "nooffset option only allowed with linpred or mu option"
exit 198
}
}
if "`us'"~=""& "`u'"~=""{
disp in re "u option does not make sense with us() option"
exit 198
}
* disp in re "setting macros"
/* check if variables already exist */
confirm new var `vars'
/* restrict to estimation sample */
tempvar idno
gen int `idno' = _n
preserve
if "`fsample'"==""{
qui keep if e(sample)
}
/* interpret if and in */
marksample touse, novarlist
qui count if `touse'
if _result(1) <= 1 {
di in red "insufficient observations"
exit 2001
}
qui keep if `touse'
tempfile file
/* set all global macros needed by gllam_ll */
setmacs 0
if "`adoonly'"!="" {
global HG_noC=1
global HG_noC1=1
}
*disp "HG_free = " $HG_free
tempname b
matrix `b'=e(b)
/* deal with from */
if "`from'"~=""{
capture qui matrix list `from'
local rc=_rc
if `rc'>1{
disp in red "`from' not a matrix"
exit 111
}
local ncol = colsof(`from')
local nrow = rowsof(`from')
local ncolb = colsof(`b')
if `ncolb'~=`ncol'{
disp in re "from matrix has `ncol' columns but should have `ncolb'"
exit 111
}
if `nrow'~=1{
disp in re "from matrix has more than one row"
exit 111
}
local coln: colnames(`b')
local cole: coleq(`b')
matrix `b' = `from'
matrix colnames `b' = `coln'
matrix coleq `b' = `cole'
}
/* deal with outcome() and above() */
if "`mu'"~=""&$HG_nolog>0{
if "`above'"==""{
disp in re "must specify above() option for ordinal responses"
exit 198
}
else{
matrix M_above = J(1,$HG_nolog,0)
local num: word count `above'
if `num'>1&`num'~=$HG_nolog{
disp in re "wrong length of numlist in above() option"
exit 198
}
local no = 1
local k = 1
while `no' <= $HG_nolog{
if `num'>1{
local k = `no'
}
local ab: word `k' of `above'
* disp in re "`no'th ordinal response, above = `ab'"
local n=M_nresp[1,`no']
local i = 1
local found = 0
while `i'<= `n'&`found'==0{
if M_resp[`i',`no']==`ab'{
local found=`i'
matrix M_above[1,`no']=`i'
}
local i = `i' + 1
}
if `found'==0{
disp in re "`ab' not a category for `no'th ordinal response"
exit 198
}
else if `found' == `n'{
disp in re "`ab' is highest category for `no'th ordinal response"
exit 198
}
local no = `no' + 1
}
}
* noi matrix list M_above
}
if "`mu'"~=""&$HG_mlog>0{
if `outcome'==-99&$HG_exp==0{
disp in re "must specify outcome() option for nominal responses unless data in expanded form"
exit 198
}
if $HG_exp==0{
local n=rowsof(M_respm)
local found = 0
local i = 1
while `i'<= `n'&`found'==0{
if M_respm[`i',1]==`outcome'{
local found=`i'
}
local i = `i' + 1
}
if `found'==0{
disp in re "`outcome not a category of the nominal response'"
exit 198
}
global HG_outc=`outcome'
}
}
/* run remcor */
/* set up names for HG_xb`i' */
local i = 1
while (`i' <= $HG_tpff){
tempname junk
global HG_xb`i' "`junk'"
local i = `i' + 1
}
/* set up names for HG_s`i' */
local i = 1
while (`i'<=$HG_tprf){
tempname junk
global HG_s`i' "`junk'"
local i = `i' + 1
}
if $HG_free{ /* names for HG_p`lev'`k' */
local lev = 2
while `lev'<=$HG_tplv{
local npar = M_np[1,`lev']
if `npar'>0{
local k = 1
while `k'<=M_nip[1, `lev']{
* disp in re "creating HG_p`lev'`k'"
tempname junk
global HG_p`lev'`k' "`junk'"
local k = `k' + 1
}
}
local lev = `lev' + 1
}
}
qui remcor "`b'" `us'
/* sort out level 1 clus variable */
local clus `e(clus)'
global HG_clus `clus'
tempvar id
if $HG_exp~=1&$HG_comp==0{
gen int `id'=_n
tokenize "`clus'"
local l= $HG_tplv
local `l' "`id'"
global HG_clus "`*'"
if $HG_tplv>1{
global HG_clus "`*'"
}
else{
global HG_clus "`1'"
}
}
* disp "HG_clus: $HG_clus"
/* deal sith us */
if "`us'"~="" {
local j = 1
while `j'<$HG_tprf{
capture confirm variable `us'`j'
if _rc~=0{
disp in re "variable `us'`j' not found"
exit 111
}
else{
global HG_U`j' "`us'`j'"
}
local j = `j' + 1
}
}
else {
if $HG_free{
/* simulate discrete latent variables HG_U`rf'*/
tempvar f r cum1 cum2
qui gen double `r' = 0
qui gen byte `f' = 0
gen double `cum2' = 0
gen double `cum1' = 0
local sortlst $HG_clus
local lev = 2
local rf = 1
local k = $HG_tplv
while `lev'<=$HG_tplv{
/* sortlist etc. */
tokenize "`sortlst'"
local `k' " "
local sortlst "`*'"
sort $HG_clus
qui by `sortlst': replace `f' = _n==1
qui replace `r' = cond(`f'==1,uniform(),.)
/* define variables HG_V */
local rf = M_nrfc[2,`lev'-1]
while `rf'<M_nrfc[2,`lev']{
tempname junk
global HG_U`rf' "`junk'"
qui gen double ${HG_U`rf'} = 0
local rf = `rf' + 1
}
/* assign values */
local i=M_nrfc[1,`lev'-1] + 1
local np = M_nip[2,`i']
local npar = M_np[1,`lev']
local n = M_nip[1,`lev']
local probs "M_zps`np'"
*disp "probabilities for level " `lev' " in `probs'"
*local n = colsof(`probs')
local i = 1
while `i'<=`n'{
qui replace `cum1' = `cum2'
if `npar'>0{
qui replace `cum2' = `cum2' + exp(${HG_p`lev'`i'})
}
else{
qui replace `cum2' = `cum2' + exp(`probs'[1,`i'])
}
local rf = M_nrfc[2,`lev'-1]
while `rf'<M_nrfc[2,`lev']{
local npt = M_nip[2,`rf'+1]
local zlocs "M_zlc`npt'"
* disp in re "replace ${HG_U`rf'} = `zlocs'[1,`i'] if r<=" `cum2' "&r> " `cum1'
qui replace ${HG_U`rf'} = `zlocs'[1,`i'] if `r'<=`cum2'&`r'>`cum1'
local rf = `rf' + 1
}
local i = `i' + 1
}
/* set all values in same cluster equal to prediction */
sort $HG_clus
local rf = M_nrfc[2,`lev'-1]
while `rf'<M_nrfc[2,`lev']{
qui by `sortlst': replace ${HG_U`rf'} = sum( ${HG_U`rf'} )
local rf = `rf' + 1
}
local lev = `lev' + 1
local k = `k' - 1
}
if "`u'"~=""|"`fac'"~=""{
local rf = 1
while `rf'<$HG_tprf{
qui gen double `pref'p`rf'=${HG_U`rf'}
local rf = `rf' + 1
}
}
}
else{
/* simulate uncorrelated iid standard normal latent variables HG_U`rf'*/
tempvar f
qui gen byte `f' = 0
local sortlst $HG_clus
local lev = 2
local rf = 1
local k = $HG_tplv
while `lev'<=$HG_tplv{
*disp "sort `sortlst'"
tokenize "`sortlst'"
local `k' " "
local sortlst "`*'"
sort $HG_clus
qui by `sortlst': replace `f' = _n==1
while `rf'<M_nrfc[2,`lev']{
tempname junk
global HG_U`rf' "`junk'"
gen double ${HG_U`rf'} = cond(`f'==1,invnorm(uniform()),0)
qui by `sortlst': replace ${HG_U`rf'} = sum( ${HG_U`rf'} )
*noi summ ${HG_U`rf'} if `f' == 1
local rf = `rf' + 1
}
local lev = `lev' + 1
local k = `k' - 1
}
/* multiply by Cholesky */
if "`u'"~=""|"`fac'"~=""{
* noi matrix list CHmat
local lv = 2
local rf = 1
while `lv'<=$HG_tplv{
local minrf = `rf'
local maxrf = M_nrfc[2,`lv']
while `rf'<`maxrf'{
qui gen double `pref'p`rf'=0
local rf2 = `minrf'
while `rf2'<=`rf'{ /* lower diagonal matrix */
* disp in re "`pref'p`rf' = `pref'p`rf' + CHmat[`rf',`rf2']*HG_U`rf2'"
qui replace `pref'p`rf'=`pref'p`rf'+CHmat[`rf',`rf2']*${HG_U`rf2'}
local rf2 = `rf2' + 1
}
local rf = `rf' + 1
}
local lv = `lv' + 1
}
}
}
}
if "`fac'"~=""{
/* regressions for factors */
if $HG_bmat{
* assumes that nocor option was used
local rf = $HG_tprf - 2
while `rf'>0{
local rf2 = `rf'+1
while `rf2'<=$HG_tprf - 1 { /* upper diagonal matrix */
*disp in re "`pref'p`rf'=`pref'p`rf'+Bmat[`rf',`rf2']*`pref'p`rf2'"
qui replace `pref'p`rf'=`pref'p`rf'+Bmat[`rf',`rf2']*`pref'p`rf2'
local rf2 = `rf2' + 1
}
local rf = `rf' -1
}
}
*disp "dealing with geqs"
tempname junk s1
local i = 1
while `i'<=$HG_ngeqs{
local k = M_ngeqs[1,`i']
local n = M_ngeqs[2,`i']
local nxt = M_ngeqs[3,`i']
*disp "random effect `k'-1 has `n' covariates"
local nxt2 = `nxt'+`n'-1
matrix `s1' = `b'[1,`nxt'..`nxt2']
*matrix list `s1'
local nxt = `nxt2' + 1
capture drop `junk'
matrix score double `junk' = `s1'
local rf = `k' - 1
replace `pref'p`rf' = `pref'p`rf' + `junk'
local i = `i' + 1
}
}
if "`y'"~=""|"`linpred'"~=""|"`mu'"~=""{
qui gen double `lpred' = $HG_xb1
local i = 2
while (`i' <= $HG_tprf){
local im = `i' - 1
qui replace `lpred' = `lpred' + ${HG_U`im'} * ${HG_s`i'}
local i = `i' + 1
}
if "`offset'"~=""&"$HG_off"~=""{
qui replace `lpred' = `lpred' - $HG_off
}
}
/* simulate y */
if "`y'"~=""|"`mu'"~=""{
/* sort out denom */
local denom "`e(denom)'"
if "`denom'"~=""{
capture confirm variable `denom'
if _rc>0{
tempvar den
qui gen `den'=1
global HG_denom "`den'"
}
else{
global HG_denom `denom'
}
}
/* sort out HG_ind */
capture confirm variable $HG_ind
if _rc>0{
tempname junk
global HG_ind "`junk'"
gen $HG_ind=1
}
/* sort out HG_lvolo */
if $HG_nolog>0{
tempname junk
global HG_lvolo "`junk'"
qui gen $HG_lvolo = 0
local no = 1
if "$HG_lv"==""{
local olog = M_olog[1,`no']
qui replace $HG_lvolo = 1
}
else{
while `no'<=$HG_nolog{
local olog = M_olog[1,`no']
qui replace $HG_lvolo = 1 if $HG_lv == `olog'
local no = `no' + 1
}
}
}
/* call gllas_yu */
qui gen double `ysim' = .
qui gen double `musim' = 0
* disp in re "musim = `musim'"
sort $HG_clus
gllas_yu `ysim' `lpred' `musim' `what'
}
/* delete macros */
delmacs
qui keep `idno' `vars'
qui sort `idno'
qui save "`file'", replace
restore
sort `idno'
qui merge `idno' using "`file'"
qui drop _merge
end
program define setmacs
version 6.0
args what
/* sort out depvar */
local depv "`e(depvar)'"
global ML_y1 "`depv'"
/* link and family-related macros */
global HG_famil "`e(famil)'"
global HG_link "`e(link)'"
global HG_linko "`e(linko)'"
global HG_nolog = `e(nolog)'
global HG_ethr = `e(ethr)'
global HG_mlog = `e(mlog)'
global HG_smlog = `e(smlog)'
global HG_oth = `e(oth)'
global HG_lv "`e(lv)'"
global HG_fv "`e(fv)'"
capture matrix M_resp=e(mresp)
capture matrix M_respm=e(mrespm)
capture matrix M_frld=e(frld)
capture matrix M_olog=e(olog)
capture matrix M_oth=e(moth)
global HG_exp = e(exp)
global HG_expf = e(expf)
global HG_ind = "`e(ind)'"
global HG_lev1 = e(lev1)
global HG_comp = e(comp)
capture local coall "`e(coall)'"
if $HG_comp~=0{
local i = 1
while `i'<=$HG_comp{
local k: word `i' of `coall'
global HG_co`i' `k'
local i = `i' + 1
}
}
/* set all other global macros */
global HG_nats = `e(nats)'
global HG_noC = `e(noC)'
global HG_noC1 = `e(noC1)'
global HG_adapt = `e(adapt)'
global HG_tplv = e(tplv)
global HG_tpff = `e(tpff)'
global HG_tpi = `e(tpi)'
global HG_tprf = e(tprf)
local tprf = $HG_tprf
global HG_free = e(free)
global HG_mult = e(mult)
global HG_lzpr lzprobg
global HG_zip zipg
if $HG_mult{
global HG_lzpr lzprobm
}
else if $HG_free{
global HG_lzpr lzprobf
global HG_zip zipf
matrix M_np=e(mnp)
}
global HG_cip = e(cip)
global which = 9
global HG_off "`e(offset)'"
global HG_error = 0
global HG_cor = `e(cor)'
global HG_bmat = e(bmat)
global HG_const = 0
global HG_ngeqs = e(ngeqs)
global HG_inter = e(inter)
global HG_dots = 0
global HG_init = e(init)
matrix M_nbrf = e(nbrf)
matrix M_nrfc = e(nrfc)
matrix M_ip = J(1,$HG_tprf+2,1)
matrix M_nffc = e(nffc)
if $HG_tprf<2{ local tprf = 2}
matrix M_znow =J(1,`tprf'-1,1)
matrix M_nip = e(nip)
capture matrix M_ngeqs = e(mngeqs)
capture matrix M_b=e(mb)
*capture matrix M_chol = e(chol)
capture matrix CHmat = e(chol)
global HG_clus `e(clus)'
local lev = 2
while `lev'<=$HG_tplv{
local l = M_nrfc[1,`lev'-1] + 1 /* loop */
local k = M_nrfc[2,`lev'-1] + 1 /* r. eff. */
while `l'<=M_nrfc[1,`lev']&$HG_tplv>1{
while `k'<=M_nrfc[2,`lev']{
*disp "loop " `l' " random effect " `k'
local w = M_nip[2,`k']
/* same loc and prob as before? */
local found = 0
local ii=M_nrfc[2,1] + 1
while `ii'<`k'{
if `w'==M_nip[2,`ii']{
local found = 1
}
local ii = `ii'+1
}
capture matrix M_zps`w' =e(zps`w')
*matrix list M_zps`w'
if `what'==2{
if $HG_free {
if `k' == M_nrfc[1,`l']{
local nip = colsof(M_zps`w')
noi disp in gr "prior probabilities"
local j = 2
local zz=string(exp(M_zps`w'[1,1]),"%6.0gc")
if `nip'>1{
local mm "0`zz'"
}
else{
local mm "1"
}
while `j'<=`nip'{
local zz=string(exp(M_zps`w'[1,`j']),"%6.0gc")
local mm "`mm'" ", " "0`zz'"
local j = `j' + 1
}
disp in gr " prob: " in ye "`mm'"
disp " "
}
}
else if `found'==0{
noi disp in gr "probabilities for `w' quad. points"
noi matrix list M_zps`w'
disp " "
}
}
* disp "M_zlc`w'"
matrix M_zlc`w'=e(zlc`w')
*matrix list M_zlc`w'
if `what'==2{
if $HG_free{
noi disp in gr "locations for random effect " `w'-1
local mm=string(M_zlc`w'[1,1],"%6.0gc")
local j = 2
while `j'<= `nip'{
local zz=string(M_zlc`w'[1,`j'],"%6.0gc")
local mm "`mm'" ", " "`zz'"
local j = `j' + 1
}
disp in gr " loc: " in ye "`mm'"
disp " "
}
else if `found'==0{
noi disp in gr "locations for `w' quadrature points"
noi matrix list M_zlc`w'
disp " "
}
}
local k = `k' + 1
}
local l = `l' + 1
}
local lev = `lev' + 1
}
end
program define delmacs
version 6.0
/* deletes all global macros and matrices*/
tempname var
if "$HG_tplv"==""{
* macros already gone
exit
}
local nrfold = M_nrfc[2,1]
local lev = 2
while (`lev'<=$HG_tplv){
local i2 = M_nrfc[2,`lev']
local i1 = `nrfold'+1
local i = `i1'
local nrfold = M_nrfc[2,`lev']
while `i' <= `i2'{
local n = M_nip[2,`i']
if `i' <= M_nrfc[1,`lev']{
capture matrix drop M_zps`n'
}
capture matrix drop M_zlc`n'
local i = `i' + 1
}
local lev = `lev' + 1
}
if $HG_free==0&$HG_init==0{
*matrix drop M_chol
matrix drop CHmat
}
matrix drop M_nrfc
matrix drop M_nffc
matrix drop M_nbrf
matrix drop M_ip
capture matrix drop M_b
capture matrix drop M_resp
capture matrix drop M_respm
capture matrix drop M_frld
matrix drop M_nip
matrix drop M_znow
capture matrix drop M_ngeqs
capture matrix drop CHmat
/* globals defined in gllam_ll */
local i=1
while (`i'<=$HG_tpff){
global HG_xb`i'
local i= `i'+1
}
local i = 1
while (`i'<=$HG_tprf){
global HG_s`i'
global HG_U`i'
local i= `i'+1
}
local i = 1
while (`i'<=$HG_tplv){
global HG_wt`i'
local i = `i' + 1
}
global HG_nats
global HG_noC
global HG_noC1
global HG_adapt
global HG_fixe
global HG_lev1
global HG_bmat
global HG_tplv
global HG_tprf
global HG_tpi
global HG_tpff
global HG_clus
global HG_weigh
global which
global HG_gauss
global HG_free
global HG_famil
global HG_link
global HG_nolog
global HG_olog
global HG_mlog
global HG_smlog
global HG_oth
global HG_exp
global HG_expf
global HG_lv
global HG_fv
global HG_nump
global HG_eqs
global HG_obs
global HG_off
global HG_denom
global HG_cor
global HG_s1
global HG_init
global HG_ind
global HG_const
global HG_dots
global HG_inter
global HG_ngeqs
global HG_ethr
global HG_mult
global HG_lzpr
global HG_zip
global HG_cip
global HG_comp
capture macro drop HG_co*
end

View File

@ -0,0 +1,151 @@
.-
help for ^gllasim^
.-
Simulate command for gllamm
---------------------------
^gllasim^ varname [^if^ exp] [^in^ range] [, ^y^ ^u^ ^fac^ ^li^npred
^mu^ ^out^come^(^#^)^ ^ab^ove^(^#^,^...^,^#^)^
^adapt^ ^fsample^ ^nooff^set ^adoonly^ ^fr^om^(^matrix^)^
^us(^varname^)^ ]
where only one of ^u^ ^fac^ ^li^npred ^mu^ may be used at a time.
^y^ can be specified in addition to one of the above.
Description
-----------
^gllasim^ is a post-estimation command for @gllamm@. It simulates
the responses from to the model just estimated.
By default responses are simulated for the estimation sample.
Use ^fsample^ to simulate responses for the full sample.
If the data were collapsed and the ^weight()^ option used in
^gllamm^, it does not make sense to simulate responses unless
the data are first expanded. This is because each record
in the collapsed dataset represents several units who happened
to have the same response in the data and it would not
make sense to simulate the same response for all these units.
Options
--------
^y^ the simulated resonses are returned in "varname". This
option is only necessary if ^u^, ^fac^, ^linpred^ or ^mu^
are also specified.
^u^ the simulated latent variables or random effects are
returned in "varname"p1, "varname"p2, etc., where the
order of the latent variables is the same as in the
call to gllamm (in the order of the equations in the eqs()
option). If the gllamm model includes equations for the
latent variables (geqs and/or bmatrix), the simulated
disturbances are returned.
^fac^ If the gllamm model includes equations for the latent
variables (^geqs()^ and/or ^bmatrix()^ options in ^gllamm^),
^fac^ causes the simulated latent variables (e.g. factors)
to be returned in "varname"p1, "varname"p2, etc. instead of
the disturbances, that is, the latent variables on the
left-hand side of the structural model.
^linpred^ returns the linear predictor including the fixed
and simulated random parts in "varname"p. The offset
is included (if there is one in the gllamm model)
unless the nooffset option is specified.
^mu^ returns the expected value of the response conditional
on the simulated values for the latent variables, e.g.
a probability if the responses are dichotmous.
^outcome(^#^)^ specifies the outcome for which the predicted
probability should be returned (^mu^ option) if there
is a nominal response and the ^expanded()^ option has not
been used in ^gllamm^ (with the ^expanded()^ option, predicted
probabilities are returned for all outcomes).
^above(^#^,^...^,^#^)^ specifies the events for which the
predicted probabilities should be returned (^mu^ option)
if there are ordinal responses. The probability of
a value higher than that specified is returned for each
ordinal response. A single number can be given for all
ordinal responses.
^nooffset^ can be used together with the ^linpred^ and ^mu^
options to exclude the offset from the simulated value.
It will only make a difference if the ^offset()^ option
was used in gllamm.
^fsample^ causes gllasim to simulate values for the
full sample (except observations exluded due to the
if and in options), not just the estimation sample.
^adoonly^ causes all gllamm to use only ado-code.
This option is not necessary if ^gllamm^ was run with the
adoonly option.
^from(^matrix^)^ specifies a matrix of parameters for which
the predictions should be made. The column and equation
names will be ignored. Without this option, the parameter
estimates from the last gllamm model will be used.
^us(^varname^)^ specifies that, instead of simulating the
latent variables, gllasim should use the variables in
"varname"1, "varname"2, etc.
Examples
--------
Estimate parameters of a three level logistic regression model:
. ^gllamm resp x, i(id school) adapt trace family(binom)^
Simulate the random intercepts
. ^gllasim int, u^
Simulate the responses
. ^gllasim y^
Simulate responses when the latent variables are equal to the values
previously simulated (note that ^gllasim int, u^ above stored the
random intercepts in intp1 and intp2):
. ^gllasim y1, us(intp)^
Simulate predicted probabilities, i.e. predicted probabilities for
simulated values of the latent variables:
. ^gllasim prob, mu^
Author
------
Sophia Rabe-Hesketh (sophiarh@@berkeley.edu)
as part of joint work with Andrew Pickles and Anders Skrondal.
Web-page
--------
http://www.gllamm.org
References
----------
Rabe-Hesketh, S., Skrondal, A. and Pickles, A. (2004). GLLAMM Manual.
U.C. Berkeley Division of Biostatistics Working Paper Series.
Working Paper 160.
Also see
--------
On-line: help for @gllamm@, @gllapred@