How to do regression in Stata and output it to Excel

Published: 10 April 2022
on channel: Investment Analysis
499
11

Comprehensive guide for beginners on how to run a either a univariate or multivariate crossectional ordinary least squares (OLS) regression in Stata. We additionally cover topics such as importing data from Excel and outputting regression results back into Excel.

**************************************************
clear all
set more off, perm
cd "C:\Users\sindr\Documents\SimpleRegression"
import excel using "DataSet_SimpleRegression.xlsx", sheet("Data_LaTeX") firstrow

// Variables
local yvar LinesOfCode
local xvars Slides

// Run regression
reg `yvar' `xvars', r

// Store all of the regression table info in a matrix
matrix output = r(table)

// Create empty matrix
local k: word count `xvars'
matrix result = J(`k',3,.)

// Column names for the empty matrix
matrix colnames result = "Coefficient" "t-stat" "p-value"

// Column names for the empty matrix
tokenize "`xvars'"
forval i = 1/`k' {
local rown = "`rown' ``i''"
}
matrix rownames result = `rown'

// Populate the empty matrix
forval i = 1/`k' {
matrix result[`i',1] = output[1,`i']
matrix result[`i',2] = output[3,`i']
matrix result[`i',3] = output[4,`i']
}

// Export to Excel
local fileName "RegressionResults"
putexcel set "`fileName'", modify
putexcel D5 = matrix(result), names

matrix list result

**************************************************
Dataset:

Slides;LinesOfCode
85;2882
97;3611
119;3987
66;2437
83;2861
108;3844
51;1771
107;6269
120;4348
174;5296
71;2207
206;6724
189;5782
76;2311


Watch video How to do regression in Stata and output it to Excel online without registration, duration hours minute second in high quality. This video was added by user Investment Analysis 10 April 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 499 once and liked it 11 people.