0

I am learning JavaScript and trying to apply it in statistical sampling. The function below does not work but assumes:

  • array = population to study
  • sample = sample drawn from population
  • element = each observation from population
const generateSample = array => {
    var sample = []
    array.filter(element => {
            (element in sample) ? return null : sample = [...sample, element]
    })
}

  • Have you tried it with any data? – Anurag Srivastava Apr 04 '20 at 18:01
  • No. The function does not work. I am trying to use the filter method as it seems less verbose and descriptive. I also realized i need to add sample size to the equation –  Apr 04 '20 at 18:06
  • Why you want to use `filter` for such cases, use `forEach` or any loop, filter should be used without any side-effects – Code Maniac Apr 04 '20 at 18:07
  • I'm unsure of what you're trying to do here. [filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) takes in a predicate function that's run over every element in the array. It returns an array made of each element for which the predicate results in `true`. If you're trying to achieve something similar to Python's slice notation, look at [slice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice). – OSH Apr 04 '20 at 18:08
  • If array = [1,2,3,4,5] and size = 2, the result could be [3,1] or [2,4], and not [2,2]. I thought filter checks a condition of whether the randomly selected element is in the sample –  Apr 04 '20 at 18:12
  • but `filter` can't be stopped at size2 – TheMaster Apr 04 '20 at 18:30
  • Does this answer your question? [Is it correct to use JavaScript Array.sort() method for shuffling?](https://stackoverflow.com/questions/962802/is-it-correct-to-use-javascript-array-sort-method-for-shuffling) – TheMaster Apr 04 '20 at 18:32
  • Yes. I tested Array.sort() but it seems to mutate the original array. I should probably read on Fisher-Yates shuffle and try to write it in js. –  Apr 04 '20 at 18:41

2 Answers2

1

After doing some research, this is what i found when retrieving a sample of size N from an array

  1. Shuffle the array
  2. Slice the shuffled array to get sample of size N

NOTE: The shuffleArray function was created with the help of this question and answer

Below are the two functions that solved the problem

// Function that shuffles an array
const shuffleArray = array => {
  if (array.length === 1) return array

  const randomIndex = Math.floor(Math.random() * array.length);
  const randomElement = array[randomIndex]

  array = array.filter((_, i) => i !== randomIndex)

  return [randomElement, ...shuffleArray(array)];
};

// Function that returns sub array from array
const subArray = (array, n) => array.slice(0, n)

const population = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const shuffled = shuffleArray(population)
console.log( shuffled )
console.log(subArray(shuffled, 4))
0

Array.filter expects a function that returns true or false (whether to keep the element in the array). The function you give to filter returns null... or doesn't return at all. (element in array) will check if array.length >= element as the in operator checks if a property is in an object or its prototype chain. To check if an element is in an array use .includes() or .indexOf() >= 0. Also the ternary operator condition ? expr : expr expects expressions, not statements like return null or sample =

Maybe something like?

let data = [1, 2, 3, 4, 5];

let sample = data.filter(() => Math.round(Math.random()));

console.log(sample);
domondo
  • 926
  • 7
  • 9
  • Ok. I edited my code and meant (element in sample) to check if randomly selected element is in sample array –  Apr 04 '20 at 18:56