提取回归循环中特定变量的系数和p值。

huangapple go评论48阅读模式
英文:

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&#39; str32 varname coefficient using myresults.dta, replace  

foreach v in price mpg rep78 headroom trunk length turn displacement gear_ratio { 
    quietly regress `v&#39; weight 
	local ++counter
	if _b[weight] &lt; 0 local ++negative
	else local ++positive 
	post `RESULTS&#39; (&quot;`v&#39;&quot;) (_b[weight]) 
} 

di &quot;variables tried:   &quot; `counter&#39; 
di &quot;negative relation: &quot; `negative&#39; 
di &quot;positive relation: &quot; `positive&#39;  

postclose `RESULTS&#39; 

use myresults, clear 
compress 
list 

huangapple
  • 本文由 发表于 2023年2月16日 17:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定