英文:
Extract coefficient and p-value for certain variable from regression loop
问题
抱歉,我无法理解你的指示。
英文:
In Stata have applied a regression loop to 1000 metabolites (outcome), and the exposure variable is BMI. I also have other variables in the model. I would like to know how I can extract only the coefficient, p-value, and 95% CI for BMI if and only aif BMI is significant. And then I want to extract them into an Excel file.
This is the code I have used. It informed me that there were, for example, 100 significant results. So I'm trying to figure out which 100 are those and extract them for BMI only, without other variables in the model.
local counter = 0
local counter_pos = 0
local counter_neg = 0
foreach outcome of varlist B - Z {
regress `outcome' bmi Age i.sex i.smoking i.lpa2c i.cholestrol
matrix M = r(table)
if M[4, 1] < 0.05 {
local ++counter
if _b[bmi] < 0 {
local ++counter_neg
}
else {
local ++counter_pos
}
}
}
display as text "Total of significant results: " as result `counter'
答案1
得分: 1
以下是一个可重现的示例,演示如何将变量名和一些结果发送到一个新文件中。在您的情况下,发布取决于一个传统上重要的结果;在这里,它是无条件的。
sysuse auto, clear
local counter = 0
local negative = 0
local positive = 0
tempname RESULTS
postfile `RESULTS' str32 varname coefficient using myresults.dta, replace
foreach v in price mpg rep78 headroom trunk length turn displacement gear_ratio {
quietly regress `v' weight
local ++counter
if _b[weight] < 0 local ++negative
else local ++positive
post `RESULTS' ("`v'") (_b[weight])
}
di "variables tried: " `counter'
di "negative relation: " `negative'
di "positive relation: " `positive'
postclose `RESULTS'
use myresults, clear
compress
list
英文:
Here is a reproducible example showing how to send a variable name and some results to a new file. In your case, posting is conditional on a conventionally significant result; here it is unconditional.
sysuse auto, clear
local counter = 0
local negative = 0
local positive = 0
tempname RESULTS
postfile `RESULTS' str32 varname coefficient using myresults.dta, replace
foreach v in price mpg rep78 headroom trunk length turn displacement gear_ratio {
quietly regress `v' weight
local ++counter
if _b[weight] < 0 local ++negative
else local ++positive
post `RESULTS' ("`v'") (_b[weight])
}
di "variables tried: " `counter'
di "negative relation: " `negative'
di "positive relation: " `positive'
postclose `RESULTS'
use myresults, clear
compress
list
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论