cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
See how to use to use Text Explorer to glean valuable information from text data at April 25 webinar.
Choose Language Hide Translation Bar
View Original Published Thread

How to sample a column?

rugolf
Level I

In a first column I simulate a random variable to create a population (for example, 10000 lines with a normal distribution).

Then, I would like, in another column, to obtain a random sample of size n to be fixed and extracted from the population created in the previous column. The best would be to obtain several samples simultaneously in juxtaposed columns.


FYI, I've only been discovering JMP for a few weeks (I've been working with Minitab for years), I haven't found the solution on my own, except for sampling when creating a graph.


Thank you in advance.

This post originally written in French and has been translated for your convenience. When you reply, it will also be translated back to French .

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User


Re: Comment échantillonner une colonne ?

 

There are multiple ways to do this.  

The simplest is to use the built in capability of the Subset platform 

     Tables=>Subset

Given a table with a column of data with 1000 rows

txnelson_0-1740184285276.png

The Tables=>Subset allow you to create a random sample of either a given percentage or a given sample size

txnelson_1-1740184470825.png

Which will give you a new table with 100 rows

txnelson_2-1740184553796.png

it is then a simple matter to join this table with the original table using 

     Tables=>Join

txnelson_4-1740184746639.png

Which will give you what you asked for

txnelson_6-1740184920107.png

The main problem with this approach is that it is somewhat violating the assumptions of a JMP data table. 

Another approach would be to use a column formula.  The formula I came up with produces a random sample, but rather than placing the selected data into the first 100 rows, it places the values in the row the selected data comes from.

Here is the formula

As Constant(
	rowCount = N Rows( Current Data Table() );
	sampleMatrix = [];
	For( i = 1, i <= 100, i++,
		found = 0;
		While( found == 0,
			row = Random Integer( 1, rowCount );
			If( N Rows( Loc( sampleMatrix, row ) ) == 0,
				found = 1;
				sampleMatrix = sampleMatrix || Matrix( row );
			);
		);
	);
);
If( N Cols( Loc( sampleMatrix, Row() ) ) != 0,
	:Column 1,
	.
);

Which produces

txnelson_0-1740189189597.png

Now if one moves the sample generation to JSL, it becomes a fairly easy task to create as many samples as needed

Names Default To Here( 1 );
dt = Current Data Table();
rowCount = N Rows( dt );
	
// Create 5 Random Samples of 100
For( k = 1, k <= 5, k++,
	dt << New Column( "Random" || Char( k ) );
	sampleMatrix = [];
	For( i = 1, i <= 100, i++,
		found = 0;
		While( found == 0,
			row = Random Integer( 1, rowCount );
			If( N Rows( Loc( sampleMatrix, row ) ) == 0,
				found = 1;
				sampleMatrix = sampleMatrix || Matrix( row );
			);
		);
	);

// Write the values to the new column
	For Each( {row}, sampleMatrix,
		Column( "Random" || Char( k ) )[row] = :Column 1[row]
	);
);

Which creates

txnelson_1-1740190043651.png

With a little modification, the random sample generation can be sampling without replacement

Names Default To Here( 1 );
dt = Current Data Table();
rowCount = N Rows( dt );
masterSampleMatrix = [];
	
// Create 5 Random Samples of 100
For( k = 1, k <= 5, k++,
	dt << New Column( "Random" || Char( k ) );
	sampleMatrix = [];
	For( i = 1, i <= 100, i++,
		found = 0;
		While( found == 0,
			row = Random Integer( 1, rowCount );
			If( N Rows( Loc( masterSampleMatrix, row ) ) == 0,
				found = 1;
				sampleMatrix = sampleMatrix || Matrix( row );
				masterSampleMatrix = masterSampleMatrix || Matrix( row );
			);
		);
	);

// Write the values to the new column
	For Each( {row}, sampleMatrix,
		Column( "Random" || Char( k ) )[row] = :Column 1[row]
	);
);

txnelson_2-1740190597506.png

 

Jim

View solution in original post

2 REPLIES 2
txnelson
Super User


Re: Comment échantillonner une colonne ?

 

There are multiple ways to do this.  

The simplest is to use the built in capability of the Subset platform 

     Tables=>Subset

Given a table with a column of data with 1000 rows

txnelson_0-1740184285276.png

The Tables=>Subset allow you to create a random sample of either a given percentage or a given sample size

txnelson_1-1740184470825.png

Which will give you a new table with 100 rows

txnelson_2-1740184553796.png

it is then a simple matter to join this table with the original table using 

     Tables=>Join

txnelson_4-1740184746639.png

Which will give you what you asked for

txnelson_6-1740184920107.png

The main problem with this approach is that it is somewhat violating the assumptions of a JMP data table. 

Another approach would be to use a column formula.  The formula I came up with produces a random sample, but rather than placing the selected data into the first 100 rows, it places the values in the row the selected data comes from.

Here is the formula

As Constant(
	rowCount = N Rows( Current Data Table() );
	sampleMatrix = [];
	For( i = 1, i <= 100, i++,
		found = 0;
		While( found == 0,
			row = Random Integer( 1, rowCount );
			If( N Rows( Loc( sampleMatrix, row ) ) == 0,
				found = 1;
				sampleMatrix = sampleMatrix || Matrix( row );
			);
		);
	);
);
If( N Cols( Loc( sampleMatrix, Row() ) ) != 0,
	:Column 1,
	.
);

Which produces

txnelson_0-1740189189597.png

Now if one moves the sample generation to JSL, it becomes a fairly easy task to create as many samples as needed

Names Default To Here( 1 );
dt = Current Data Table();
rowCount = N Rows( dt );
	
// Create 5 Random Samples of 100
For( k = 1, k <= 5, k++,
	dt << New Column( "Random" || Char( k ) );
	sampleMatrix = [];
	For( i = 1, i <= 100, i++,
		found = 0;
		While( found == 0,
			row = Random Integer( 1, rowCount );
			If( N Rows( Loc( sampleMatrix, row ) ) == 0,
				found = 1;
				sampleMatrix = sampleMatrix || Matrix( row );
			);
		);
	);

// Write the values to the new column
	For Each( {row}, sampleMatrix,
		Column( "Random" || Char( k ) )[row] = :Column 1[row]
	);
);

Which creates

txnelson_1-1740190043651.png

With a little modification, the random sample generation can be sampling without replacement

Names Default To Here( 1 );
dt = Current Data Table();
rowCount = N Rows( dt );
masterSampleMatrix = [];
	
// Create 5 Random Samples of 100
For( k = 1, k <= 5, k++,
	dt << New Column( "Random" || Char( k ) );
	sampleMatrix = [];
	For( i = 1, i <= 100, i++,
		found = 0;
		While( found == 0,
			row = Random Integer( 1, rowCount );
			If( N Rows( Loc( masterSampleMatrix, row ) ) == 0,
				found = 1;
				sampleMatrix = sampleMatrix || Matrix( row );
				masterSampleMatrix = masterSampleMatrix || Matrix( row );
			);
		);
	);

// Write the values to the new column
	For Each( {row}, sampleMatrix,
		Column( "Random" || Char( k ) )[row] = :Column 1[row]
	);
);

txnelson_2-1740190597506.png

 

Jim
rugolf
Level I

Re: How to sample a column?

Thank you so much.

JL

This post originally written in French and has been translated for your convenience. When you reply, it will also be translated back to French .