2
$\begingroup$

I have a lot of data organized in Datasets. In the end I'm using ErrorListPlots to show different variables. Therefore I have to calculate mean values, standard deviations, Sqrt[n]... Afterwards I have to prepare list for plotting containing values, coords, errors.

As shown in this example:

values = {32, 39, 27, 30, 32, 14, 28, 21, 32, 24, 32, 41, 26, 27, 37, 
22, 27, 29, 27, 37, 14, 28, 32, 27, 28, 27, 35, 28, 26, 39, 21, 29,
 33, 17, 20, 25, 28, 39, 14, 16, 33, 29, 31, 29, 25, 30, 40, 31, 
32};
values2 = {40, 39, 30, 40, 32, 42, 25, 7, 15, 30, 28, 40, 13, 40, 15, 
33, 31, 17, 19, 22, 29, 38, 30, 29, 29, 20, 17, 16, 13, 21, 19, 20, 
28, 19};

mean = Mean[values]; sd = StandardDeviation[values]; n = Length[values];
sqn = Sqrt[n];
mean2 = Mean[values2]; sd2 = StandardDeviation[values2]; n2 = Length[values2];
sqn2 = Sqrt[n2];

xVal = {10, 15};
yVal = {mean, mean2};
errVal = {sd/sqn, sd2/sqn2};
coordsVal = Riffle[xVal, yVal]; 
tempELPD = Partition[Riffle[coordsVal, errVal, {3, -1, 3}], 3];

tempELP = 
 ErrorListPlot[{tempELPD}, PlotRange -> {{9, 17}, {0, 40}}, 
 PlotTheme -> "Detailed", Axes -> False, ImageSize -> Large, 
 PlotStyle -> { Default, Default, Default, Black}]

At the moment I did a simple copy and paste (and re-naming of variables) of the example for many variables, but in this way my code gets very long. And since I want to do this now for even a lot more variables and different datasets, I'm thinking about the best possibility to organize the code.

I read some things here and there, but I didn't have a good idea at the moment how to realize this in Mathematica. Is it to use a for loop or better using functions for each step?

I'm thankful for every hint.

$\endgroup$
1
  • $\begingroup$ Maybe this question was a little to generic, as far as I understood now, functional programming is the "best" solution in Mathematica. And using pure functions is the expert way. Still much to learn... $\endgroup$
    – Lea
    Commented Mar 7, 2018 at 9:26

2 Answers 2

1
$\begingroup$

You can get yVal and errVal in a single step:

{yVal, errVal} = Transpose[Through[{Mean, StandardDeviation[#] / Sqrt[Length @ #]&}@ #]&/@
 {values, values2}];
$\endgroup$
1
  • $\begingroup$ This is a nice way to shorten the first part. Maybe I can put the second in a function and have than a way better solution. $\endgroup$
    – Lea
    Commented Mar 7, 2018 at 9:13
0
$\begingroup$

In order to make my improvements available, I post here an answer to my own question. I'm using kglr's proposal (statFun) and I'm also introducing a faster way to obtain data (ELPD) for the ErrorListPlots and a function to show these plots (labelPlotFun). With these tools I'm able to really safe code lines even for many datasets:

statFun[list__] := 
 Transpose[Through[{Mean, StandardDeviation[#]/Sqrt[Length[#]] &}@#] & /@ {list}]

xVal = {10, 15}; 
{yVal, errVal} = statFun[values, values2];

tempELPD = Partition[Riffle[Riffle[xVal, yVal], errVal, {3, -1, 3}], 3];
tempELPD2 = Partition[Riffle[Riffle[xVal + 0.5, yVal], errVal, {3, -1, 3}], 3];

labelPlotFun[label__String, list__List, name__String] := 
 ErrorListPlot[{list}, PlotTheme -> "Detailed", Axes -> False, ImageSize->Large,
 FrameLabel -> {label}, PlotLegends -> Placed[{name}, Scaled[{0.1, 0.15}],
 PlotRange -> {{9, 18}, {0, 40}}]

tempELP = 
 labelPlotFun["variable x", "variable y", tempELPD, tempELPD2,"data","data shifted"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.