Fitting Data in Plots

Nirmal Malapaka, Wolfram Research

#WolframTechConf

Fitting Data in Plots

Visualizing fitted data

Interpolation vs. Regression

WL functions for fitting data

ListFitPlots

PlotFit

PlotFitElements

Data for fitting

Fit and visualize

Future directions

Talks

Unified Fitting and Modeling in Wolfram Language

Giulio Alessandrini

Friday, November 7, 9:00 AM-9:30 AM CT

Visualization Updates

Brett Champion

Thursday, November 6, 11:30 AM-12:00 PM CT

Styling Tabular

MinHsuan Peng

Thursday, November 6, 1:00 PM-1:30 PM CT

Visualization Office Hours

Thursday, November 6, 3:30 PM-4:30 PM CT

Friday, November 7, 9:30 AM-10:30 AM CT

Visualizing fitted data

Interpolation vs. Regression

Fitting

SeedRandom[1];data3d=Table[i+j+RandomReal[{-100, 100}],{i,10},{j,15}];

SeedRandom[1];data2d=Table[{i,i+RandomReal[25]},{i,1,30}];

ListPlot[data2d,]

ListLinePlot[data2d,]

Plot[Interpolation[data2d,InterpolationOrder->1][x],{x,1,30},]

Fitting

gkern=Table[Exp[-n^2/(1.2*10^2)]/(Sqrt[2. Pi]*1.2),{n,-1,1}];

gsmooth=ListConvolve[gkern,Last/@data2d];

Show[ListPlot[data2d],ListLinePlot[gsmooth],]

mvavg=ListConvolve[{1,1,1}/3,Last/@data2d];

Show[ListPlot[data2d],ListLinePlot[mvavg,InterpolationOrder->2],]

mgkern=Table[Exp[-2*n^2],{n,.1,1}];

mgsmooth=ListConvolve[mgkern,Last/@data2d];

Show[ListPlot[data2d],ListLinePlot[mgsmooth],]

lmf=LinearModelFit[data2d,x,x]

Show[ListLinePlot[data2d],Plot[lmf[x],{x,1,30}],]

LocalModelFit

KernelModelFit

Fitting

mf=LocalModelFit[data2d]

Show[ListLinePlot[data2d],Plot[mf[x],{x,1,30}],]

kmf=KernelModelFit[data2d]

Show[ListLinePlot[data2d],Plot[kmf[x],{x,1,30}],]

Fit Plots

ListFitPlot[data2d,Frame->True]

ListFitPlot3D[data3d]

PlotFit

PlotFit

ListPlot[data2d,PlotFit->"Kernel",]

ListPlot3D[data3d,PlotFit->"Quadratic"]

PlotFitElements

PlotFitElements

ListPlot[data2d,PlotFit->"Kernel",PlotFitElements->{"Residuals",{"BandCurves",<|"Style"->StandardRed|>}},]

ListPlot3D[data3d,PlotFit->"Kernel",PlotFitElements->{{"BandSurfaces",<|"PillarPoints"->True|>},"DataPoints"}]

Diamonds

diamonds=ResourceData["Sample Tabular Data: Diamonds"];

dts=Tabular[diamonds,ImageSize->{All,150}]

Grid[{{TabularStructure[diamonds],dia3D}},ItemSize->All]

Diamonds

cut=Normal[diamonds[All,"cut"]["Categories"]]

color=Normal[diamonds[All,"color"]["Categories"]]

clarity=Normal[diamonds[All,"clarity"]["Categories"]]

Quantitative Variables

carats=Length[DeleteDuplicates[Normal[diamonds[All,"carat"]]]]

Histogram[diamonds->"carat",PlotRange->All,Frame->True,PlotLabel->"carat"]

Histogram[diamonds->"price",Frame->True, PlotLabel->"price"]

Histogram[diamonds->"price","Log",Frame->True,PlotLabel->"Log[price]"]

Categorical Variables

Histogram[diamonds->"color",ChartStyle->colors,PlotRange->All,LabelingFunction->Above,ChartElements->mydia,ChartLegends->leg,ImageSize->Large]

