在stata中,要求生成一个新的变量highearns。当某个观测值的wkearns大于wkearns的平均数时,highearns取值与wkearns相同,否则取值为0
时间: 2024-05-07 15:15:25 浏览: 40
以下是stata代码:
```
egen mean_wkearns = mean(wkearns) // 计算wkearns的平均数并生成一个新变量mean_wkearns
gen highearns = cond(wkearns > mean_wkearns, wkearns, 0) // 根据条件生成highearns变量
```
解释一下代码:
首先使用`egen`命令计算变量`wkearns`的平均数,并生成一个新变量`mean_wkearns`。然后使用`gen`命令生成一个新变量`highearns`,该变量的值根据条件来确定。`cond()`函数是stata中的条件函数,其语法为`cond(condition, true_value, false_value)`。在这个例子中,`condition`为`wkearns > mean_wkearns`,即`wkearns`是否大于平均值;`true_value`为`wkearns`,即当`wkearns`大于平均值时,`highearns`的值为`wkearns`的值;`false_value`为`0`,即当`wkearns`小于或等于平均值时,`highearns`的值为`0`。
相关问题
代码生成(stata中求某一变量值为0对应的解释变量个数怎么写?
在 Stata 中,求某一变量值为0对应的解释变量个数可以使用以下代码实现:
```
regress dependent_variable independent_variables
test independent_variable = 0
```
其中,dependent_variable 是你要回归的因变量,independent_variables 是你要回归的自变量。在代码的第二行中,使用 test 命令来检验某个自变量是否等于0,如果检验结果为显著,说明该自变量对因变量的解释能力较强。在 Stata 的回归结果中,可以看到每个变量的 t 值,如果 t 值很大(绝对值大于2),则表明该变量对因变量的解释能力较强。
代码生成(stata中寻找变量值为0对应的解释变量个数怎么写?
假设你要寻找变量A值为0对应的解释变量个数,可以按照以下步骤来写Stata代码:
1. 首先,生成一个新变量B,其中B等于1当且仅当A等于0,否则B等于0。代码如下:
```
gen B = (A == 0)
```
2. 然后,使用summarize命令来计算B的平均值,即为变量A值为0对应的解释变量个数。代码如下:
```
summarize B
```
输出结果中的mean就是B的平均值,即为变量A值为0对应的解释变量个数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)