Allin Cottrell
1.4
2014-01-26
OLS showing standardized coefficients
The first parameter, y, is the dependent variable. The second parameter,
xlist, is the list of independent variables. This function performs
OLS estimation then calculates the "standardized coefficients": these
represent the estimated effect on the dependent variable, measured in
standard deviations, of a one-standard deviation change in the given
independent variable.
If a constant is included in xlist, its coefficient is simply divided
by the standard deviation of the dependent variable, so it represents
the y-intercept measured in standard devations.
The column vector of standardized coefficients is available as a return
value.
```
# OLS showing standardized coefficients
if nelem(xlist) == 0
funcerr "xlist cannot be empty"
endif
string yname = argname(y)
if strlen(yname) > 0
rename y @yname
else
yname = "y"
endif
ols @yname xlist --simple-print
matrix beta = $coeff
scalar sdy = sd(@yname)
printf "Standard deviation of dependent var. = %g\n\n", sdy
printf "Independent variables:\n\n"
printf " std. dev. standardized coeff.\n\n"
loop foreach i xlist --quiet
scalar sdx = sd(xlist.$i)
if sdx == 0
beta[i] /= sdy
else
beta[i] *= sdx / sdy
endif
printf "%16s %12.5g %16.5g\n", "$i", sdx, beta[i]
endloop
printf "\n"
return beta
```

include sols.gfn
open data4-1
list xlist = const sqft bedrms
matrix b = sols(price, xlist)
print b