Chapter 7 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 Chapter 3 & 4

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]

9. MATHEMATICA COMMANDS FOR CORE 1 (Data) Data Transformation Chapter 5

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"]