简体   繁体   English

Stata:使用局部变量在Stata中构建程序

[英]Stata: using local variables to build a program in Stata

I would like to perform a regression based on the interests of the user of the program. 我想根据程序用户的兴趣进行回归。

For example I would like to run the following regression with the ability to change the inputs 例如,我想运行以下回归,并能够更改输入

reg prefers inSchool#female#treatment reg喜欢在学校#女性#治疗

I have multiple preference options for example math, science, arts, etc... 我有多种偏好选项,例如数学,科学,艺术等......

what i would like is something like 我想要的是什么样的

local preference = math (where I can choose the others as well)

local compareSchooling = Y
local compareGender = Y

reg preference inSchool#female#treatment

if compareSchooling=Y & compareGender=Y {
test 1.inSchool#1.female#1.treatment = 0.inSchool#0.female#1.treatment
}

The motivation behind this is that someone else can easily use the program to customize his/her analysis. 这背后的动机是其他人可以轻松地使用该程序来定制他/她的分析。

However, I have had a difficult time making the mechanics work. 但是,我很难让机械师工作。

If you have a better idea about implementation, then I'm all ears. 如果你对实现有更好的了解,那么我全都听见了。

Thanks! 谢谢!

It is hard to identify a general problem within your example that requires a program. 在您的示例中很难确定需要程序的一般问题。

Also, when you ask about a Stata program, a Stata programmer tends to imagine that you expect to write program and that way to define a new command, but it's not at all obvious that you really need to write a new command here. 另外,当你询问Stata程序时,Stata程序员倾向于想象你期望编写program和那种方式来定义一个新命令,但是你真的需要在这里写一个新命令并不是很明显。

The example I take to be that regress and test could be run repeatedly for different variables. 我认为regresstest的例子可以针对不同的变量重复运行。

But some details are obscure. 但有些细节不明确。 What is Y ? 什么是Y Or is it just a way of indicating "Yes"? 或者它只是一种表示“是”的方式? If you were to run this interactively, the use of local macros would be futile, as you would just be putting a name into a local macro and taking it out again. 如果你以交互方式运行它,那么使用本地宏将是徒劳的,因为你只需要将一个名称放入本地宏并再次取出它。

If you do write a program, then you need to document it with a help file to explain what it does, or minimally otherwise. 如果您确实编写了一个程序,那么您需要使用帮助文件对其进行记录,以解释它的功能,否则最低限度。 For what purpose? 出于什么目的? So, that a user (you or someone else) can repeat two commands with different details? 那么,用户(您或其他人)可以重复两个具有不同细节的命令? The gain there is not clear. 那里的收益不明确。

As far I understand your problem, the main need is not a program at all, but 到目前为止,我理解你的问题,主要需要的不是程序,而是

  1. To put repeated commands in a do-file and allow that do-file to take arguments. 将重复的命令放在do-file中并允许该do-file接受参数。 That's a much underestimated possibility. 这是一个被低估的可能性。 See the [U] manual, 16.4 参见[U]手册,16.4

  2. To learn about looping over variables with a foreach loop. 了解使用foreach循环循环变量。

Note: "local variables" is not standard Stata terminology; 注意:“局部变量”不是标准的Stata术语; they would always be called "local macros". 它们总是被称为“本地宏”。 In Stata a variable is a column in the dataset. 在Stata中,变量是数据集中的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM