如何在Profiler中為仿真指定相關矩陣?你好,
我正在編寫一個腳本,以從一組樣本數據中創建具有指定行數,均值/標準差向量和相關結構的多元正態數據。 我想利用Profiler平台中的Simulator通過以下步驟生成數據表:
- 計算每個變量的樣本均值和標準差。
- 從樣本數據生成相關矩陣。
- 使用(1)和(2)中的匯總統計量模擬多元數據。
當前,我的腳本為每個變量生成了具有適當均值和標準偏差的模擬數據表,但是我無法正確維護變量之間的相關結構。 我認為我的問題的原因是我如何指定[ NxN相關] X相關使用變量而不是顯式指定相關矩陣(例如[1 0.3 0、0.3 1 0、0 0 1]),但是我的故障排除技能使我無解。 下面提供了當前腳本以及預期輸出與實際輸出,歡迎提供有關如何實現這些步驟的任何建議:
JSL:
names default to here(1);
Try( Close( Data Table(dt) ) );
If( Is Empty( dt ),
Try( dt = Open(), Throw( "No data found" ) )
);
ncol = NCol(dt);
//Dialog Window.
Dlg = New Window("New Window",
<< Modal,
BorderBox(left(3), top(2),
V List Box(
TextBox("Simulate multivariate data"),
HListBox(
VListBox(
PanelBox("Select variables",
colListData = ColListBox(all,nLines,(min(ncol,10)))
),
),
PanelBox("Cast Selected Variables",
LineupBox(NCol(2), Spacing(3),
ButtonBox("Variables", colListY << Append(colListData << Get Selected)),
colListY = ColListBox(nLines(6), "numeric"))
),
//specifications - not in use.
PanelBox("Enter Specifications",
Spacer Box(Size(10, 1)),
HListBox(
VListBox(
Spacer Box(Size(5, 10)),
lsl1 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
lsl2 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
lsl3 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
lsl4 = Number Edit Box(, 3)
),
SpacerBox(Size(10,10)),
VListBox(
Spacer Box(Size(5, 10)),
usl1 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
usl2 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
usl3 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
usl4 = Number Edit Box(, 3)
)
),
),
PanelBox("Action",
LineupBox(NCol(1),
// OK button
Button Box("OK",
lsl_mat = J(4,1, .);
lsl_mat[1] = Eval(lsl1 << Get);
lsl_mat[2] = Eval(lsl2 << Get);
lsl_mat[3] = Eval(lsl3 << Get);
lsl_mat[4] = Eval(lsl4 << Get);
usl_mat = J(4,1, .);
usl_mat[1] = Eval(usl1 << Get);
usl_mat[2] = Eval(usl2 << Get);
usl_mat[3] = Eval(usl3 << Get);
usl_mat[4] = Eval(usl4 << Get);
// Store summary statistics
tpListY = colListY << Get Items;
tpMean = J(4, 1, 0);
tpSD = J(4, 1, 0);
for (i=1, i <= nitems(tpListY), i++,
one_col = tpListY[i];
Meani = Col Mean(Column(dt, one_col));
SDi = Col Std Dev(Column(dt, one_col));
tpMean[i] = Meani;
tpSD[i] = SDi;
);
Dlg << CloseWindow
),
// Cancel button
Button Box("Cancel", Dlg << CloseWindow),
Spacer Box(Size(10,10)),
// Remove button
Button Box("Remove", collistY << RemoveSelected)
)
),
)
)
)
);
//correlation matrix
corr = multivariate(
Y(Eval List(tpListY)),
estimation method("Row-wise"),
matrix format("Square"),
scatterplot matrix(0)
);
corr.run = corr << run;
corr.rep = corr.run << report;
corr.mat = corr.rep["Correlations"][matrix box(1)] << get(1);
//only used to launch profiler
prof.x = J(2,4 ,1);
prof.tbl = as table(prof.x, << column names(tpListY), << invisible);
prof.tbl << new column("Y",
Formula( :TP1 + :TP2 + :TP3 + :TP4)
);
prof.tbl = current data table();
random reset(1234);
//specify multivariate data from summary statistics
sim.design = Profiler(
Y(:Y),
Invisible
);
sim.design << Simulator(
1,
Factors(
TP1 << multivariate( tpMean[1], tpSD[1]),
TP2 << multivariate( tpMean[2], tpSD[2]),
TP3 << multivariate( tpMean[3], tpSD[3]),
TP4 << multivariate( tpMean[4], tpSD[4]),
),
X Correlations(
1,
tpListY,
corr.mat
),
N Runs(10000)
);
//Make data table
report( sim.design )["Simulate to Table"][button box( 1 )] << click( 1 );
sim.dt = data table( 1 );
sim.dt << set name( "Simulated multivariate data" );
sim.dt << delete columns({"Y","Obj"});預期與實際產量:
預期的
數據表腳本編寫回复:如何在Profiler中為仿真指定相關矩陣?你好@eangello ,
這是一個有趣的問題,如果可以的話,我想嘗試並提供幫助,但我無法重現您的問題。 當我嘗試運行您的腳本時,它要求我從示例數據目錄中打開數據表。 這裡的一個問題是我不知道您正在使用哪個數據表,也不會遇到相同的問題。 您可以提供正在使用的數據表嗎?
如果確實從樣本數據中選擇了一個數據表,例如2D高斯過程example.jmp文件,則下一個對話框窗口要求將列轉換為角色,但是當我單擊OK時,我得到一個錯誤,即未解決name多餘的名稱或對'Button',Button(-1)/ * ### * /的評估。 然後,您的代碼中的第124行出現了另一個錯誤,該錯誤與sim.design << Simulator()send命令不一樣。
顯然,您有一個可以工作的數據表。 可以分享嗎? 還是提供有關使用哪個表生成帖子中顯示的輸出的更多詳細信息?
謝謝!,
DS
回复:如何在Profiler中為仿真指定相關矩陣?你好@DiedrichSchmidt ,
發布之前我沒有進行盡職調查-這是示例數據中的10行示例(4個X; TP1-TP4)。 當前腳本限制為4個變量,但我希望能夠將其擴展為ñ未來的變數。 此示例數據表將重現我的問題。 我相信未解決的名稱錯誤是來自對話框(也許是取消/刪除按鈕),但是我沒有找到根本原因。
謝謝,
EA
回复:如何在Profiler中為仿真指定相關矩陣?你好@eangello ,
謝謝你的數據表。 正如您提到的,我可以重提您的問題。 我注意到的一件事是,當您運行模擬器時,它不會在探查器旁邊創建直方圖,這與X Correlations的幫助索引中的Tiretread示例不同。
我認為發生了兩件事:
1.調用tpListY
2.調用corr.mat
我不確定為什麼,但是我認為您需要在代碼中明確“拼寫”出X關聯的列表,其中包括相關矩陣。因此,除了調用tpListY之外,您需要將其替換為{TP1,TP2 ,TP3,TP4}。 同樣,您需要在方括號[]之間放置實際的4x4(或NxN)相關矩陣。
我以為執行Parse(tpListY [1])之類的方法是可以的,但是即使包裹了Eval(),我也無法使其正常工作。 如果我將JSL代碼放入{TP1,TP2,TP3,TP4}中,我只能使其自動工作。 請注意,當您放入NxN相關矩陣時,它實際上是使用矩陣的左下三角形填充表格。
我還認為調用corr.mat的元素可能會起作用,就像在corr.mat [1]等中一樣,但這也不起作用。 如果嘗試這樣做,則會收到無效的矩陣令牌錯誤,並且不確定原因,因為corr.mat的元素是數字。
下面的代碼(我修改了功能以對其進行一些修改)可以提供預期的輸出,但是這並不能滿足您從4x4系統擴展到NxN系統的需求。
names default to here(1);
Try( Close( Data Table(dt) ) );
If( Is Empty( dt ),
Try( dt = Open(), Throw( "No data found" ) )
);
ncol = NCol(dt);
//Dialog Window.
Dlg = New Window("New Window",
<< Modal,
BorderBox(left(3), top(2),
V List Box(
TextBox("Simulate multivariate data"),
HListBox(
VListBox(
PanelBox("Select variables",
colListData = ColListBox(all,nLines,(min(ncol,10)))
),
),
PanelBox("Cast Selected Variables",
LineupBox(NCol(2), Spacing(3),
ButtonBox("Variables", colListY << Append(colListData << Get Selected)),
colListY = ColListBox(nLines(6), "numeric"))
),
//specifications - not in use.
PanelBox("Enter Specifications",
Spacer Box(Size(10, 1)),
HListBox(
VListBox(
Spacer Box(Size(5, 10)),
lsl1 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
lsl2 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
lsl3 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
lsl4 = Number Edit Box(, 3)
),
SpacerBox(Size(10,10)),
VListBox(
Spacer Box(Size(5, 10)),
usl1 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
usl2 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
usl3 = Number Edit Box(, 3),
Spacer Box(Size(5, 10)),
usl4 = Number Edit Box(, 3)
)
),
),
PanelBox("Action",
LineupBox(NCol(1),
// OK button
Button Box("OK",
lsl_mat = J(4,1, .);
lsl_mat[1] = Eval(lsl1 << Get);
lsl_mat[2] = Eval(lsl2 << Get);
lsl_mat[3] = Eval(lsl3 << Get);
lsl_mat[4] = Eval(lsl4 << Get);
usl_mat = J(4,1, .);
usl_mat[1] = Eval(usl1 << Get);
usl_mat[2] = Eval(usl2 << Get);
usl_mat[3] = Eval(usl3 << Get);
usl_mat[4] = Eval(usl4 << Get);
// Store summary statistics
tpListY = colListY << Get Items;
tpMean = J(4, 1, 0);
tpSD = J(4, 1, 0);
for (i=1, i <= nitems(tpListY), i++,
one_col = tpListY[i];
Meani = Col Mean(Column(dt, one_col));
SDi = Col Std Dev(Column(dt, one_col));
tpMean[i] = Meani;
tpSD[i] = SDi;
);
Dlg << CloseWindow
),
// Cancel button
Button Box("Cancel", Dlg << CloseWindow),
Spacer Box(Size(10,10)),
// Remove button
Button Box("Remove", collistY << RemoveSelected)
)
),
)
)
)
);
//correlation matrix
corr = multivariate(
Y(Eval List(tpListY)),
estimation method("Row-wise"),
matrix format("Square"),
scatterplot matrix(0)
);
corr.run = corr << run;
corr.rep = corr.run << report;
corr.mat = corr.rep["Correlations"][matrix box(1)] << get(1);
//only used to launch profiler
prof.x = J(200,4 ,1);
prof.tbl = as table(prof.x, << column names(tpListY), << invisible);
prof.tbl << new column("Y",
Formula( :TP1+:TP2^2-:TP3+:TP4*Random Normal(0,2))
);
prof.tbl = current data table();
random reset(1234);
//specify multivariate data from summary statistics
sim.design = Profiler( Y( :Y ), Invisible );
sim.design << Simulator(
1,
Factors(
TP1 << multivariate( tpMean[1], tpSD[1] ), TP2 << multivariate( tpMean[2], tpSD[2] ),
TP3 << multivariate( tpMean[3], tpSD[3] ), TP4 << multivariate( tpMean[4], tpSD[4] ),
),
Responses( Y << No Noise ),
Automatic Histogram Update( 1 ),
X Correlations( 1, {TP1, TP2, TP3, TP4},
[1 0.6401843997 0.670478399654806 0.6405126152, 0.6401843997 1 0.9443055859 0.8815992957, 0.6704783997 0.9443055859 1 0.9355872238, 0.6405126152 0.8815992957 0.9355872238 1]
),
N Runs( 10000 ),
Simulate
);
//Make data table
report( sim.design )["Simulate to Table"][button box( 1 )] << click( 1 );
sim.dt = data table( 1 );
sim.dt << set name( "Simulated multivariate data" );
sim.dt << delete columns({"Y","Obj"});必須有一種方法可以執行此操作,因為您可以調用tpMean和tpSD的元素來填充因子的多元噪聲。 我認為可能需要對JSL有更多經驗的人對此發表評論。
最後,我認為您對其中一個按鈕框是正確的,但我也無法確切指出它-更加專注於嘗試解決tpListY和corr.mat問題!;)
我對您所發現的內容非常感興趣。
祝你好運,
DS
回复:如何在Profiler中為仿真指定相關矩陣?你好@DiedrichSchmidt ,
謝謝您對故障排除的幫助,不勝感激。 我玩過類似的功能,例如Eval,Eval Expr(Expr(x) ) ,和纈草胺並通過在發送到Profiler之前強制對列和矩陣變量進行求值來找到解決方案( X相關似乎接受)。 我採用了與上述相同的解決方案,以使Profiler可以靈活地適應不同的列名。 現在,我的相關結構保留在模擬數據中。
new.design = evalinsert(
"\[sim.design << Simulator(
1,
Factors(
Name( "^col1^" ) << multivariate( tpMean[1], tpSD[1]),
Name( "^col2^" ) << multivariate( tpMean[2], tpSD[2]),
Name( "^col3^" ) << multivariate( tpMean[3], tpSD[3]),
Name( "^col4^" ) << multivariate( tpMean[4], tpSD[4]),
),
X Correlations(
1,
^tpListY^,
^corr.mat^
),
N Runs(10000)) ]\");
eval(parse(new.design));
再次感謝,
EA
回复:如何在Profiler中為仿真指定相關矩陣?你好@eangello ,
很好的解決方案,我希望我會記得那樣做。 在編寫腳本以自動進行某些模型調整時,我必須做類似的事情。 EvalInsert()和轉義字符(用於引號)和^^(用於插入值)非常有用。 我只是不經常使用它,而忘記了這種特定的做事方式。
出於好奇,為什麼您需要生成具有指定相關結構的模擬數據。 我很感興趣,因為它給了我一些如何將其整合到我正在研究的東西中的想法。
謝謝!,
DS
回复:如何在Profiler中為仿真指定相關矩陣?你好@DiedrichSchmidt ,
我經常出於建模目的(通常是在其中進行模型調整)來模擬多變量數據集,通常基於一些歷史參數。 該腳本只是提供此模擬數據的簡單解決方案。 我目前沒有特定的用例,但我打算將此腳本縮放為n維,並有可能將其用作功能模塊我可以將其合併到使用大樣本技術的更廣泛的腳本中。 給定將均值/ sd向量和相關矩陣發送到Profiler平台的約束,我可能會求助於顯式編寫線性運算,以便將模擬縮放到n維。
問候,
恩格洛
回复:如何在Profiler中為仿真指定相關矩陣?你好@eangello ,
當我意識到您想要對相關結構進行操作時,這正是我的想法。 我想藉用您的想法並將其修改為我面臨的一些特定問題。 我在另一個線程中發布了關於歸納為N個輸入的信息。 我認為我已經很近了,但還不是很遠。 在調用X Correlations()並設置因子的均值和標準差值時,我很難按比例放大它。
隨附的是我正在使用的模型數據表和腳本,全部基於您的原始腳本。 也許您可以找到我正在犯的錯誤? 正如您所指出的那樣,我很樂意看到此方法適用於任意數量的列,這對歷史數據非常有幫助。
謝謝!,
DS
回复:如何在Profiler中為仿真指定相關矩陣?你好@eangello ,
我認為我已經能夠設計出可擴展到N列的腳本。 我給@小姐大量的信用額度,用於幫助概括Profiler的相關部分。 該線程是這裡。
有兩個腳本,一個獨立的版本(類似於您實現的腳本),但是沒有您在原始文章中擁有的額外變量,還有一個可以從數據表本身運行的腳本。 獨立服務器在下面,而要從數據表運行的服務器在Gendt.jmp數據表中。 它既適用於具有4列的數據表,也適用於我具有11列的其他數據表。 我還嘗試對帶有其他列類型的數據表的獨立腳本進行泛化。
Names Default To Here( 1 );
// Expression to clear all current settings
clearRoles = Expr( ColListY << RemoveAll );
// Expression to store the current settings in global variables
recallRoles = Expr(
::ycolRecall = ColListY << GetItems
);
//get data table
Try( Close( Data Table( dt ) ) );
If( Is Empty( dt ),
Try( dt = Open(), Throw( "No data found" ) )
);
//Find the number of columns in the data table
ncol = N Col( dt );
ColList = dt << Get Column Names( Numeric, Continuous );
//Dialog Window.
Dlg = New Window( "New Window",
<<RETURN result=""></RETURN>
另外,我認為您收到的按鈕框錯誤與對話窗口的<< Modal調用有關。 我將其替換為<<返回結果,並且不再收到錯誤。 我還包括了“調用”按鈕,但您不必一定要使用它。
我希望這會有所幫助,這真的很棒,並且我從中學到了很多。 我知道我一定可以在工作中使用它!
謝謝!,
DS
回复:如何在Profiler中為仿真指定相關矩陣?你好@DiedrichSchmidt ,
我從來沒有想到過這種方法,做得很好。 至於對話窗口,我認為您是對的,我必須仔細看一下。 事實證明,避免同時使用Profiler平台並不是件容易的事,儘管這是一個不錯的主意,但對我而言並不成功。 這是相同的結果,但大驚小怪(類似於我在R中實現的步驟),並且僅作為輸入需要均值向量和協方差矩陣。
//tpListY is variables.<BR /><BR />cov = multivariate(
Y(eval list(tpListY)),
estimation method("Row-wise"),
matrix format("Lower Triangular"),
scatterplot matrix(1)
);
cov << covariance matrix(1);
cov.run = cov << run;
cov.rep = cov.run << report;
cov.mat = cov.rep["Covariance Matrix"][matrix box(1)] << get(1);
random reset(1234);
//generate X = (X1, . . . , Xn) where X ∼ MNn(µ, Σ) using the Cholesky decomposition of Σ
//to find (nxn) matrix, C, such that C*C` = Σ.
//let Z = (Z1, . . . , Zn)` where the Zi’s are IID N(µ, 1) for i = 1, . . . , n.
//mean.M is mean vector.
sim.n = 1e4;
dummy.mat = j(1,sim.n,1);
mean.mat = mean.M*dummy.mat;
n.means = n row(mean.M);
sim.C = cholesky(cov.mat);
sim.Z = j(n.means, sim.n, random normal());
sim.X = sim.C*sim.Z;
sim.X2 = sim.X + mean.mat;
sim.Xt = transpose(sim.X2);
as table(sim.Xt, << column names(tpListY));
我更喜歡使用更少的代碼來完成相同的工作,因此它比Profiler路由更具吸引力。 希望這使得在其他地方實現起來更容易。
再次感謝,
恩格洛