0
$\begingroup$

I am attempting to import data from a database, group it according to a given identification parameter, get it's statistically relevant information, and then export it as a JSON object for visualization in d3. Previously, I had been hard-coding functions for each piece of my data to be passed through a FormatForJSON function:

FormatForJSON[function_, domain_] := 
<|"function" -> ToString@function, 
"output" -> Map[<|"x" -> #, 
"y" -> function@#|> &, domain]|>

Instead, I would like to automate this by building the JSON during the post-processing of the import thru the use of this function:

JSONBuilder[ID_String, data_List] := 
Block[{records, H, fittedDist, pdf, pdfFunc},
(*container for records*)
records = <||>;

(*stats*)
H = FindDistribution[data];
pdfFunc = PDF[H];

(*fitted dist info*) 
fittedDist = <|"dist" -> ToString@H, 
"params" -> KeyMap[ToString]@(Association@(H)), 
"pdf" -> ToString@FullForm@PDF[H][x], 
"pdfFunction" -> <|"function" -> ToString@FullForm[pdfFunc@x], 
"line" -> FormatForJSON[pdfFunc, data]|>|>;

(*add all info to records*)
AppendTo[records, ID -> fittedDist]]

However, I am getting an association error in KeyMap:

KeyMap::invak: "The argument Association[H] is not a valid Association"

I previously ran JSONBuilder using DistributionFitTest and it worked just fine; all I have modified is that function to FindDistribution with the appropriate arguments, so I don't understand why the association wouldn't occur like normal (no pun intended). They have the same Attributes, and I've limited it to find the single best distribution (as opposed to producing several in a list, which I think would require an AssociationThread); is there a subtle difference between the two functions that I am just not seeing?

Edit: Here is the working code for when DistributionFitTest is used:

JSONBuilder[ID_String, data_List] := 
Block[{records, H, fittedDist, pdf, pdfFunc},
(*container for records*)
records = <||>;

(*stats*)
H = DistributionFitTest[data, Automatic, "HypothesisTestData"];
pdfFunc = PDF[H["FittedDistribution"]];

(*fitted dist info*)
fittedDist = <|
"dist" -> ToString@H["FittedDistribution"], 
"params" -> KeyMap[ToString]@Association@(H["FittedDistributionParameters"]), 
"pdf" -> ToString@FullForm@PDF[H["FittedDistribution"]][x],
"pdfFunction" -> <|"function" -> ToString@FullForm[pdfFunc@x], 
"line" -> FormatForJSON[pdfFunc, data]|>|>;

(*add all info to records*)
AppendTo[records, ID -> fittedDist]]

Update:

Apparently the object that DistributionFitTest[data, Automatic, "HypothesisTestData"] outputs is listable. I have reason to believe that my data doesn't follow a normal distribution; is there a way to have FindDistribution output a List?

$\endgroup$
  • 1
    $\begingroup$ That error is caused because H is not a list, meaning you cannot make it into an Association. $\endgroup$ – JungHwan Min Jul 22 '16 at 22:04
  • $\begingroup$ @JHM that makes sense, but then what doesn't make sense to me is why it worked for DistributionFitTest, which doesn't produce a list either. $\endgroup$ – Lame-Ov2.0 Jul 22 '16 at 22:15
  • $\begingroup$ That is odd... DistributionFitTest returns a Real, so it can't be made an Association. Could you post the working code that has DistributionFitTest? $\endgroup$ – JungHwan Min Jul 22 '16 at 22:19
  • $\begingroup$ @JHM Certainly. Went ahead and edited the post to add it in $\endgroup$ – Lame-Ov2.0 Jul 22 '16 at 22:35
  • $\begingroup$ DistributionFitTest[data, Automatic, "HypothesisTestData"]["FittedDistributionParameters"] does give you a List of Rules so it works... $\endgroup$ – JungHwan Min Jul 22 '16 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.