MASTERSHEET 
Copy/Paste Formulae for Chapter 5

>Copy the formula you need
>Paste it into the other workbook you are using
>Type into the pink boxes as instructed in your other workbook
>NOTE: Do NOT alter the formulae in this workbook, these are your templates only.

A. Univariate Data Mathematica Commands

1. To create a tally of Data, a frequency table of data, calculate the mode

dataset:={"Enter data separated by commas"}
Tally[dataset]
TableForm[Tally[dataset],TableHeadings->{None, {"Column 1","Frequency"}}]
lm:=Commonest[dataset]//N
Print["mode = ",Normal[lm]]

dataset:={S,S,L,M,M,M,L,S,S,M,M,S,L,S,M,M,M,S,S,M}
Tally[dataset]
TableForm[Tally[dataset],FrequencyAllocation[dataset],TableHeadings->{None, {"Column 1","Frequency","Percentage"}}]
lm:=Commonest[dataset]//N
Print["mode = ",Normal[lm]]

2. To calculate Percentage Frequency of a Frequency Table

PercentForm[WordFrequency["\",{"\","\","\"}]]

PercentForm[WordFrequency["S,S,L,M,M,M,L,S,S,M,M,S,L,S,M,M,M,S,S,M",{"S","L","M"}]]

3. To calculate total of a Frequency Table

Total[{"Count Numbers Seperated by Commas"}]

Total[{8,3,9}]

4. To calculate Percentage Frequency of a Frequency Table

PercentForm[Solve["Per cent"==(("Count")/("Total Count")),"Per cent"]//N]

PercentForm[Solve["Per cent"==(8/20),"Per cent"]//N]

5. To create a bar chart of data

BarChart[{"Enter frequency values in order, separated by commas"},ChartLabels->{"Enter variable names in double quotation marks and separated by commas"}, ChartStyle->{"Enter colours with a capital letter and separated by commas"}]

BarChart[{3,3,2},ChartLabels->{L,M,S}, ChartStyle->{Red,Yellow,Blue}]

6. To define a data set

"Name of defined data set":={"Enter data separated by commas"}

EDHospital:={36,76,14,26,11,90,32,2,16,125,12,45,59,75,17,5,18,22,45,28,15,23,9,53,17,13,31,78,12,51}

MAXTemperature:={17,26,36,32,17,12,32,2,16,15,18,25,30,23,33,33,17,23,28,36,45,17,19,37}

homework:={1,5,3,2,1,1,2,4,3,1,4,2,4,5,3,2,2,1,1,1}

7. To create a Histogram from a defined list of data

Histogram[{"Name of defined data set"},{"Enter interval spacing"}]

Histogram[{homework},{1}]

8. To create a Histogram from a list of data

Histogram[{"Enter data separated by commas"},{"Enter interval spacing"}]

Histogram[{1,5,3,2,1,1,2,4,3,1,4,2,4,5,3,2,2,1,1,1},{1}]

9. To create a back-to-back Stem and Leaf Plot from two defined sets of data

Needs["StatisticalPlots`"]
StemLeafPlot["Enter names of two datasets separated by a comma"]

Needs["StatisticalPlots`"]
StemLeafPlot[EDHospital,MAXTemperature]

10. To calculate the Mean, the Standard Deviation

dataset1:={"Enter data separated by commas"}
lm:=Round[Mean[dataset1],0.01]//N
Print["Mean = ",Normal[lm]]
lm:=Round[StandardDeviation[dataset1],0.01]//N
Print["Standard Deviation = ",Normal[lm]]

dataset1:={176,160,163,157,168,172,173,169}
lm:=Round[Mean[dataset1],0.01]//N
Print["Mean = ",Normal[lm]]
lm:=Round[StandardDeviation[dataset1],0.01]//N
Print["Standard Deviation = ",Normal[lm]]

11. To calculate the Five-Number Summary: Minimum, (Subscript[Q, 1]),Median, (Subscript[Q, 3]), Maximum, IQR, Box Whisker Plot, Stem Leaf Plot

dataset2:={"Enter data separated by commas"}
lm:=Min[dataset2]//N
Print["Minimum = ",Normal[lm]]
lm:=If[EvenQ[Length[dataset2]], Quartiles[dataset2], Quartiles[Drop[Sort[dataset2], {((Length[dataset2] + 1)/2)}]]]//N
Print["Quartile 1, Quartile 2 could be wrong, Quartile 3= ",Normal[lm]]
lm:=Median[dataset2]//N
Print["Quartile 2 = Median = ",Normal[lm]]
lm:=Max[dataset2]//N
Print["Maximum = ",Normal[lm]]
lm:=If[EvenQ[Length[dataset2]], InterquartileRange[dataset2], InterquartileRange[Drop[Sort[dataset2], {((Length[dataset2] + 1)/2)}]]]//N
Print["IQR = ",Normal[lm]]
lm:=Max[dataset2]-Min[dataset2]//N
Print["Range = ",Normal[lm]]
BoxWhiskerChart[dataset2,"Outliers",BarOrigin->Left]//N
Needs["StatisticalPlots`"]
StemLeafPlot[dataset2]

dataset2:={28,21,21,3,22,31,35,26,27,33,43,31,30,34,48,36,35,23,24}
lm:=Min[dataset2]//N
Print["Minimum = ",Normal[lm]]
lm:=If[EvenQ[Length[dataset2]], Quartiles[dataset2], Quartiles[Drop[Sort[dataset2], {((Length[dataset2] + 1)/2)}]]]//N
Print["Quartile 1, Quartile 2 could be wrong, Quartile 3= ",Normal[lm]]
lm:=Median[dataset2]//N
Print["Quartile 2 = Median = ",Normal[lm]]
lm:=Max[dataset2]//N
Print["Maximum = ",Normal[lm]]
lm:=If[EvenQ[Length[dataset2]], InterquartileRange[dataset2], InterquartileRange[Drop[Sort[dataset2], {((Length[dataset2] + 1)/2)}]]]//N
Print["IQR = ",Normal[lm]]
lm:=Max[dataset2]-Min[dataset2]//N
Print["Range = ",Normal[lm]]
BoxWhiskerChart[dataset2,"Outliers",BarOrigin->Left]//N
Needs["StatisticalPlots`"]
StemLeafPlot[dataset2]

12. Lower Fence of  Box plot

Solve["Lower Fence"=="Lower Quartile"-1.5*("Upper Quartile"-"Lower Quartile"),"solve for"]

13. Upper Fence of  Box plot

Solve["Upper Fence"=="Upper Quartile"+1.5*("Upper Quartile"-"Lower Quartile"),"solve for"]

14. To create Parallel Box plots from two lists of data

BoxWhiskerChart[{{"Enter data separated by commas"},{"Enter data separated by commas"}},"Outliers",BarOrigin->Left]

BoxWhiskerChart[{{17,26,36,32,17,12,32,2,16,15,18,25,30,23,33,33,17,23,28,36,45,17,19,37},{36,76,14,26,11,90,32,2,16,125,12,45,59,75,17,5,18,22,45,28,15,23,9,53,17,13,31,78,12,51}},"Outliers",BarOrigin->Left]

B. Bivariate Data Mathematica Commands

1. Scatterplot In Mathematica, a Scatterplot is called a ListPlot

ListPlot[{{"Data set 1"},{"Data set 2"},{"Data set 3"},{"Data set 4"},{"Data set 5"},{"Data set 6"}},AxesLabel->{"x","y"}]

Example
Scatterplot following data.

({{Hours, 2, 6, 10, 14, 18, 22}, {No. of coffees, 20, 35, 50, 80, 120, 200}})

ListPlot[{{20,2},{35,6},{50,10},{80,14},{120,18},{200,22}},AxesLabel->{"No. of coffees","Hours"}]

2. Pearson’s Correlation Coefficient

rvalue=(Sqrt[LinearModelFit[{{"Data set 1"},{"Data set 2"},{"Data set 3"},{"Data set 4"},{"Data set 5"},{"Data set 6"},{"Data set 7"},{"Data set 8"}},x,x]["RSquared"]])

Example
The table below shows the weight (in kg) and blood glucose level (in mg/100 mL) of eight adults.
Weight	82.1	70.1	76.6	82.1	83.9	73.2	66.0	77.5
Glucose	101	89	98	100	108	104	94	89
Determine the value of Pearson’s correlation coefficient for this data set. Write your answer correct to three decimal places.

rvalue=(Sqrt[LinearModelFit[{{82.1,101},{70.1,89},{76.6,98},{82.1,100},{83.9,108},{73.2,104},{66.0, 94},{77.5,89}},x,x]["RSquared"]])
0.570406
0.570

3. Write the equation of the regression line

model=LinearModelFit[{{"Data set 1"},{"Data set 2"},{"Data set 3"},{"Data set 4"},{"Data set 5"},{"Data set 6"},{"Data set 7"},{"Data set 8"},{"Data set 9"}},x,x]

Example
The following table gives the shoe size and weight in kilograms of 10 adult males.
Shoe size	          9.5    10.0	10.5	10.5	11	9.0	8.5	9.5    7.5	 8
Weight (kg)	64	85	70	80	82	73	70	66	55	70
Write the equation of the regression line in terms of the variables weight and shoe size with the coefficients given correct to three significant figures.

model=LinearModelFit[{{9.5,64},{10.0,85},{10.5,70},{10.5,80},{11,82},{9.0,73},{8.5,70},{9.5,66},{7.5,55},{8,70}},x,x]
FittedModel[19.760504201680664` +5.50420168067227` x]
Therefore the equation of the line is  weight = 19.8 + 5.50×Shoe size

4. Fit a least squares regression line to the scatterplot

Show[ListPlot[{{"Data set 1"},{"Data set 2"},{"Data set 3"},{"Data set 4"},{"Data set 5"},{"Data set 6"},{"Data set 7"},{"Data set 8"},{"Data set 9"}},AxesLabel->{"x","y"}],Plot[Normal[model],{x,"Min","Max"}]]

Example
The following table gives the shoe size and weight in kilograms of 10 adult males.
Shoe size	          9.5    10.0	10.5	10.5	11	9.0	8.5	9.5    7.5	 8
Weight (kg)	64	85	70	80	82	73	70	66	55	70
Fit a least squares regression line to the scatterplot.

Show[ListPlot[{{9.5,64},{10.0,85},{10.5,70},{10.5,80},{11,82},{9.0,73},{8.5,70},{9.5,66},{7.5,55},{8,70}},AxesLabel->{“Shoe Size”,”Weigh(kg)”}],Plot[Normal[model],{x,7,12}]]

5. Create a Stem and Leaf Plot

Needs["StatisticalPlots`"]

StemLeafPlot[{"data 1","data 2","data 3","data 4","data 5","data 6","data 7"}]

Example
StemLeafPlot[{52,43,40,44,35,36,36,40,39,34,36,29,30,34,31,31,29,31,27,30,31,30,29}]
({{({{"Stem", "Leaves"}, {"2", "7999"}, {"3", "00011114456669"}, {"4", "0034"}, {"5", "2"}})}, {"Stem units: \!\(\*RowBox[{\"10\"}]\)"}})

6. Create a Boxplot from a list of data

BoxWhiskerChart[{"Enter data separated by commas"},"Outliers",BarOrigin->Left]

Example
BoxWhiskerChart[{52,43,40,44,35,36,36,40,39,34,36,29,30,34,31,31,29,31,27,30,31,30,29},”Outliers”,BarOrigin->Left]

7. To define a data set

"Name of defined data set":={"Enter data separated by commas"}

Example
rv:={66,54,43,42,49,45,64,61,61,66}

8. MATHEMATICA COMMANDS FOR CORE 1 (Data) Bivariate Data

a. To create a Scatter Plot (with Regression Line), Residual Plot of Data, calculate Regression Equation, Pearson’s r Value, Coefficient of Determination EV=Explanatory Variables; RV= Response Variables

ev:={"Enter EV Data Seperated by Commas"}
rv:={"Enter RV Data Seperated by Commas"}
data:=Thread[{ev,rv}]
lm=LinearModelFit[Sort[data],x,x]
Print["Regression equation:    y = ",Normal[lm]]
Print["Pearson's Correlation Coefficient r value = ",If[D[Normal[lm],x]<0,"-",""],Round[(Sqrt[lm["RSquared"]]),0.001]]
Print["Coefficient of Determination ((r^2)) =   ",Round[lm["RSquared"],0.001]]
Print["Coefficient of Determination ((r^2)) =   ",PercentForm[lm["RSquared"]]]
Show[ListPlot[data,AxesLabel->{"ev","rv"}],Plot[Normal[lm],{x,Min[ev],Max[ev]}],PlotLabel->"Scatterplot"]
ListPlot[lm["FitResiduals"], Filling->Axis,PlotLabel->"Residual Plot"]

ev:={88.7,67.0,77.5,61.7,86.8,32.4,72.4,77.1,86.5}
rv:={0.3,1.3,2.8,7.6,1.2,5.7,9.4,10.0,0.7}
data:=Thread[{ev,rv}]
lm=LinearModelFit[Sort[data],x,x]
Print["Regression equation:    y = ",Normal[lm]]
Print["Pearson's Correlation Coefficient r value = ",If[D[Normal[lm],x]<0,"- ",""],Round[(Sqrt[lm["RSquared"]]),0.001]]
Print["Coefficient of Determination ((r^2)) =   ",Round[lm["RSquared"],0.001]]
Print["Coefficient of Determination ((r^2)) =   ",PercentForm[lm["RSquared"]]]
Show[ListPlot[data,AxesLabel->{"ev","rv"}],Plot[Normal[lm],{x,Min[ev],Max[ev]}],PlotLabel->"Scatterplot"]
ListPlot[lm["FitResiduals"], Filling->Axis,PlotLabel->"Residual Plot"]

b. Finding the equation of the line from two points (x1,y1) and (x2,y2) along the line

lm=LinearModelFit[{{(Subscript[x, 1]),(Subscript[y, 1])},{(Subscript[x, 2]),(Subscript[y, 2])}},x,x]
Print["Equation:    y = ",Normal[lm]]

Example
Find the equation of the line that passes through the points (0,3) and (2,2)
lm=LinearModelFit[{{0,3},{2,2}},x,x]
Print[“Equation:    y = “,Normal[lm]]
FittedModel[3.` -0.5` x]
Equation: y = 3 - 0.5x

c. Predict a value from a given equation

"Enter equation here"/.x->"value for prediction"

Example
Predict the value of x = 3 for the equation y = 3 - 0.5x
y=3-0.5x/.x->3
1.5`

d. Calculating slope b by r value,  Sx EV  standard deviation & Sy RV standard deviation

slope[r_,Sx_,Sy_]:=((r*Sy)/Sx)
NSolve[slope["r value Correlation coefficient","Sx Standard deviation of EV","Sy Standard deviation of RV"]=="Slope value",Reals]

slope[r_,Sx_,Sy_]:=((r*Sy)/Sx)
NSolve[slope["r value Correlation coefficient",4.29,1.69]==0.312,Reals]

e. Calculating intercept a by  slope b, (OverTilde[x]) EV mean & (OverTilde[y]) RV mean

intercept[b_,x_,y_]:=y-b*x
NSolve[intercept["slope b","\!\(\*FormBox[RowBox[{StyleBox[OverscriptBox[\"x\", \"~\"],FontWeight->\"Bold\"], StyleBox[\" \",Background->RGBColor[1, 1, 0]], \"EV\", \" \", \"Mean\"}],TraditionalForm]\)","\!\(\*FormBox[RowBox[{StyleBox[OverscriptBox[RowBox[{\"y\", \" \"}], \"~\"],FontWeight->\"Bold\"], StyleBox[\" \",Background->RGBColor[1, 1, 0]], \"RV\", \" \", \"Mean\"}],TraditionalForm]\)"]=="intercept a",Reals]

intercept[b_,x_,y_]:=y-b*x
NSolve[intercept[-0.408,18.0,15.7]=="intercept a",Reals]

a. The Squared (x^2) Transformation

ev:={"Enter EV Data Seperated by Commas"}
rv:={"Enter RV Data Seperated by Commas"}
data1=Thread[{ev,rv}];
scatdata1=ListPlot[data1]
TableForm[{{(ev^2),rv,ev}},TableHeadings->{None,{"(ev^2)","rv","ev"}}]//N
data:=Thread[{(ev^2),rv}]
lm=LinearModelFit[Sort[data],x,x]
Print["Regression equation:    y = ",Normal[lm], " ->(x^2)",Style["\tWarning: You have to change x to \!\(\*SuperscriptBox[\(X\), \(\(2\)\(\\ \)\)]\) to the equation yourself when writing it!",{Red,Small}]]
Print["Pearson's Correlation Coefficient r value = ",Round[(Sqrt[lm["RSquared"]]),0.000001],Style["\tWarning: the r value is negative if the slope is negative!!",{Red,Small}]]
Print["Coefficient of Determination ((r^2)) =   ",Round[lm["RSquared"],0.00000001]]
Print["Coefficient of Determination ((r^2)) =   ",PercentForm[lm["RSquared"]]]
Show[ListPlot[data,AxesLabel->{"ev","rv"}],
Plot[Normal[lm],{x,0,125}],PlotLabel-> "Scatterplot with X Squared Transformation"]
ListPlot[lm["FitResiduals"], Filling->Axis, PlotLabel->"Residual Plot"]

b. The Transformation

If[evname=="",assigned,notassigned,evname="(ev)"];
If[rvname=="",assigned,notassigned,rvname="(rv)"];
DialogInput[Grid[{{"EV Name",InputField[Dynamic[evname],String,FieldSize->10],"EV values",InputField[Dynamic[evinput],String,FieldSize->Automatic]},{"RV Name", InputField[Dynamic[rvname],String,FieldSize->10],"RV values",InputField[Dynamic[rvinput],String]},
{"Transformation:",ButtonBar[{"None":>DialogReturn[tfmType=1],"(x^2)":>DialogReturn[tfmType=2],"Log x":>DialogReturn[tfmType=3],"(1/x)":>DialogReturn[tfmType=4],"(y^2)":>DialogReturn[tfmType=5],"Log y":>D
ialogReturn[tfmType=6],"(1/y)":>DialogReturn[tfmType=7]},ImageMargins->5,ImageSize->{50,40},FrameMargins->5,Background->LightBlue],SpanFromLeft,SpanFromLeft}}],WindowTitle->"Regression analysis."];
tfmlist:={{{"No transformation"},{"y","x"},{"ev","rv"}},{{"x-Squared"},{"y","(x^2)"},{"(ev^2)","rv"}},{{"(Subscript[log, 10])(x)"},{"y","(Subscript[Log, 10])(x)"},{"Log10[ev]","rv"}},{{"reciprocal x"},{"y","(1/x)"},{"(1/ev)","rv"}},{{"y-squared"},{"(y^2)","x"},{"ev","(rv^2)"}},{{"(Subscript[log, 10])(y)"},{"(Subscript[Log, 10])(y)","x"},{"ev","Log10[rv]"}},{{"reciprocal y"},{"(1/y)","x"},{"ev","(1/rv)"}}}
ev:=ImportString[evinput,"List","LineSeparators"->","]
rv:=ImportString[rvinput,"List","LineSeparators"->","]
data:=Thread[ToExpression[Part[tfmlist,tfmType,3]]]
label="Scatterplot with "<>Part[tfmlist,tfmType,1,1]<>" transformation";
xaxislabel=StringReplace[Part[tfmlist,tfmType,3,1],"ev"->evname];
yaxislabel=StringReplace[Part[tfmlist,tfmType,3,2],"rv"->rvname];
xmin=Min[ToExpression[Part[tfmlist,tfmType,3,1]]];
xmax = Max[ToExpression[Part[tfmlist,tfmType,3,1]]];
lm=LinearModelFit[Sort[data],x,x];
Print["Regression equation: ",Part[tfmlist,tfmType,2,1]," = ",Extract[Normal[lm],1],If[D[Normal[lm],x]<0," - "," + "],Abs[Extract[Normal[lm],2]/x],Part[tfmlist,tfmType,2,2],"    ( ",yaxislabel," = ",Extract[Normal[lm],1],If[D[Normal[lm],x]<0," - "," + "],Abs[Extract[Normal[lm],2]/x]," x ",xaxislabel,"  )"]
Print["Pearson's r value = ",If[D[Normal[lm],x]<0,0-(Sqrt[lm["RSquared"]]),(Sqrt[lm["RSquared"]])]]
Print["Coefficient of Determination ((r^2)) =   ",lm["RSquared"]]
Show[ListPlot[data,AxesLabel->{xaxislabel,yaxislabel}],Plot[Normal[lm],{x,xmin,xmax}],PlotLabel->If[tfmType>1,label,"Scatterplot"]]
ListPlot[lm["FitResiduals"], Filling->Axis,PlotLabel->"Residual Plot"]