cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
  • New JMP features coming to desktops everywhere this September. Sign up to learn more at jmp.com/launch.
Choose Language Hide Translation Bar
rugolf
Level I

Comment échantillonner une colonne ?

Dans une première colonne je simule une variable aléatoire pour créer une population (par exemple, 10000 lignes avec une loi normale).

Ensuite, j'aimerais, dans une autre colonne obtenir un échantillon aléatoire de taille n à fixer et extrait de la population créée dans la colonne précédente. Le top, serait d'obtenir plusieurs échantillons simultanément dans des colonnes juxtaposées.

 

Pour info, je découvre JMP que depuis quelques semaines (je travaille depuis des années avec Minitab), je n'ai pas trouvé la solution tout seul, si ce n'est un échantillonnage lors de la création d'un graphique.

 

Par avance, merci.

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: Comment échantillonner une colonne ?

Merci beaucoup.

JL

Recommended Articles