stata - Mata / Stata 中的 st_numscalar
问题描述
我正在尝试使用 Stata,但对它不是很熟悉。下面的forvalues
循环令人困惑。我无法弄清楚st_numscalar
Mata 中的命令在做什么。对于循环的每次迭代,我如何查看这条 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 |
* +-------------------------------+
解决方案
我想我已经弄清楚了如何查看这个循环在每次迭代中所做的事情。我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")
}
推荐阅读
- sql - 来自 SQL / Impala 中脚本的 JOIN 查询
- bash - 只有一些并行步骤的作业数组和 shell 脚本
- objective-c - XPC 客户端等待服务加载
- c++ - 为什么 Glew 在 MacOS Catalina 中找不到 OpenGL 标头?
- linux - 每 5 分钟运行一次任务,但不要在星期一 9 到星期四下午 1 点之间运行
- java - int 无法转换为 2D 数组的 int[][] 错误
- laravel - CSRF 令牌与 laravel 和 pusher 不匹配
- c - 我们如何在c中调用汇编函数?
- kotlin - kotlin 循环遍历 2 个不同长度的列表
- monaco-editor - 在光标位置访问类型定义?