cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
pulong
Level I

create formula to generate columns of design matrix

I am a beginner to use JMP, and cannot find similar answers to my questions in JMP community. Suppose that I have a data table with several columns, and the user needs to select the column from the data table and then a design matrix will be generated according the selected column. The selected column and the design matrix will be created in a new data table. But this is not enough. If the user want to change the value in the selected column in the new data table, then the design matrix will be updated accordingly. My problems is that I cannot update the design matrix correctly with the code below:

// create a window to select column

// omit some code to create the window

CheckAndConvert = Expr(

selectedColumns = c << Get Items;

// If inputs are valid, we continue

If(N Items(selectedColumns) > 0,

win << Close Window;

A = Column(selectedColumns);

s = A << GetAsMatrix;   // extract the select column in s  

// create the new data table "Design Matrix"

dt = New Table("Design Matrix");

dt << New Column("slice", Numeric, Values(s[0, 1]));  // copy s to the column "slice" in the new data table

// create corresponding columns of design matrix and adding them into the new data table

For( i=1, i<=Col Max(dt:slice), i++,

  dt << New Column("X"||Char(i), Numeric,

  After Last,

  Formula(

  If(dt:slice == i, 1,

  0

  )

  )

  );

);

);

);

PS:  I can get the correct answer when run the code without change the value in the slice column, but I cannot update X columns if I change the value of slice. Note that the number of X columns is the maximum value in the slice column, which is not fixed with different values. In following pictures, I show the examples to select column 1 and column 2 in the test data.

Thanks in advance,

Pulong

11 REPLIES 11

Re: create formula to generate columns of design matrix

Please see Help > Scripting Index. Change the browser to show Functions. Then select the Matrix group of functions. You will find here several functions to make the model matrix (for terms) from the design matrix (factor levels).

Re: create formula to generate columns of design matrix

My apologies. I read your request again and realized that none of the JSL functions will help in this case. They are intended to create the columns for the effects of categorical factors for the linear regression.

 

Here is a script that will take the matrix with the factor levels and create the matrix with a column for each term in a quadratic polynomial:

 

Names Default to Here( 1 );

x2fx = Function( { x }, { n factors, n treat, fx, f, f1, f2 },
	n factors = N Col( x );
	n treat = N Row( x );
	fx = J( n treat, 1, 1 );
	For( f = 1, f <= n factors, f++,
		fx ||= x[0,f];
	);
	For( f1 = 1, f1 < n factors, f1++,
		For( f2 = f1+1, f2 <= n factors, f2++,
			fx ||= x[0,f1] :* x[0,f2];
		);
	);
	For( f = 1, f <= n factors, f++,
		fx ||= x[0,f]^2;
	);
	fx;
);

X = [1 10,
     2 20,
     3 10,
     4 20,
     5 15,
     6 15];

D = x2fx( X );

Show( D );

It is not that hard to make your own functions.