Allin Cottrell0.32015-07-11Confidence band plotMODELWIN/Graphs
The function bandplot() produces a confidence band plot for
the effect of a specific regressor, in the context of a multiple
regression estimated via OLS. You need to select a particular
independent variable to be examined and a confidence level.
The parameters of bandplot() are noted below. The associated
"accessor" that can be used to get the relevant variable after
estimation is shown in parentheses where applicable. Note,
hwever, the the matrices should be passed in "pointer" form.
function bandplot (const matrix *b,
const matrix *V,
const matrix *X,
int xcol,
scalar conf,
int df,
string yname)
b coefficient matrix ($coeff)
V covariance matrix ($vcv)
X matrix of regressors ({$xlist})
xcol the 1-based index of the column of X containing the
variable that should be placed on the x-axis
conf the desired confidence level, as a decimal fraction
df the degrees of freedom of the model ($df)
yname the name of the dependent variable ($depvar)
This function displays a confidence band for the effect of
a specific regressor, in the context of a multiple regression
estimated via OLS. You need to select a particular independent
variable to be examined and a confidence level.
The fitted line shows the prediction for the dependent variable
as the value of the selected regressor is varied, while holding
all other regressors at their mean values.
The confidence band takes the form of the fitted value plus and
minus a certain number of standard errors, the number depending
on the chosen level of confidence. The standard error in
question is that for the prediction of the conditional mean of
the dependent variable: it incorporates uncertainty over the
true parameter values, and its size varies with the distance of
the value of the chosen regressor from its sample mean.
x-axis variableconfidence levelmatrix b = $coeff
matrix V = $vcv
list Xlist = $xlist
matrix X = {Xlist}
colnames(X, Xlist)
bandplot(&b, &V, &X, xvar, conf, $df, $depvar)
# the variable we want on the x-axis
matrix xref = X[,xcol]
# and its max and min
scalar xmax = maxc(xref)
scalar xmin = minc(xref)
# set the other regressors to their means (this could
# be made configurable)
matrix Xm = meanc(X)
# the number of points to evaluate
scalar n = 50
# the number of standard errors +/-
scalar nse = critical(t, df, (1 - conf) / 2)
# the matrix for plotting
matrix plotmat = zeros(n, 4)
scalar xi = xmin
scalar xstep = (xmax - xmin) / n
scalar yhat se
loop i=1..n --quiet
Xm[xcol] = xi
# find the fitted value and its standard error
yhat = Xm * b
se = sqrt(qform(Xm, V))
# fill the current row of the plot matrix
plotmat[i,1] = yhat
plotmat[i,2] = yhat - nse * se
plotmat[i,3] = yhat + nse * se
plotmat[i,4] = xi
xi += xstep
endloop
string title
title = sprintf("%s: fitted values with %g%% confidence band for mean", yname, 100 * conf)
string xname = colname(X, xcol)
plot plotmat
options with-lines fit=none
literal set linetype 3 lc rgb "#0000ff"
literal set nokey
printf "set title \"%s\"", title
printf "set xlabel \"%s\"", xname
end plot --output=display
include bandplot.gfn
open data4-10.gdt
list Xlist = 0 CATHOL PUPIL WHITE
ols ENROLL Xlist
matrix b = $coeff
matrix V = $vcv
matrix X = {Xlist}
colnames(X, Xlist)
scalar xvar = 2 # the variable (X column) to put on the x-axis
bandplot(&b, &V, &X, xvar, 0.95, $df, $depvar)