I am trying to run multiple regressions with the following output, but my efforts are to no avail. Kindly assist. thanks.
Here is the output format I am targeting.
Here is my data.
Here is the code I attempted
Here is the output format I am targeting.
Code:
| Covariate | Outcome1 | Outcome2 | Outcome3 | Outcome4 | |-----------|------------------------|------------------------|------------------------|------------------------| | cov1 | 1.23 [CI: 1.10, 1.38]* | 1.35 [CI: 1.12, 1.63]* | 0.89 [CI: 0.78, 1.01] | 1.11 [CI: 0.95, 1.30] | | cov2 | 0.75 [CI: 0.60, 0.92]* | 0.88 [CI: 0.74, 1.05] | 1.45 [CI: 1.21, 1.74]* | 0.98 [CI: 0.80, 1.19] | | cov3 | 1.12 [CI: 0.95, 1.31] | 0.79 [CI: 0.67, 0.94]* | 1.02 [CI: 0.85, 1.22] | 0.91 [CI: 0.73, 1.14] | | cov4 | 0.89 [CI: 0.73, 1.08] | 1.15 [CI: 0.95, 1.38] | 0.93 [CI: 0.78, 1.12] | 1.08 [CI: 0.90, 1.30] |
Code:
* Example generated by -dataex-. For more info, type help dataex clear input byte area float agecat byte sex float(educat occupcat married) byte sexever float(sexdebut_cat mdp mh2cat mh7ab2 mh17cat mh10cat) 2 1 1 3 2 2 1 1 . 0 . . . 2 2 2 4 2 1 1 3 0 1 1 0 1 2 3 2 3 1 1 1 4 . 0 1 0 1 2 1 2 3 2 2 0 . . 1 1 0 1 2 1 2 3 1 2 1 1 . 0 1 0 1 2 1 2 2 2 1 1 1 1 0 0 0 1 2 2 1 3 1 2 0 . . 0 . . . 2 3 1 3 2 2 0 . 1 0 . . . 2 2 1 4 2 1 1 3 0 0 . . . 2 3 1 4 2 1 1 3 . 0 . . . 2 2 1 4 3 2 1 3 0 0 . . . 2 1 1 3 3 2 1 3 0 0 . . . 2 3 1 3 2 1 1 3 . 0 . . . 2 3 1 3 2 1 1 3 . 0 . . . 2 3 1 3 2 1 1 3 . 0 . . . 2 1 1 3 3 2 0 . . 0 . . . 2 2 1 3 2 2 1 3 . 0 . . . 2 1 1 3 3 2 0 . . 0 . . .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 1 end label values area area label def area 2 "2. Urban", modify label values agecat agecat label def agecat 1 "1. 18-19", modify label def agecat 2 "2. 20-35", modify label def agecat 3 "3. 36-49", modify label values sex sex label def sex 1 "1. Male", modify label def sex 2 "2. Female", modify label values educat educat label def educat 1 "None/Non-formal", modify label def educat 2 "Primary", modify label def educat 3 "Secondary", modify label def educat 4 "Tertiary", modify label values occupcat occupcat label def occupcat 1 "Unemployed", modify label def occupcat 2 "Employed", modify label def occupcat 3 "Homemaker/Student", modify label values married married label def married 1 "1. Married", modify label def married 2 "2. Unmarried", modify label values sexever yesno label values mdp yesno label def yesno 0 "0. No", modify label def yesno 1 "1. Yes", modify label values sexdebut_cat sexdebut_cat label def sexdebut_cat 1 "1. Less than 18", modify label def sexdebut_cat 2 "2. 18-19", modify label def sexdebut_cat 3 "3. 20-36", modify label def sexdebut_cat 4 "4. 36 and above", modify label values mh2cat mh2cat label def mh2cat 0 "0. Poor Knowledge", modify label def mh2cat 1 "1. Good Knowledge", modify label values mh7ab2 mh7ab2 label def mh7ab2 0 "0. Non-favourable attitude", modify label def mh7ab2 1 "1. Favourable attitude", modify label values mh17cat mh17cat label def mh17cat 0 "0. Negative practices", modify label def mh17cat 1 "1. Positve practices", modify label values mh10cat mh10cat label def mh10cat 0 "0. Low quality materials", modify label def mh10cat 1 "1. High quality materials", modify
Code:
* Define the outcomes and covariates local outcomes mh2cat mh7ab2 mh17cat mh10cat // Replace with your actual outcome variables local covariates area agecat sex educat occupcat married sexever sexdebut_cat mdp // Replace with your actual covariates set trace on * Set up the Excel file and add headers putexcel set newresults.xlsx, replace putexcel A1 = "Covariate" // Covariate names in the first column * Add outcome names as column headers local col = 2 foreach y of local outcomes { local cell = "`=char(`col' + 64)'1" // Construct valid cell name (e.g., B1, C1) putexcel `cell' = "`y'" local col = `col' + 1 } * Initialize row counter for covariates local row = 2 * Loop over covariates foreach x of local covariates { * Check if dummy variables already exist and drop them capture drop dummy_* * Generate dummy variables for categorical covariates quietly tabulate `x', generate(dummy_) foreach dummyvar of varlist dummy_* { * Write dummy variable names row-wise putexcel A`row' = "`dummyvar'" local col = 2 // Initialize column counter for outcomes foreach y of local outcomes { quietly logit `y' i.`covariates' matrix M = r(table) * Get column names of the matrix local ncols = colsof(M) // Number of columns in the matrix local colnames "" forval i = 1/`ncols' { local colnames "`colnames' " + "`=colname(M, `i')'" } * Find the index of the current dummy variable in the matrix local idx = . // Default to missing foreach colname of local colnames { if "`colname'" == "`dummyvar'" { local idx = `i' break } } * Extract coefficients, confidence intervals, and p-value if `idx' != . { local OR = el(M, 1, `idx') // Odds ratio local lowerCI = el(M, 5, `idx') // Lower confidence interval local upperCI = el(M, 6, `idx') // Upper confidence interval local pval = el(M, 4, `idx') // P-value * Check significance local significance = "" if `pval' < 0.05 { local significance = "*" } * Combine coefficients and CIs into a single string local result = "`=round(`OR', 2)' [CI: `=round(`lowerCI', 2)', `=round(`upperCI', 2)']`significance'" * Write results to Excel local cell = "`=char(`col' + 64)'`row''" // Construct valid cell name putexcel `cell' = "`result'" } else { * Handle the case where the dummy variable was not found putexcel `cell' = "Not found" } local col = `col' + 1 } local row = `row' + 1 } drop dummy_* // Clean up generated dummy variables }
Comment