|
In this section we consider the linear model
z=read("westwood.dat") ; reads the data z ; shows the data x=z[,2] ; puts the x-data into x y=z[,3] ; puts the y-data into ygives as output
Contents of z [ 1,] 1 30 73 [ 2,] 2 20 50 [ 3,] 3 60 128 [ 4,] 4 80 170 [ 5,] 5 40 87 [ 6,] 6 50 108 [ 7,] 7 60 135 [ 8,] 8 30 69 [ 9,] 9 70 148 [10,] 10 60 132We use the quantlet
{beta,bse,bstan,bpval}=linreg(x,y) ; computes the linear regression and returns the variables of beta, bse, bstan and bpval beta ; shows the value of betagives the output
A N O V A SS df MSS F-test P-value ______________________________________________________________ Regression 13600.000 1 13600.000 1813.333 0.0000 Residuals 60.000 8 7.500 Total Variation 13660.000 9 1517.778 Multiple R = 0.99780 R^2 = 0.99561 Adjusted R^2 = 0.99506 Standard Error = 2.73861 PARAMETERS Beta SE StandB t-test P-value ______________________________________________________________ b[ 0,]= 10.0000 2.5029 0.0000 3.995 0.0040 b[ 1,]= 2.0000 0.0470 0.9978 42.583 0.0000and
Contents of beta [1,] 10 [2,] 2As a result, we have the ANOVA (ANalysis Of VAriance) table and the parameters. The estimates
Let us now describe how to visualize these results.
In the left window we show
the regression result computed by
linreg
.
In the right window we use the
quantlet
grlinreg
to get the graphical
object directly from the data set.
yq=(beta[1]+beta[2]*x[1:10]) ; creates a vector with the ; estimated values of y data=sort(x~y) ; creates object with the data set setmaskp(data,1,11,4) ; creates a graphical object for ; the data points rdata=sort(x~yq) ; creates an object with yq rdata=setmask(rdata,"reset","line","red","thin") ; sets the options for the ; regression function by linreg regrdata=grlinreg(data,4) ; creates the same graphical ; object directly from the data regrdata=setmask(regrdata,"reset","line","red","thin") ; sets options for the regression ; function by grlinreg linregplot=createdisplay(1,2); creates display with 2 windows show(linregplot,1,1,data,rdata) ; shows rdata in the 1st window show(linregplot,1,2,data,regrdata) ; shows regrdata in the 2nd window setgopt(linregplot,1,1,"title","linreg") ; sets the title of the 1st window setgopt(linregplot,1,2,"title","grlinreg") ; sets the title of the 2nd window
![]() |
This will produce the results visible in Figure 4.1.
We create a plot of the regression function by
grlinreg
if we are only interested in a graphical exploration
of the regression line.
A second tool for our simple linear regression problem is the generalized
least squares (GLS) method given by the quantlet
gls
.
Here we only consider a model
We take the Westwood data again and assume that it has already been stored in
x and y using the unit matrix as weight matrix. This example is
stored in
XLGregr2.xpl
.
b=gls(x,y) ; computes the GLS fit and stores the ; coefficients in the variable b b ; shows bshows
Contents of b [1,] 2.1761As a result, we get the parameter
![]() |
Note that we have got different results depending on the choice of the
method. This is not surprising, as
gls
ignores the absolute value
.
Now we also want to visualize this result.
yq=b*x[1:10] ; creates a vector with the ; estimated values data=sort(x~y) ; creates object with the data set setmaskp(data,1,11,8) ; creates graphical object ; for the data rdata=sort(x~yq) ; creates object with yq rdata=setmask(rdata,"reset","line","red","medium") ; creates graphical object for yq glsplot=createdisplay(1,1) ; creates display show(glsplot,1,1,data,rdata) ; shows the graphical objects setgopt(glsplot,1,1,"title","gls") ; sets the window title