Message Boards Message Boards

repeated sampling from a list

Posted 10 years ago

Hi, I have a list of 250 numbers in "data" which I imported from an Excel file.
I can use RandomSample[data, 7] to get samples from this "data".

How can I sample my "data" x times, calculate the mean each time and create a dot plot of the means?

I tried For[i=1, i<100, i++, samplemeanlist[[i]]=Mean[RandomSample[data,7]]]

But I get an Noval: value not immediately available error.

I do get a list of 100 means when I do this: For[i=1, i<100, i++, Print[Mean[RandomSample[data,7]]]]

Now, how can I store these values and then make a dot plot? (or Histogram?) Thank you!

POSTED BY: Philip Cooper
8 Replies

Not sure if its the optimum method but ... f[n_] := Table[Mean[RandomSample[data, 7]], {n}];

ListPlot[f[100]]

Posted 10 years ago

I am extremely grateful, thank you! I changed ListPlot to Histogram. That's what I wanted. And your code looks pretty darn optimum to me! Thanks x 1e6!

Attachments:
POSTED BY: Philip Cooper

Slightly more efficient, I suspect:

f[n_]:=Mean /@ Partition[RandomChoice[data, 7 n], 7]

Also note (!) the distinction that this needs to be RandomChoice rather than RandomSample. RandomChoice allows random selection from the list of data without using any of the data more than once. RandomSample allows for choosing a random datum more than once. I assume that this is actually what you want.

If you actually want RandomSample then the original approach posted above by Melvyn would be better, but the lenght of data must be greater than or equal to 7.

POSTED BY: David Reiss

See my comment below about the difference between RandomSample and RandomChoice... it's an important distinction that you need to make to be sure you are calculating what you intend.

POSTED BY: David Reiss
Posted 10 years ago

Wow! Thank you! Cool code! I think you accidentally wrote it backwards? RandomChoice - samples "with replacement", meaning you can draw the same list item more than once in a sample, whereas RandomSample will not (let you). But for (my) statistical purposes, either is fine as the sample is less than 5% of the population (the data set,) and the data set is greater than 30 items.

POSTED BY: Philip Cooper

Right you are! I think the coffee hadn't kicked in yet. But the code is right ;-) (I an often code in Mathematica before I can speak sensibly....)

POSTED BY: David Reiss

Even more efficient would be to use:

f[n_] := Mean /@ RandomChoice[data, {n , 7}]
POSTED BY: Sander Huisman
Posted 10 years ago

Thank you!

POSTED BY: Philip Cooper
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract