0
$\begingroup$

I am importing a .dat file that contains two columns of data and performing various statistical analyses on the data. Column 1 is the dataset and column 2 is the uncertainties (sigma) in the values of column 1. I can not get WeightedData to work properly. The weight (w) is set to 1/sigma^2 and I keep getting the error that this is not a valid weight specification. How do I resolve this error?

$Path = Append[$Path, "c:"]
grades = OpenRead["grades.dat"]
data = ReadList[grades, {Number, Number}]
Close[grades]
Mean[data[[All, 1]]] // N
StandardDeviation[data[[All, 1]]] // N
w = (1/({data[[All, 2]]}^2)) // N
data2 = {data[[All, 1]]}
\[ScriptCapitalA] = WeightedData[data2, w]
InputStream["grades.dat", 3]

{{42, 47}, {40, 9.2}, {4, 9.5}, {36, 83.6}, {45, 9.5}, {35, 8.9}, {78,
   53}, {1000, 9.5}, {423, 7.8}, {85, 9.3}, {934, 959}, {84, 
  9.2}, {59, 7.8}, {33, 7.7}, {62, 7.9}, {53, 77}, {33, 9.2}, {81, 
  9}, {65, 8.1}, {33, 8.6}, {69, 8.5}, {69, 42.7}, {53, 7.7}, {55, 
  7.5}, {33, 8.4}, {82, 9}, {81, 9}, {76, 8.9}, {2076, 9.3}, {83, 
  9.3}, {74, 8.9}, {86, 8.9}, {78, 9.2}, {43, 7.7}, {66, 8.3}, {60, 
  7.8}, {68, 8.3}, {92, 10}, {1365, 9.5}, {86, 67}, {88, 6.8}, {43, 
  45.3}, {78, 54.6}, {77, 42.4}, {67, 54}, {45, 34}, {33, 56}, {102, 
  722}, {56, 54}, {55, 54}}

grades.dat

171.28

376.54

{{0.000452694, 0.0118147, 0.0110803, 0.000143083, 0.0110803, 
  0.0126247, 0.000355999, 0.0110803, 0.0164366, 0.011562, 
  1.08733*10^-6, 0.0118147, 0.0164366, 0.0168663, 0.0160231, 
  0.000168663, 0.0118147, 0.0123457, 0.0152416, 0.0135208, 0.0138408, 
  0.000548459, 0.0168663, 0.0177778, 0.0141723, 0.0123457, 0.0123457, 
  0.0126247, 0.011562, 0.011562, 0.0126247, 0.0126247, 0.0118147, 
  0.0168663, 0.0145159, 0.0164366, 0.0145159, 0.01, 0.0110803, 
  0.000222767, 0.0216263, 0.000487308, 0.00033544, 0.000556248, 
  0.000342936, 0.000865052, 0.000318878, 1.91834*10^-6, 0.000342936, 
  0.000342936}}

{{42, 40, 4, 36, 45, 35, 78, 1000, 423, 85, 934, 84, 59, 33, 62, 53, 
  33, 81, 65, 33, 69, 69, 53, 55, 33, 82, 81, 76, 2076, 83, 74, 86, 
  78, 43, 66, 60, 68, 92, 1365, 86, 88, 43, 78, 77, 67, 45, 33, 102, 
  56, 55}}

WeightedData: The argument {{0.000452694, 0.0118147, 0.0110803, 0.000143083, 0.0110803, 0.0126247, 0.000355999, 0.0110803, 0.0164366, 0.011562, <<30>>, 0.0216263, 0.000487308, 0.00033544, 0.000556248, 0.000342936, 0.000865052, 0.000318878, 1.91834*10^-6, 0.000342936, 0.000342936}} is not a valid weight specification.

WeightedData[{{42, 40, 4, 36, 45, 35, 78, 1000, 423, 85, 934, 84, 59, 
   33, 62, 53, 33, 81, 65, 33, 69, 69, 53, 55, 33, 82, 81, 76, 2076, 
   83, 74, 86, 78, 43, 66, 60, 68, 92, 1365, 86, 88, 43, 78, 77, 67, 
   45, 33, 102, 56, 55}}, {{0.000452694, 0.0118147, 0.0110803, 
   0.000143083, 0.0110803, 0.0126247, 0.000355999, 0.0110803, 
   0.0164366, 0.011562, 1.08733*10^-6, 0.0118147, 0.0164366, 
   0.0168663, 0.0160231, 0.000168663, 0.0118147, 0.0123457, 0.0152416,
    0.0135208, 0.0138408, 0.000548459, 0.0168663, 0.0177778, 
   0.0141723, 0.0123457, 0.0123457, 0.0126247, 0.011562, 0.011562, 
   0.0126247, 0.0126247, 0.0118147, 0.0168663, 0.0145159, 0.0164366, 
   0.0145159, 0.01, 0.0110803, 0.000222767, 0.0216263, 0.000487308, 
   0.00033544, 0.000556248, 0.000342936, 0.000865052, 0.000318878, 
   1.91834*10^-6, 0.000342936, 0.000342936}}]
$\endgroup$
  • $\begingroup$ the way the last code segment is presented (the WeightedData part) seems to have an extra set of curly braces around both the data and weights; if you remove these extra braces, WeightedData executes normally $\endgroup$ – user42582 Feb 3 '18 at 13:32
1
$\begingroup$

You have extra braces in your expressions for data1 and w. The following revision of your code works.

data = {{42, 47}, {40, 9.2}, {4, 9.5}};
data2 = data[[All, 1]] // N;
w = 1/(data[[All, 2]]^2) // N;
WeightedData[data2, w]

result

Update

Some advice. Your code can be made simpler and will run faster if you avoid evaluating forms like data[[All, 1]] multiple times. Instead, write code like this:

data = {{42, 47}, {40, 9.2}, {4, 9.5}, {36, 83.6}, {45, 9.5}, {35, 8.9}};
{data1, data2} = Transpose[data // N];
Mean[data1]

33.6667

StandardDeviation[data1]

15.0022

w = 1/(data2^2);
\[ScriptCapitalA] = WeightedData[data1, w]

result

$\endgroup$
  • $\begingroup$ Thank you m_goldberg. That fixed my error. $\endgroup$ – Amy Feb 6 '18 at 18:59
  • $\begingroup$ @Amy. I'm glad you find my answer useful. Please consider accepting it. You can do that by clicking on the check mark that appears on the left of the answer below the down arrow. $\endgroup$ – m_goldberg Feb 6 '18 at 22:32
  • $\begingroup$ Absolutely. Thx again! I’m just learning Mathmatica and am grateful for the assistance $\endgroup$ – Amy Feb 7 '18 at 19:48

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