Am I doing this math correctly?

Am I doing this math correctly?
0.0 0

#1

I’m working on my scatter plot project, and the data set I’m using weights each data point. They give a formula for calculating an estimate, but the math they use is new to me. After some research, I think I’ve figured it out. I’m just looking for somebody who’s smarter than I am to verify that I’m doing this correctly. Here are the instructions from the data set:

And here is a small subset of the data that I’m working with. It represents the number of minutes spent playing games for each respondent, along with the weight. If I understand the instructions correctly:
fwgt(i) = data.weight
T(ij) = data.minutes

[ { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 570, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 60, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 135, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 90, weight: 4734291 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 4381314 },
  { year: 2014, age: 18, sex: 'M', minutes: 90, weight: 7970627 },
  { year: 2014, age: 18, sex: 'M', minutes: 329, weight: 5999389 },
  { year: 2014, age: 18, sex: 'M', minutes: 50, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 2 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 6769183 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 9722641 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 2 },
  { year: 2014, age: 18, sex: 'M', minutes: 395, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 60, weight: 2 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 9145549 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 3 },
  { year: 2014, age: 18, sex: 'M', minutes: 118, weight: 4466045 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 7149976 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 6195287 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 2 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 2 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 9759886 },
  { year: 2014, age: 18, sex: 'M', minutes: 660, weight: 7210502 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 2 },
  { year: 2014, age: 18, sex: 'M', minutes: 180, weight: 4769934 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 4255097 },
  { year: 2014, age: 18, sex: 'M', minutes: 180, weight: 7844106 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 3 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 4051823 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 6980967 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 2 },
  { year: 2014, age: 18, sex: 'M', minutes: 195, weight: 5948595 },
  { year: 2014, age: 18, sex: 'M', minutes: 60, weight: 3421269 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 6932904 },
  { year: 2014, age: 18, sex: 'M', minutes: 60, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 149, weight: 6187478 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 9243905 },
  { year: 2014, age: 18, sex: 'M', minutes: 33, weight: 5706961 },
  { year: 2014, age: 18, sex: 'M', minutes: 45, weight: 4923626 },
  { year: 2014, age: 18, sex: 'M', minutes: 60, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 6336646 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 1 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 2 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 9931656 },
  { year: 2014, age: 18, sex: 'M', minutes: 0, weight: 4547729 },
  { year: 2014, age: 18, sex: 'M', minutes: 270, weight: 1 } ]

Here is the code that I’m using to try to solve the expression that is included in the instructions.

var accMinutes = 0, 
    accWeight = 0;

for (var i = 0; i < data.length; i++) {
    accMinutes = accMinutes + (data[i].minutes * data[i].weight);
    accWeight = accWeight + data[i].weight;
}

var estMinutes = accMinutes / accWeight;
console.log("Estimate: " + estMinutes);

The estimate that I arrive at for the given subset of data is 76.58498157779648.

Somebody help me, am I doing this correctly? Thank you in advance!


#2
for (var i = 0; i < data.length; i++) {
    accMinutes = accMinutes + (data[i].minutes * data[i].weight);
    accWeight = accWeight + data[i].weight;
}

With this you assign new value to accMinutes and accWeight with each loop iteration, so in the end in your var estMinutes = accMinutes / accWeight; you’re using only the last value. If I remember my math lessons correctly, sigma means sum, so you should sum all the data and then divide to receive an average. You could use += instead of = to accumulate values into one sum.


#3

Thank you for taking a look at it! I was hoping that I was understanding the expression correctly after researching it. I only used the less terse version for better readability. a += b and a = a + b should result in the same value for a. :slight_smile:


#4

Ugh, right. I should sleep more :stuck_out_tongue:


#5

Am I missing some secret meaning of the word weight?
Cause I dont get how weight could influence the average time spend on something. The code you wrote is correct relating to the given formula. Excel gives the same result.

Average time for me means :
var accMinutes = 0, respondent =0;

for (var i = 0; i < data.length; i++) {
accMinutes += data[i].minutes
respondent += 1;
}

var estMinutes = accMinutes / respondent;
console.log("Estimate: " + estMinutes);


#6

@Redredion it is connected with statistics. I won’t try to explain it because I never studied statistics. I only have a vague understanding of how it works. Apparently, since each respondent was only asked questions about the previous day, their responses aren’t actually representative. For example, a person may spend more time playing games on the weekend than they would have on a weekday. Apparently, though the magic of statistics, those responses can be adjusted to be representative by “weighting” each response. Then, you take the data and the weight and do some math, and the responses of a small group of people can be made to be representative of a much larger group of people.

Thanks statistics!

Anyway, I don’t really understand it, I’m just trying to follow the instructions. :smiley: