PeterBurbery/ AssociationFunctions

Functions for modifying associations

Contributed by: Peter Cullen Burbery

Functions for manipulating and creating associations.

Installation Instructions

To install this paclet in your Wolfram Language environment, evaluate this code:
PacletInstall["PeterBurbery/AssociationFunctions"]


To load the code after installation, evaluate this code:
Needs["PeterBurbery`AssociationFunctions`"]

Paclet Guide

Examples

Basic Examples (2) 

Generate an Association to label the results of different operations on an expression:

In[1]:=
AssociationThrough[{f, g}, {1, 2, 3, 4}]
Out[1]=

Perform multiple statistical operations on a single distribution, returning a single expression:

In[2]:=
AssociationThrough[{Min, Max, StandardDeviation, GeometricMean}, {1, 2, 3, 4}]
Out[2]=

Statistical Examples (11) 

The keys are cities and the values are the cost of a round trip airplane ticket:

In[3]:=
cities = <|"Miami" -> 230, "San Diego" -> 651, "New York City" -> 221,
    "Chicago" -> 279, "Seattle" -> 722, "Salt Lake City" -> 768, "Boston" -> 221, "Honolulu" -> 970, "Denver" -> 529, "Boulder, Colorado" -> 529|>;

I create a function to compute the geographical coordinates of a place from Wikidata:

In[4]:=
WikidataGeoPosition[place_?StringQ] := First[WikidataData[First[WikidataSearch[place]], ExternalIdentifier["WikidataID", "P625", <|"Label" -> "coordinate location", "Description" -> "geocoordinates of the subject. For Earth, please note that only WGS84 coordinating system is supported at the moment"|>]]]

compute the distances to the cities:

