马塔/斯塔塔的st_numscalar

时间:2019-06-14 00:27:18

标签: stata

我正在尝试使用Stata,但对它不是很熟悉。下面的forvalues循环令人困惑。我不知道Mata中的st_numscalar命令在做什么。如何为循环的每次迭代查看此Mata行的结果?

forvalues this_month = 1 / `max_months' {

     mata: st_numscalar("apple_quota", apple_harvest[`this_month'])

}

这是一个功能代码块,可能比理想的代码块还要长,但它确实可以运行并准确地表示我要学习的实际代码。

clear

global mt_to_kilo  = 1000
global kilo_to_lbs = 2.20462262

global apples1 = 4000
global apples2 = 2000
global apples3 = 1000
global apples4 =  700
global apples5 =  100

global apple_quota1 = $apples1 * $mt_to_kilo * $kilo_to_lbs
global apple_quota2 = $apples2 * $mt_to_kilo * $kilo_to_lbs
global apple_quota3 = $apples3 * $mt_to_kilo * $kilo_to_lbs
global apple_quota4 = $apples4 * $mt_to_kilo * $kilo_to_lbs
global apple_quota5 = $apples5 * $mt_to_kilo * $kilo_to_lbs

* to view a global variable
macro list apple_quota5
* apple_quota5:   220462.262

local max_months = 5

input month pounds total frac
1 5 100 0.05
2 10 100 0.10
3 20 100 0.20
4 30 100 0.30
5 35 100 0.35
end
list
save my_apples

* to view the data set my_apples
list `my_apples'
*     +-------------------------------+
*     | month   pounds   total   frac |
*     |-------------------------------|
*  1. |     1        5     100    .05 |
*  2. |     2       10     100     .1 |
*  3. |     3       20     100     .2 |
*  4. |     4       30     100     .3 |
*  5. |     5       35     100    .35 |
*     +-------------------------------+

clear
use "my_apples.dta", replace
putmata mf=(month frac), replace

* to view a mata data set
mata : mf
*                 1             2
*    +-----------------------------+
*  1 |            1   .0500000007  |
*  2 |            2   .1000000015  |
*  3 |            3    .200000003  |
*  4 |            4   .3000000119  |
*  5 |            5    .349999994  |
*    +-----------------------------+

mata : apple_harvest=(0 \ 0 \ $apple_quota1*mf[.,2] \ $apple_quota2*mf[.,2] \ $apple_quota3*mf[.,2]\ $apple_quota4*mf[.,2]\ $apple_quota5*mf[.,2])
mata : apple_harvest

mata : apple_harvest = apple_harvest[|19\.|]
mata : apple_harvest
*                 1
*    +---------------+
*  1 |  154323.5857  |
*  2 |  308647.1714  |
*  3 |  462970.7686  |
*  4 |  540132.5327  |
*  5 |  11023.11326  |
*  6 |  22046.22653  |
*  7 |  44092.45306  |
*  8 |  66138.68123  |
*  9 |  77161.79039  |
*    +---------------+

* What is this loop doing and how can I see the result of the mata line
* with each iteration of the loop?
forvalues this_month = 1 / `max_months' {

     mata: st_numscalar("apple_quota", apple_harvest[`this_month'])

}

* Why does the next command return the original 'my_apples' data set?
list `apple_quota5'
*     +-------------------------------+
*     | month   pounds   total   frac |
*     |-------------------------------|
*  1. |     1        5     100    .05 |
*  2. |     2       10     100     .1 |
*  3. |     3       20     100     .2 |
*  4. |     4       30     100     .3 |
*  5. |     5       35     100    .35 |
*     +-------------------------------+

1 个答案:

答案 0 :(得分:1)

我想我已经弄清楚如何查看此循环在每次迭代中的作用。我在循环中添加了display命令。该循环似乎只是一次从apple_harvest数据集中获取一个值。如果我在循环外添加display命令,它将仅显示循环中最后一次通过返回的值。

forvalues this_month = 1 / `max_months' {

     mata: st_numscalar("apple_quota", apple_harvest[`this_month'])

     display apple_quota

}

154323.59
308647.17
462970.77
540132.53
11023.113

display apple_quota

11023.113

此处显示了查看每次迭代结果的另一种方法:

forvalues this_month = 1 / `max_months' {

     mata: st_numscalar("apple_quota", apple_harvest[`this_month'])
     mata: st_numscalar("apple_quota")

}