Dear statalist,
I'm using a logistic regression in a loop, which I later on use in a coefplot
First I used margins with the command below and this resulted in the first figure:
webuse lbw
global outcomes_binary "low smoke"
global outcomes_continuous "bwt"
global outcomes "$outcomes_binary $outcomes_continuous"
global covariates "age lwt ptl ht ui"
foreach y of global outcomes_binary {
logit `y' $covariates
margins, dydx(*) post
mat b_`y' = r(table)
mat b_`y' = b_`y'[1..6,1...]'
mat b_`y' = b_`y'[1...,1], b_`y'[1...,5], b_`y'[1...,6], b_`y'[1...,4]
}
foreach y of global outcomes_continuous {
reg `y' $covariates
margins, dydx(*) post
mat b_`y' = r(table)
mat b_`y' = b_`y'[1..6,1...]'
mat b_`y' = b_`y'[1...,1], b_`y'[1...,5], b_`y'[1...,6], b_`y'[1...,4]
}
local i = 1
foreach var of global covariates {
mat `var' = J(1,4,0)
foreach y of global outcomes {
mat `var' = `var' \ b_`y'[`i',1...]
}
mat `var' = `var'[2...,1...]
mat rownames `var' = $outcomes_binary $outcomes_continuous $outcomes_ordinal0 $outcomes_ordinal5 $outcomes_ordinal4
mat colnames `var' = `var' ci_l ci_h
local i = `i' + 1
}
coefplot matrix(age[,1]), ci((age[,2] age[,3])) ///
|| matrix(lwt[,1]), ci((lwt[,2] lwt[,3])) ///
|| matrix(ptl[,1]), ci((ptl[,2] ptl[,3])) ///
||, scheme(s1color) xline(0) byopts(row(1)) xlab(-200(200)200 ,format(%9.0f)) mlabel mlabposition(2) format(%9.3f)
I would like to change the marginal effects into odds ratios, but when I try this command:
foreach y of global outcomes_binary {
logistic `y' $covariates, or
mat b_`y' = r(table)
mat b_`y' = b_`y'[1..6,1...]'
mat b_`y' = b_`y'[1...,1], b_`y'[1...,5], b_`y'[1...,6], b_`y'[1...,4]
}
Something goes wrong and the figure is incomplete (smoke & bwt are missing).
Can anyone help me to fix the command for storing odds ratios in loops?
Thanks in advance!
Regards, Anouk
I'm using a logistic regression in a loop, which I later on use in a coefplot
First I used margins with the command below and this resulted in the first figure:
webuse lbw
global outcomes_binary "low smoke"
global outcomes_continuous "bwt"
global outcomes "$outcomes_binary $outcomes_continuous"
global covariates "age lwt ptl ht ui"
foreach y of global outcomes_binary {
logit `y' $covariates
margins, dydx(*) post
mat b_`y' = r(table)
mat b_`y' = b_`y'[1..6,1...]'
mat b_`y' = b_`y'[1...,1], b_`y'[1...,5], b_`y'[1...,6], b_`y'[1...,4]
}
foreach y of global outcomes_continuous {
reg `y' $covariates
margins, dydx(*) post
mat b_`y' = r(table)
mat b_`y' = b_`y'[1..6,1...]'
mat b_`y' = b_`y'[1...,1], b_`y'[1...,5], b_`y'[1...,6], b_`y'[1...,4]
}
local i = 1
foreach var of global covariates {
mat `var' = J(1,4,0)
foreach y of global outcomes {
mat `var' = `var' \ b_`y'[`i',1...]
}
mat `var' = `var'[2...,1...]
mat rownames `var' = $outcomes_binary $outcomes_continuous $outcomes_ordinal0 $outcomes_ordinal5 $outcomes_ordinal4
mat colnames `var' = `var' ci_l ci_h
local i = `i' + 1
}
coefplot matrix(age[,1]), ci((age[,2] age[,3])) ///
|| matrix(lwt[,1]), ci((lwt[,2] lwt[,3])) ///
|| matrix(ptl[,1]), ci((ptl[,2] ptl[,3])) ///
||, scheme(s1color) xline(0) byopts(row(1)) xlab(-200(200)200 ,format(%9.0f)) mlabel mlabposition(2) format(%9.3f)
I would like to change the marginal effects into odds ratios, but when I try this command:
foreach y of global outcomes_binary {
logistic `y' $covariates, or
mat b_`y' = r(table)
mat b_`y' = b_`y'[1..6,1...]'
mat b_`y' = b_`y'[1...,1], b_`y'[1...,5], b_`y'[1...,6], b_`y'[1...,4]
}
Something goes wrong and the figure is incomplete (smoke & bwt are missing).
Can anyone help me to fix the command for storing odds ratios in loops?
Thanks in advance!
Regards, Anouk
Comment