In[5]:=
data = AssociationMap[<|"Position" -> WikidataGeoPosition[#], "Distance" -> GeoDistance[Here, WikidataGeoPosition[#]], "RoundtripTicket" -> cities[#]|> &, Keys[cities]]
Out[5]=

organize the data for processing:

In[6]:=
dataset = Dataset[data]
Out[6]=

turn the data into an association:

In[7]:=
geoData = AssociationThread[Values@Normal@dataset[All, "Position"], Values@Normal@dataset[All, "RoundtripTicket"]]
Out[7]=

Make a geo bubble chart for the cost of the round trip ticket and the distance:

In[8]:=
Labeled[GeoBubbleChart[
  AssociationThread[Values@Normal@dataset[All, "Position"], Values@Normal@dataset[All, "RoundtripTicket"]], Sequence[
  ImageSize -> Large, GeoBackground -> "VectorClassic", ChartStyle -> "Pastel"]], "Round trip ticket geo bubble chart"]
Out[8]=
In[9]:=
Labeled[GeoBubbleChart[
  AssociationThread[Values@Normal@dataset[All, "Position"], Values@Normal@dataset[All, "Distance"]], Sequence[
  ImageSize -> Large, GeoBackground -> "VectorClassic", ChartStyle -> "Pastel"]], "Distance geobubble chart"]
Out[9]=

Make a plot of distance and cost:

In[10]:=
AssociationThread[Values@Normal@dataset[All, "Distance"], Values@Normal@dataset[All, "RoundtripTicket"]]
Out[10]=
In[11]:=
Labeled[ListPlot[
  AssociationThread[Values@Normal@dataset[All, "Distance"], Values@Normal@dataset[All, "RoundtripTicket"]], ImageSize -> Large,
   PlotStyle -> "Pastel"], "Distance verses cost"]
Out[11]=

I think the line starts at 100 and has a slope of 0.15:

In[12]:=
Show[ListPlot[
  AssociationThread[Values@Normal@dataset[All, "Distance"], Values@Normal@dataset[All, "RoundtripTicket"]], ImageSize -> Full, PlotStyle -> "Pastel"], Plot[150 + 0.15 x, {x, 0, 7000}]]
Out[12]=
In[13]:=
CopyToClipboard[
 Rasterize@
  Show[ListPlot[
    AssociationThread[Values@Normal@dataset[All, "Distance"], Values@Normal@dataset[All, "RoundtripTicket"]], ImageSize -> Full, PlotStyle -> "Pastel"], Plot[150 + 0.15 x, {x, 0, 7000}]]]

Let's test our prediction with linear interpolation:

In[14]:=
{Values@Normal@dataset[All, #Distance &], Values@Normal@dataset[All, #RoundtripTicket &]}
Out[14]=

Calculate the linear interpolation for the data:

In[15]:=
linearModelFit = LinearModelFit[
  Transpose@{Values@
     Normal@dataset[All, QuantityMagnitude[#Distance] &], Values@Normal@dataset[All, #RoundtripTicket &]}, x, x]
Out[15]=

The correct line of best fit is in green and my guess for the line of best fit is in purple:

In[16]:=
Show[ListPlot[linearModelFit["Data"], ImageSize -> Full, PlotStyle -> Blue], Plot[150 + 0.15 x, {x, 0, 7000}, PlotStyle -> Purple], Plot[linearModelFit[x], {x, 0, QuantityMagnitude@Max@dataset[All, #Distance &]}, PlotStyle -> Green]]
Out[16]=

Scope


Neat Examples (8) 

Create a constant association:

In[17]:=
Needs["PeterBurbery`AssociationFunctions`"]
In[18]:=
ConstantAssociation[{a, b, c, d}, x]
Out[18]=

Partition an association:

In[19]:=
AssociationPartition[AssociationMap[f, ToExpression /@ Alphabet[]], 2]
Out[19]=

Combine partitioning associations with other association operations like AssociationThread:

In[20]:=
AssociationThread[
 Range[3] -> (AssociationThread[Range[2] -> #] & /@ Partition[
     AssociationThread[Range[2] -> #] & /@ Partition[
       AssociationPartition[
        AssociationMap[f, ToExpression /@ Alphabet[][[;; 24]]], 2], 2], 2])]
Out[20]=

Create a nested dataset:

In[21]:=
Dataset[
AssociationThread[
 Range[3] -> Map[AssociationThread[Range[2] -> #]& , 
Partition[
Map[AssociationThread[Range[2] -> #]& , 
Partition[
PeterBurbery`AssociationFunctions`AssociationPartition[
AssociationMap[f, 
Map[ToExpression, 
Part[
Alphabet[], 
Span[1, 24]]]], 2], 2]], 2]]]]
Out[21]=

Create a large nested association:

In[22]:=
AssociationThread[
 Range[3] -> (AssociationThread[Range[2] -> #] & /@ Partition[
     AssociationThread[Range[2] -> #] & /@ Partition[
       AssociationPartition[AssociationMap[f, Array[a, 24]], 2], 2], 2])]
Out[22]=

Make a large nested dataset:

In[23]:=
Dataset[
AssociationThread[
 Range[3] -> Map[AssociationThread[Range[2] -> #]& , 
Partition[
Map[AssociationThread[Range[2] -> #]& , 
Partition[
PeterBurbery`AssociationFunctions`AssociationPartition[
AssociationMap[f, 
Array[a, 24]], 2], 2]], 2]]]]
Out[23]=

Find all the properties for a linear model fit:

In[24]:=
data = Table[k + RandomInteger[{-10, 10}], {k, 5, 100, 5}]
Out[24]=
In[25]:=
linearModelFit = LinearModelFit[data, x, x]
Out[25]=
In[26]:=
PropertiesSummary[linearModelFit]
Out[26]=

Find all the values for a country:

In[27]:=
PropertiesSummary[Entity["Country", "UnitedStates"]]
Out[27]=

Publisher

Peter Burbery

Compatibility

Wolfram Language Version 13.1

Version History

  • 1.5.0 – 13 July 2022
  • 1.4.1 – 13 July 2022
  • 1.4.0 – 09 July 2022
  • 1.3.0 – 08 July 2022
  • 1.2.0 – 08 July 2022
  • 1.1.0 – 08 July 2022
  • 1.0.1 – 08 July 2022
  • 1.0.0 – 08 July 2022

License Information

MIT License

Paclet Source