Docsity

Mathematica Notebook: Statistical Analysis Functions and Regression - Prof. Raoul Lepage, Lab Reports of Probability and Statistics

Toggle sidebar
bg1
You will have to use a computer in a university lab (e.g. Wells Hall B-Wing).
This Mathematica notebook contains a number of useful functions briefly described below. The first time you attempt to use one
of these functions a panel will pop up asking "Do you want to evaluate all the initialization cells?" to which you must answer yes.
To enter a given command line you click on the screen whereupon a horizontal line should appear at the cursor. When right
brackets are in view on the Mathematica panel you want to click at a place where a horizontal line will extend between two such
brackets if you desire a new line. If you attempt to type multiple commands into a single bracketed location Mathematica will
become confused.
Type the command you wish to execute then PRESS THE ENTER KEY ON THE NUMERIC KEYPAD. This is required
because Mathematica wants to use the return or other enter key to move to the next line. You do nor want to move to a new line.
You want to enter a command. That is why you must use the ENTER key on the numeric keypad. You may also use SHIFT and
ENTER together instead of using ENTER on the numeric keypad.
To save your work select save from the pull down file menu, which saves it as a Mathematica .nb (notebook) file. If you wish to
print your work at home, select print, then select the option of saving as a PDF. You will be unable to work with the .nb Mathe-
matica file itself unless you have Mathematica installed (unlikely) but you can transport and print the .pdf file virtually anywhere.
Click the line below and press ENTER on the numeric keypad.
size@84.5, 7.1, 7.8, 9.1<D
4
Just above, I clicked to open a new line then typed
size[{4.5, 7.1, 7.8, 9.1}]
followed by a press of the numeric keypad ENTER key. Notice that off to the right of the entry there are nested brackets joining
the command line and its output 4 = the number of data items in {4.5, 7.1, 7.8, 9.1}.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Mathematica Notebook: Statistical Analysis Functions and Regression - Prof. Raoul Lepage and more Lab Reports Probability and Statistics in PDF only on Docsity!

You will have to use a computer in a university lab (e.g. Wells Hall B-Wing). This Mathematica notebook contains a number of useful functions briefly described below. The first time you attempt to use one of these functions a panel will pop up asking "Do you want to evaluate all the initialization cells?" to which you must answer yes. To enter a given command line you click on the screen whereupon a horizontal line should appear at the cursor. When right brackets are in view on the Mathematica panel you want to click at a place where a horizontal line will extend between two such brackets if you desire a new line. If you attempt to type multiple commands into a single bracketed location Mathematica will become confused. Type the command you wish to execute then PRESS THE ENTER KEY ON THE NUMERIC KEYPAD. This is required because Mathematica wants to use the return or other enter key to move to the next line. You do nor want to move to a new line. You want to enter a command. That is why you must use the ENTER key on the numeric keypad. You may also use SHIFT and ENTER together instead of using ENTER on the numeric keypad. To save your work select save from the pull down file menu, which saves it as a Mathematica .nb (notebook) file. If you wish to print your work at home, select print, then select the option of saving as a PDF. You will be unable to work with the .nb Mathe- matica file itself unless you have Mathematica installed (unlikely) but you can transport and print the .pdf file virtually anywhere. Click the line below and press ENTER on the numeric keypad. size@ 8 4.5, 7.1, 7.8, 9.1<D 4 Just above, I clicked to open a new line then typed size[{4.5, 7.1, 7.8, 9.1}] followed by a press of the numeric keypad ENTER key. Notice that off to the right of the entry there are nested brackets joining the command line and its output 4 = the number of data items in {4.5, 7.1, 7.8, 9.1}.

