我现在要进行如下计算: 分年龄分年计算几个指标的log variance
但现在的问题是,没有一个Variance 函数。只有采用sum命令,然后再利用矩阵转换,才能声称上述指标。
应该不难的,但我对矩阵还一窍不通啊,哪位高手帮帮忙!(应该采用return list 或return list,我猜)
by age yr: sum hinc hinc1 hcon2 hcon1 hcon4
foreach var of varlist hinc hinc1 hcon2 hcon1 hcon4{
qui sum `var'
dis in y "log-var(`var') = " in g ln(r(sd)^2)
}
真的非常感谢楼上的。
但是否有可能说说,这是什么意思?或者到什么地方能查到
这些都的慢慢积累,看的书多了就知道了。
学什么软件,不是拿过就能想干什么就干什么的,必须的先把一些基础的看看。
比如:stata的manual。
谢谢斑竹的提醒,你说的对。
其实我本来stata9的manual都复印了的,但现在因为不在自己学校,查资料很不方便,所以就想偷懒了。
没想到论坛上还有电子版的manual,谢谢啦!
我还是没有办法求出来啊!
我要求按年龄按年代汇总求方差啊!
foreach可以解决我同时算好几个指标的问题,但解决不了按年龄按年代求解的问题。我只会BY啊,但by不能与foreach 放在一起用?
谁还知道?谢谢了阿!
foreach var of varlist hinc hinc1 hcon2 hcon1 hcon4{
by age yr: egen `var'sd=sd(`var')
gen ln`var'sd=ln(`var'sd)
}
[此贴子已经被作者于2007-10-17 22:58:21编辑过]
foreach var of varlist hinc hinc1 hcon2 hcon1 hcon4{
by age yr: egen `var'sd=sd(`var) 没有sd函数,还是要用sum感觉,估计是这里错了。
gen ln`var'sd=ln(`var'sd)
}
help egen dialog: egen
----------------------------------------------------------------------------------------------------------
Title
[D] egen -- Extensions to generate
Syntax
egen [type] newvar = fcn(arguments) [if] [in] [, options]
by may be used with some of the egen functions as noted below.
where depending on the fcn, arguments refers to an expression, varlist, or numlist, and the options
are also fcn dependent, and where fcn is
anycount(varlist), values(integer numlist)
may not be combined with by. It returns the number of variables in varlist for which values
are equal to any of the integer values in a supplied numlist. Values for any observations
excluded by either [if] or [in] are set to 0 (not missing). Also see anyvalue(varname) and
anymatch(varlist).
anymatch(varlist), values(integer numlist)
may not be combined with by. It is 1 if any of the variables in varlist are equal to any of
the integer values in a supplied numlist and 0 otherwise. Values for any observations
excluded by either [if] or [in] are set to 0 (not missing). Also see anyvalue(varname) and
anycount(varlist).
anyvalue(varname), values(integer numlist)
may not be combined with by. It takes the value of varname if varname is equal to any of
the integer values in a supplied numlist and is missing otherwise. Also see
anymatch(varlist) and anycount(varlist).
concat(varlist) [, format(%fmt) decode maxlength(#) punct(pchars)]
may not be combined with by. It concatenates varlist to produce a string variable. Values
of string variables are unchanged. Values of numeric variables are converted to string, as
is, or converted using a numeric format under option format(%fmt) or decoded under option
decode, in which case maxlength() may also be used to control the maximum label length used.
By default, variables are added end to end: punct(pchars) may be used to specify
punctuation, such as a space, punct(" "), or a comma, punct(,).
count(exp) (allows by varlist:)
creates a constant (within varlist) containing the number of nonmissing observations of exp.
Also see rownonmiss() and rowmiss() below.
cut(varname), {at(#,#,...,#)|group(#)} [icodes label]
may not be combined with by. It creates a new categorical variable coded with the left-hand
ends of the grouping intervals specified in the at() option, which expects an ascending
numlist.
at(#,#,...,#) supplies the breaks for the groups, in ascending order. The list of break
points may be simply a list of numbers separated by commas but can also include the syntax
a(b)c, meaning from a to c in steps of size b. If no breaks are specified, the command
expects the option group().
group(#) specifies the number of equal frequency grouping intervals to be used in the
absence of breaks. Specifying this option automatically invokes icodes.
icodes requests that the codes 0, 1, 2, etc., be used in place of the left-hand ends of the
intervals.
label requests that the integer-coded values of the grouped variable be labeled with the
left-hand ends of the grouping intervals. Specifying this option automatically invokes
icodes.
diff(varlist)
may not be combined with by. It creates an indicator variable equal to 1 if the variables
in varlist are not equal and 0 otherwise.
ends(strvar) [, punct(pchars) trim [head|tail|last]]
may not be combined with by. It gives the first "word" or head (with the head option), the
last "word" (with the last option), or the remainder or tail (with the tail option) from
string variable strvar.
head, last, and tail are determined by the occurrence of pchars, which is by default a
single space (" ").
The head is whatever precedes the first occurrence of pchars, or the whole of the string if
it does not occur. For example, the head of "frog toad" is "frog", and that of "frog" is
"frog". With punct(,), the head of "frog,toad" is "frog".
The last word is whatever follows the last occurrence of pchars or is the whole of the
string if a space does not occur. The last word of "frog toad newt" is "newt", and that of
"frog" is "frog". With punct(,), the last word of "frog,toad" is "toad".
The remainder or tail is whatever follows the first occurrence of pchars, which will be the
empty string "" if pchars does not occur. The tail of "frog toad newt" is "toad newt", and
that of "frog" is "". With punct(,), the tail of "frog,toad" is "toad".
The trim option trims any leading or trailing spaces.
fill(numlist)
may not be combined with by. It creates a variable of ascending or descending numbers or
complex repeating patterns. numlist must contain at least two numbers and may be specified
using standard numlist notation; see numlist. [if] and [in] are not allowed with fill().
group(varlist) [, missing label lname(name) truncate(num)]
may not be combined with by. It creates a single variable taking on values 1, 2, ... for
the groups formed by varlist. varlist may contain numeric variables, string variables, or a
combination of the two. The order of the groups is that of the sort order of varlist.
missing indicates that missing values in varlist (either . or "") are to be treated like any
other value when assigning groups, instead of as missing values being assigned to the group
missing. The label option returns integers from 1 up according to the distinct groups of
varlist in sorted order. The integers are labeled with the values of varlist or the value
labels, if they exist. lname() specifies the name to be given to the value label created to
hold the labels; lname() implies label. The truncate() option truncates the values
contributed to the label from each variable in varlist to the length specified by the
integer argument num. The truncate option cannot be used without specifying the label
option. The truncate option does not change the groups that are formed; it changes only
their labels.
iqr(exp) (allows by varlist:)
creates a constant (within varlist) containing the interquartile range of exp. Also see
pctile().
kurt(varname) (allows by varlist:)
returns the kurtosis (within varlist) varname.
mad(exp) (allows by varlist:)
returns the median absolute deviation from the median (within varlist) of exp.
max(exp) (allows by varlist:)
creates a constant (within varlist) containing the maximum value of exp.
mdev(exp) (allows by varlist:)
returns the mean absolute deviation from the mean (within varlist) of exp.
mean(exp) (allows by varlist:)
creates a constant (within varlist) containing the mean of exp.
median(exp) (allows by varlist:)
creates a constant (within varlist) containing the median of exp. Also see pctile().
min(exp) (allows by varlist:)
creates a constant (within varlist) containing the minimum value of exp.
mode(varname) [, minmode maxmode nummode(integer) missing] (allows by varlist:)
produces the mode (within varlist) for varname, which may be numeric or string. The mode is
the value occurring most frequently. If two or more modes exist, the mode produced will be
a missing value. To avoid this, the minmode, maxmode, or nummode() options may be used to
specify choices for selecting among the multiple modes. minmode returns the lowest value,
and maxmode returns the highest value. nummode(#) will return the #th mode, counting from
the lowest up. Missing values are excluded from determination of the mode unless missing is
specified. Even so, the value of the mode is recorded for observations for which the values
of varname are missing unless they are explicitly excluded, that is, by
if varname < . or if varname != "".
mtr(year income)
may not be combined with by. It returns the U.S. marginal income tax rate for a married
couple with taxable income income in year year, where 1930 < year < 2004. year and income
may be specified as variable names or constants; e.g., mtr(1993 faminc), mtr(surveyyr
28000), or mtr(surveyyr faminc). A blank or comma may be used to separate income from year.
pc(exp) [, prop] (allows by varlist:)
returns exp (within varlist) scaled to be a percentage of the total, between 0 and 100. The
prop option returns exp scaled to be a proportion of the total, between 0 and 1.
pctile(exp) [, p(#)] (allows by varlist:)
creates a constant (within varlist) containing the #th percentile of exp. If p(#) is not
specified, 50 is assumed, meaning medians. Also see median().
rank(exp) [, [field|track|unique]] (allows by varlist:)
creates ranks (within varlist) of exp; by default, equal observations are assigned the
average rank. The field option calculates the field rank of exp: the highest value is
ranked 1, and there is no correction for ties. That is, the field rank is 1 + the number of
values that are higher. The track option calculates the track rank of exp: the lowest
value is ranked 1, and there is no correction for ties. That is, the track rank is 1 + the
number of values that are lower. The unique option calculates the unique rank of exp:
values are ranked 1,...,#, and values and ties are broken arbitrarily. Two values that are
tied for second are ranked 2 and 3.
rowfirst(varlist)
may not be combined with by. It gives the first nonmissing value in varlist for each
observation (row). If all values in varlist are missing for an observation, newvar is set
to missing.
rowlast(varlist)
may not be combined with by. It gives the last nonmissing value in varlist for each
observation (row). If all values in varlist are missing for an observation, newvar is set
to missing.
rowmax(varlist)
may not be combined with by. It gives the maximum value (ignoring missing values) in
varlist for each observation (row). If all values in varlist are missing for an
observation, newvar is set to missing.
rowmean(varlist)
may not be combined with by. It creates the (row) means of the variables in varlist,
ignoring missing values; for example, if three variables are specified and, in some
observations, one of the variables is missing, in those observations newvar will contain the
mean of the two variables that do exist. Other observations will contain the mean of all
three variables. Where none of the variables exist, newvar is set to missing.
rowmin(varlist)
may not be combined with by. It gives the minimum value in varlist for each observation
(row). If all values in varlist are missing for an observation, newvar is set to missing.
rowmiss(varlist)
may not be combined with by. It gives the number of missing variables in varlist for each
observation (row).
rownonmiss(varlist) [, strok]
may not be combined with by. It gives the number of nonmissing values in varlist for each
observation (row)--this is the value used by rowmean() for the denominator in the mean
calculation.
String variables may not be specified unless option strok is also specified. If strok is
specified, string variables will be counted as containing missing values when they contain
"". Numeric variables will be counted as containing missing when their value is ">.".
rowsd(varlist)
may not be combined with by. It creates the (row) standard deviations of the variables in
varlist, ignoring missing values.
rowtotal(varlist)
may not be combined with by. It creates the (row) sum of the variables in varlist, treating
missing as 0.
sd(exp) (allows by varlist:)
creates a constant (within varlist) containing the standard deviation of exp. Also see
mean().
seq() [, from(#) to(#) block(#)] (allows by varlist:)
returns integer sequences. Values start from from() (default 1) and increase to to() (the
default is the maximum number of values) in blocks (default size 1). If to() is less than
the maximum number, sequences restart at from(). Numbering may also be separate within
groups defined by varlist or decreasing if to() is less than from(). Sequences depend on
the sort order of observations, following three rules: (1) observations excluded by if or in
are not counted, (2) observations are sorted by varlist, if specified; and (3) otherwise,
the order is that when called. Note that no arguments are specified.
skew(varname) (allows by varlist:)
returns the skewness (within varlist) of varname. When skew() is specified, newvar will
always be produced as type double. The type specified by the user will be ignored.
std(exp) [, mean(#) std(#)]
may not be combined with by. It creates the standardized values of exp. The options
specify the desired mean and standard deviation. The default is mean(0) and std(1),
producing a variable with mean 0, standard deviation 1.
tag(varlist) [, missing]
may not be combined with by. It tags just one observation in each distinct group defined by
varlist. When all observations in a group have the same value for a summary variable
calculated for the group, it will be sufficient to use just one value for many purposes.
The result will be 1 if the observation is tagged and never missing, and 0 otherwise.
Values for any observations excluded by either [if] or [in] are set to 0 (not missing).
Hence, if tag is the variable produced by egen tag = tag(varlist), the idiom if tag is
always safe. missing specifies that missing values of varlist may be included.
total(exp) (allows by varlist:)
creates a constant (within varlist) containing the sum of exp. Also see mean().
扫码加好友,拉您进群



收藏
