互斥条件在 Stata 中不起作用
Mutually exclusive conditionals don't work in Stata
我在 Stata 上有一些数据,其中包含一些变量,例如 logTA
和 class
。我有超过一千个观察值,logTA
没有任何缺失值。数据如下所示:
logTA class
-------- --------
.
.
21.26871
.
现在,我要做的是根据以下简单规则为 class
变量赋值:
replace class = 1 if logTA < mean(logTA) - sd(logTA)
replace class = 2 if logTA >= mean(logTA) - sd(logTA) & logTA < mean(logTA) + sd(logTA)
replace class = 3 if logTA >= mean(logTA) + sd(logTA)
看起来很简单,但是Stata没有可以这样使用的mean()和sd()函数。无论如何,我将 mean(logTA) 替换为 19.76543 并将 sd(logTA) 替换为 1.507828.
然而,在数千个观测值中,logTA
= 21.26871 的观测值未分配任何 class。当我的条件互斥时,这怎么可能?
这里我上传了我的文件:http://wikisend.com/download/187254/problem.dta
当我试图简化要发送到这里的数据集时,我意识到更改变量名可以解决问题。因此,为了重现错误,只需 运行 以下代码并检查最后 9 行。
gen sBuyuklukSinifi = .
replace sBuyuklukSinifi = 1 if logmToplamVarliklar < 19.76543 - 1.507828
replace sBuyuklukSinifi = 2 if logmToplamVarliklar >= 19.76543 - 1.507828 & logmToplamVarliklar < 19.74152 + 1.507828
replace sBuyuklukSinifi = 3 if logmToplamVarliklar >= 19.76543 + 1.507828
我可以通过以下方式实现:
/* Fake Data */
clear
set seed 5615
set obs 100000
generate logTA = rnormal(19.76543,1.507828)
replace logTA=21.26871 in 1
summarize logTA, detail
generate class = .
replace class = 1 if logTA < r(mean) - r(sd)
replace class = 2 if inrange(logTA,r(mean) - r(sd),r(mean) + r(sd))
replace class = 3 if logTA >= r(mean) + r(sd)
计算结果由 summarize
存储在 r()
中,以便可以轻松访问它们并将其代入后续命令,而无需键入值。
我不确定为什么您的代码不起作用。我得到与上面相同的答案:
generate class2 = .
replace class2 = 1 if logTA < 19.76503 - 1.513932
replace class2 = 2 if inrange(logTA,19.76503 - 1.513932,19.76503 + 1.513932)
replace class2 = 3 if logTA >= 19.76503 + 1.513932
tab class class2
与 Dimitriy 一样,我无法重现您的问题,因此不得不将您标题中的断言视为未经证实的。
这是我的检查,还确认了优先规则(先评估哪个操作)没有影响。
. local a 21.26871
. di `a' >= 19.76503 - 1.507828 & `a' < 19.76503 + 1.507828
1
. di (`a' >= 19.76503 - 1.507828) & (`a' < 19.76503 + 1.507828)
1
我注意到所讨论的值并非悬而未决:
. di 19.76503 + 1.507828
21.272858
不是问题,但 egen
具有 std()
功能。然后您可以根据需要对结果进行舍入,尽管统计分类只是丢弃信息。
我在 Stata 上有一些数据,其中包含一些变量,例如 logTA
和 class
。我有超过一千个观察值,logTA
没有任何缺失值。数据如下所示:
logTA class
-------- --------
.
.
21.26871
.
现在,我要做的是根据以下简单规则为 class
变量赋值:
replace class = 1 if logTA < mean(logTA) - sd(logTA)
replace class = 2 if logTA >= mean(logTA) - sd(logTA) & logTA < mean(logTA) + sd(logTA)
replace class = 3 if logTA >= mean(logTA) + sd(logTA)
看起来很简单,但是Stata没有可以这样使用的mean()和sd()函数。无论如何,我将 mean(logTA) 替换为 19.76543 并将 sd(logTA) 替换为 1.507828.
然而,在数千个观测值中,logTA
= 21.26871 的观测值未分配任何 class。当我的条件互斥时,这怎么可能?
这里我上传了我的文件:http://wikisend.com/download/187254/problem.dta 当我试图简化要发送到这里的数据集时,我意识到更改变量名可以解决问题。因此,为了重现错误,只需 运行 以下代码并检查最后 9 行。
gen sBuyuklukSinifi = .
replace sBuyuklukSinifi = 1 if logmToplamVarliklar < 19.76543 - 1.507828
replace sBuyuklukSinifi = 2 if logmToplamVarliklar >= 19.76543 - 1.507828 & logmToplamVarliklar < 19.74152 + 1.507828
replace sBuyuklukSinifi = 3 if logmToplamVarliklar >= 19.76543 + 1.507828
我可以通过以下方式实现:
/* Fake Data */
clear
set seed 5615
set obs 100000
generate logTA = rnormal(19.76543,1.507828)
replace logTA=21.26871 in 1
summarize logTA, detail
generate class = .
replace class = 1 if logTA < r(mean) - r(sd)
replace class = 2 if inrange(logTA,r(mean) - r(sd),r(mean) + r(sd))
replace class = 3 if logTA >= r(mean) + r(sd)
计算结果由 summarize
存储在 r()
中,以便可以轻松访问它们并将其代入后续命令,而无需键入值。
我不确定为什么您的代码不起作用。我得到与上面相同的答案:
generate class2 = .
replace class2 = 1 if logTA < 19.76503 - 1.513932
replace class2 = 2 if inrange(logTA,19.76503 - 1.513932,19.76503 + 1.513932)
replace class2 = 3 if logTA >= 19.76503 + 1.513932
tab class class2
与 Dimitriy 一样,我无法重现您的问题,因此不得不将您标题中的断言视为未经证实的。
这是我的检查,还确认了优先规则(先评估哪个操作)没有影响。
. local a 21.26871
. di `a' >= 19.76503 - 1.507828 & `a' < 19.76503 + 1.507828
1
. di (`a' >= 19.76503 - 1.507828) & (`a' < 19.76503 + 1.507828)
1
我注意到所讨论的值并非悬而未决:
. di 19.76503 + 1.507828
21.272858
不是问题,但 egen
具有 std()
功能。然后您可以根据需要对结果进行舍入,尽管统计分类只是丢弃信息。