ü A complete list of the commands in this notebook and what they do. size [{4.5, 7.1, 7.8, 9.1}] returns 4 mean [{4.5, 7.1, 7.8, 9.1}] returns the mean 7. median [{4.5, 7.1, 7.8, 9.1}] returns the median of the list {4.5, 7.1, 7.8, 9.1} s [{4.5, 7.1, 7.8, 9.1}] returns the sample standard deviation s=1. sd [{4.5, 7.1, 7.8, 9.1}] returns the n-divisor version of standard deviation s=1. r[x, y] returns the sample correlation r = xy - x y x^2 - x^2 y^2 - y^2 for paired data. sample [{4.5, 7.1, 7.8, 9.1}, 10] returns 10 samples from 8 4.5, 7.1, 7.8, 9.1< ci [{4.5, 7.1, 7.8, 9.1}, 1.96] returns a 1.96 coefficient CI for the mean from given data bootci [mean, {4.5, 7.1, 7.8, 9.1}, 10000, 0.95] returns 0.95 bootstrap ci for pop mean smooth [{4.5, 7.1, 7.8, 9.1}, 0.2] returns the density for data at bandwidth 0. smooth 2 [{4.5, 7.1, 7.8, 9.1}, 0.2] returns the density for data at bandwidth 0. overlaid with normal densities having sd = 0.2 around each data value smoothdistribution [{{1, 700},{4 ,300}}, 0.2] returns the density at bandwidth 0. for a list consisting of 700 ones and 300 fours. popSALES is a file of 4000 sales amounts used for examples entering popSALES will spill 4000 numbers onto the screen. To prevent that enter popSALES; instead (the appended semi-colon suppresses output). betahat[matrix x, data y] returns the least squares coefficients b for a fit of the model y = x b + e. **resid[matrix x, data y]** returns the estimated errors e^^ = y - xb (see **betahat** above). **R[matrix x, data y]** returns the **multiple correlation** between the fitted values xb and data y. xquad[matrix x] returns the full quadratic extension of a design matrix with constant term xcross[matrix x] returns the extension of x to include all products of differing columns. betahatCOV[x matrix, data y] returns the estimated covariance matrix of the vector betahat ` b. normalprobabilityplot[data, dotsize] returns a normal probability plot for data (e.g. with dotsize .01). t[df, conf] returns the t-score used in lieu pf z-score in a CI for confidence conf (t[Infinity, .95] ~ 1.96). Tprob[t, df] returns P(|T| < t) for df (e.g. T[1.96, Infinity] ~ 0.95). Some changes to the grading scale are announced below. A. HW7 (below) is important. A portion of your GRADE on HW7 will also apply to boost your exam 2 GRADE as follows: NEW EXAM 2 GRADE = Exam 2 GRADE + .15 HW7 GRADE B. Your homework GRADE is 25% of your course grade whereas the average of your exam 1 and 2 GRADES is 0.5 (2/3) = 33.3% of your course GRADE (see the syllabus). That should give you a ready way to determine your overall grade to date. C. A modified grading scale is now in place for the course. Your overall course GRADE will be determined as the LARGER of (a) 0.5 (three exam average grade) + 0.25 (homework grade) + 0.25 (final exam grade) (b) 0.25 (exams 1 and 2 average grade) + 0.2 (exam 3 grade)

  • 0.25 (homework grade) + 0.3 (final exam grade) You do not have to elect a choice. It will be done automatically and will not give you a course grade lower than your grade under the original plan (a) found in the syllabus. As you can see, plan (b) places more empha- sis on the work from here on out.

HW 7 is due at the end of class Wednesday, November 5. It will count towards your regular homework in the usual way. 2** Little_Software7.nb

Try statistical methods, compare with what the book says, explain the work as best you can.

1. Re-do example 12.11 page 471 in Little Software. I've partially done this below so you can see how it is accomplished. You must a. Enter the x data and the y data making sure to maintain the proper 1:1 matchup of x-scores with y-scores. b. Transform the data into matrix form as required by the Mathematica routine. The matrix setup is used in multiple linear regression and I am giving you a head start on this way of looking at straight line regression. c. Obtain the least squares solution which includes plot the data form the LS estimate b

`

0 of^ the^ y-intercept^ (see^ also^ formula^ on^ pg.^ 469) form the LS estimate b

`

1 of^ the^ y^ to^ x^ slope^ (see^ also^ formula^ on^ pg.^ 469) plot the regression line together with the data form the estimate s

` 2

of variance of errors (see also formula on pg. 470) estimate standard error Var b

`

1 by^ s b^ `

1 (see also formula on pg. 470) determine the 95% CI for b 1 given by b

`

1 ±^ 1.96^ s b

` 1

determine the fitted values y

`

i =^ b

`

0 +^ b

`

0 xi

pull off the residuals yi - y

`

i =^ yi^ -^ (b

`

0 +^ b

`

0 xi )^ left^ by^ least^ squares

prepare a normal probability plot of the residuals calculate the coefficient of determination r^2 Where possible, compare your answers above with the output of SAS shown in table 12.15 and other commentary on this example found in your textbook.

2. Repeat (1) for the data of Example 12.4 pg. 457.

Below, I show how to do these things using only a portion of the data from Example 12.11.

You will use all of the data of 12.11.