Row[Histogram[diamonds->#,ScalingFunctions->{NominalScale[Automatic],None},ImageSize->300,PlotLabel->#]&/@{"clarity","cut"}]

Categorical Variables

dts

ListPlot[diamonds->{"carat","price"},]

ListPlot[diamonds->{"carat","price"},PlotFit->"Linear",PlotRange->20000,]

ListPlot[diamonds->{"carat","price"},PlotFit->Automatic,PlotFitElements->"BandCurves",]

ListPlot[diamonds->{"carat","price"},PlotFit->Automatic,PlotFitElements->{"BandCurves",<|"Style"->StandardOrange,"FillingStyle"->StandardOrange|>},]

Cut

cut2carat=PivotToColumns[diamonds,"cut"->"carat"]

ListPlot[cut2carat->{"Fair","price"},PlotFit->Automatic,]

ListPlot[cut2carat->{"Fair","price"},PlotFit->Automatic,PlotFitElements->"BandCurves",]

pd=PivotTable[diamonds,Mean[#price]&,"carat","cut"]

ListPlot[pd->{"carat","Fair"},PlotFit->Automatic,]

ListPlot[pd->{"carat","Fair"},PlotFit->"Automatic",PlotFitElements->"BandCurves",]

ListPlot[pd->{"carat","Fair"},PlotFit->"Kernel",]

ListPlot[pd->{{"carat","Fair"},{"carat","Ideal"}},PlotFitElements->{"BandCurves",<|"Style"->{StandardBlue,StandardYellow},"FillingStyle"->{StandardBlue,StandardYellow}|>},PlotFit->Automatic,]

Color

pd2=PivotTable[diamonds,Mean[#price]&,"carat","color"]

ListPlot[pd2->{"carat","D"},]

SpearmanRankTest[Sequence@@Transpose@QuantityMagnitude@DeleteMissing[FromTabular[pd2[All,{"carat","J"}],"Matrix"],1,All],"TestStatistic"]

ListPlot[pd2->{"carat","D"},PlotFit->Automatic,PlotFitElements->{"Residuals",<|"Style"->Directive[StandardRed,Opacity[0.6],Thick]|>},]

ListPlot[pd2->{{"carat","D"},{"carat","J"}},PlotFit->Automatic,]

ListPlot[pd2->{{"carat","D"},{"carat","J"}},PlotFitElements->{"Residuals",<|"Style"->{Directive[StandardBlue,Opacity[0.5],Thick],Directive[StandardYellow,Opacity[0.5],Thick]}|>},PlotFit->Automatic,]

Clarity

pd3=PivotTable[diamonds,Mean[#price]&,"carat","clarity"]

ListPlot[pd3->{"carat","I1"},PlotFit->"Automatic",]

ListPlot[pd3->{"carat","I1"},PlotFit->"Linear",PlotFitElements->{"Residuals",<|"Style"->Directive[StandardBlue,Opacity[.7]]|>},]

ListPlot[pd3->{{"carat","I1"},{"carat","IF"}},PlotFit->"Linear",PlotFitElements->"BandCurves",]

Modeling

lmf=LinearModelFit[MapAt[Log,QuantityMagnitude[FromTabular[diamonds[All,{"carat","price"}],"Matrix"]],{All,2}],x,x]

lmf["BestFit"]

ListPlot[diamonds->{"carat","price"},PlotFit->lmf,ScalingFunctions->"Log",PlotFitElements->{"BandCurves",<|"Style"->Red|>},]

ListPlot[diamonds->{"carat","price"},PlotFit->"Linear",ScalingFunctions->"Log",]

lmf2=LinearModelFit[MapAt[Log,QuantityMagnitude[FromTabular[diamonds[All,{"carat","price"}],"Matrix"]],{All,2}],CubeRoot[x],x]

ListPlot[diamonds->{"carat","price"},PlotFit->lmf2,PlotFitElements->{"FitCurve",<|"Style"->Red|>},ScalingFunctions->"Log",]

Multi-Variable

ListFitPlot3D[RandomSample[diamonds,5000]->{"price","table","depth"},PlotFit->Automatic,PlotFitElements->{{"BandSurfaces",<|"PillarPoints"->True|>},"DataPoints"},PlotRange->80,MaxPlotPoints->1000,AxesLabel->{"price","table","depth"}]

ListFitPlot3D[diamonds->{"carat","price","depth"},PlotFit->Automatic,PlotFitElements->{{"BandSurfaces",<|"PillarPoints"->True|>},"DataPoints"},AxesLabel->{"carat","price","depth"}]

ListPlot3D[RandomSample[diamonds,1000]->{"depth","y","table"},PlotFit->Automatic,PlotFitElements->{"Residuals",{"DataPoints",<|"Style"->Directive[StandardBlue,AbsolutePointSize[2]]|>}},AxesLabel->{"depth","y","table"}]

Multi-Variable

ListContourPlot[RandomSample[diamonds,1000]->{"depth","y","table"},PlotFit->Automatic,PlotFitElements->{"BandSurfaces",{"DataPoints",<|"Style"->Directive[StandardBlue,AbsolutePointSize[2]]|>}},FrameLabel->{"depth","width"}]

ListDensityPlot[RandomSample[diamonds,1000]->{"depth","z","table"},PlotFit->Automatic,PlotFitElements->{"BandSurfaces",{"DataPoints",<|"Style"->Directive[StandardBlue,AbsolutePointSize[2]]|>}},FrameLabel->{"depth%","depth"}]

Multi-Panel

dts

PairwiseListPlot[diamonds->{"carat","depth","table","price","x","y","z"}]

Multi-Panel

PairwiseListPlot[diamonds->{"carat","depth","table","price","x","y","z"},PlotFit->"Linear"]

Multi-Panel

colorgroups=PivotTable[diamonds,Function[Length[#clarity]],"color","clarity"];

cg=SortBy[colorgroups,First]

BarChart[cg->"IF",ChartLabels->Normal[cg[All,"color"]]]

PairwiseListPlot[cg->Rest[ColumnKeys[colorgroups]],PlotFit->Automatic,PlotRange->Automatic]

Multi-Panel

PairwiseQuantilePlot[cg->Rest[ColumnKeys[colorgroups]]]

Future Directions

cut2price=PivotToColumns[diamonds,"cut"->"price"];

BoxWhiskerChart[cut2price->cut,ChartLabels->cut,]

clarity2price=PivotToColumns[diamonds,"clarity"->"price"];

DistributionChart[clarity2price->clarity,ChartLabels->clarity,]

jpd=ColumnwiseCombine[{pd,pd2,pd3}->"carat"];

BubbleChart[jpd->{"IF","J","Ideal"},]

BubbleChart[jpd->{{"IF","J","carat"},{"I1","D","carat"}},]

BubbleChart3D[jpd->{{"IF","J","Ideal","carat"},{"I1","D","Fair","carat"}},]