mortarair = 8 5.7, 6.8, 9.6<; dryden = 8119 , 121.3, 118.2<; Length@mortarairD 3 You may wish to use a different point size in your plot for best legibility. 4 Little_Software7.nb

ListPlotATable@ 8 mortarair@@iDD, dryden@@iDD<, 8 i, 1 , 3 <D, AxesLabel Ø 9 "x=mortarair", "y=dryden"=, PlotStyle -> [email protected] 7 8 9 x=mortarair

y=dryden xmortarair = Table@ 81 , mortarair@@iDD<, 8 i, 1 , 3 <D; MatrixForm@xmortarairD 1 5. 1 6. 1 9. PseudoInverse is a least squares solver applicable to systems of linear equations. It produces the unique solution of simultaneous linear equations in several variables (such as the normal equations of Least Squares) if there is one. If not, it produces a particular choice of a least squares solution known as the Moore - Penrose Inverse. In the present example, the matrix formulation of the equations of our linear model is

b 0 b 1 Because the points (x, y) do not fall on a line (see the plot above) there is no exact solution of these 3 equa- tions in only 2 unknowns. The least squares solver uses a Pseudo-Inverse to find a Least Squares solution 1 5. 1 6. 1 9.

  • 1

b 0 b 1 (Careful! It is not a genuine inverse, only LS.) Observe the little dot in the code below. It denotes matrix product and is very important! betahatmortar = [email protected] 8 122.315, - 0.38211< From the line above, the LS estimated slope and intercept are 122.315 and - 0.38211 respectively. Next, find

the fitted values y

`

i =^ b

`

0 +^ b

`

1 xi.

drydenhat = xmortarair.betahatmortar 8 120.137, 119.717, 118.647< Next, overlay the LS line on the plot of (x, y). Notice that the code below asks that Mathematica join the line

of LS fitted values, i.e. the line joins points ( xi , y

`

i ).^ See^ that^ LS^ produces^ fitted^ values^ falling^ perfectly^ on^ a line (otherwise Mathematica would have plotted a broken zig-zag line). Little_Software7.nb 5

A very nice thing happens. The variances and covariances of the estimators b 0 , b 1 are just the entries of the

matrix H x tr^ x L

  • 1 s^2 , provided it exists. This is always the case if the columns of x are not linearly dependent. In Mathematica, H x tr^ x **L
  • 1** is coded Inverse[Transpose[x].x]. The preferred estimator of s^2 is s

` 2

n - 1

n - d s residuals

2 , where d is the number of columns of the design matrix x (for straight line regression above d = 2). So the least squares estimators of intercept and slope have variances and covariance that are estimated by the entries of the following matrix: Inverse@[email protected] 3 - 1 3 - 2 Hs@drydenresidDL^2 88 28.1713, - 3.6432<, 8 - 3.6432, 0.494552<< MatrixForm@%D 28.1713 - 3.

  • 3.6432 0.

From the above, we estimate the variance of b

`

0 to^ be^ 28.1713,^ the^ variance^ of^ b

`

1 to^ be

0.494552, and the covariance of b

`

0 with^ b

`

1 (same^ as^ cov^ of^ b

`

1 with^ b

`

0 )^ to^ be^ -3.6432.

So a 95% CI for b 0 Hthe true intercept absent errors of observationL is

122.315 ± t 28.

and a 95% CI for b 1 Hthe true slope absent errors of observationL is

-0.38211 ± t 0.

where t is for degrees of freedom d-2 and a = 0.025 (for 95% confidence). This use of t results

in an exact ci provided the measurements (x, y) are from a process under statistical control.

As part of this assignment, when you work with the full data of Example 12.11, you will

want to check that this CI for slope agrees with the one reported on page 472.

For a partial check on the normal errors assumption of the probability model it is customary to

perform a normal probability plot for the residuals to see if it departs very much from a straight

line. Since n is only 3, not much can be learned from the plot. But when you do the same for the

full data of Example 12.11 you can address the issue more confidently.

normalprobabilityplot@drydenresid, 0.03D

  • 1.0 - 0.5 0.5 1. zpercentile

orderstat

Here is the correlation between the independent variable mortarair and the dependent variable

dryden. Squaring it gives the coefficient of determination which is "the fraction of var y

accounted for by regression on x." It is not very large in this tiny example.

Little_Software7.nb 7

Here is the correlation between the independent variable mortarair and the dependent variable

dryden. Squaring it gives the coefficient of determination which is "the fraction of var y

accounted for by regression on x." It is not very large in this tiny example.

r@mortarair, drydenD

% ^ 2

8 Little_Software7.nb