JMP > JSL > 多次调用拟合模型(N > 2000)的脚本加速?您好 JMP 社区,
我的数据集越来越大,而我不久前创建的基本脚本(见下文)从速度的角度来看并没有跟上。
我将非常感谢听到有关如何优化此类脚本以提高速度的想法。 例如,我怀疑像“连接”这样的操作相对较慢,使用矩阵来进行这种类型的操作可能是有意义的。
Names Default to Here (1);
dt = Current Data Table ();
CList = dt << Get Column Names (string);
dtoutl = New Table ("LSM OUTPUT", "invisible");
dtoutp = New Table ("PVal OUTPUT", "invisible");
For (i = 1, i<= 2000, i++,
fm = dt << Fit Model(
Y( Column (i) ),
Effects( :VISIT, :TREATMENT, :VISIT * :TREATMENT, Column (i+2000) ),
Personality( "Standard Least Squares" ),
Emphasis( "Minimal Report" ),
Run(
Column (i) << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
Parameter Estimates( 1 ), Scaled Estimates( 0 ),
Plot Actual by Predicted( 0 ), Plot Regression( 0 ),
Plot Residual by Predicted( 0 ), Plot Studentized Residuals( 0 ),
Plot Effect Leverage( 0 ), Plot Residual by Normal Quantiles( 0 ),
Box Cox Y Transformation( 0 ), {:VISIT * :TRT01A <<
{LSMeans Contrast( [0 0 1 -1 0 0, 0 0 0 0 1 -1] )}}}
)
);
fmr = report (fm);
table_list = fmr << xpath ("//OutlineBox [text() = 'VISIT*TREATMENT']//TableBox");
temp_lsm = table_list [1];
temp_pval = table_list [2];
temp_tbl_lsm = temp_lsm << Make into Data Table (invisible (1));
temp_tbl_pval = temp_pval << Make into Data Table (invisible (1));
temp_tbl_lsm << New column ("ID", character, nominal, << set each value (CList [i]));
temp_tbl_pval << New column ("ID", character, nominal, << set each value (CList [i]));
fmr << close window;
dtoutl << Concatenate (Data table (temp_tbl_lsm), "Append to First Table");
Wait (0);
dtoutp << Concatenate (Data table (temp_tbl_pval), "Append to First Table");
Close (temp_tbl_lsm, NoSave);
Close (temp_tbl_pval, NoSave);
);
dtoutl << Bring Window to Front;
dtoutp << Bring Window to Front;
欢迎所有反馈。
最好的,
TS
脚本回复:JMP > JSL > 多次调用拟合模型(N > 2000)的脚本加速?嗨,
使 Fit Model 不可见又减少了 8 秒(从 61 秒到 53 秒),用直接分配替换“For Each Row”又减少了 5 秒(53 秒到 48 秒)。
杰出的!
最好的,
TS
回复:JMP > JSL > 多次调用拟合模型(N > 2000)的脚本加速?这些可能会进一步(很可能相当小)提高速度:
您很可能会添加不可见以适合模型
fm = dt << Fit Model(
Y( Column (i) ),
Effects( :VISIT, :TRT01A, :VISIT * :TRT01A, Column (i-1933) ),
Personality( "Standard Least Squares" ),
Emphasis( "Minimal Report" ),
Run(
Column (i) << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
Parameter Estimates( 1 ), Scaled Estimates( 0 ),
Plot Actual by Predicted( 0 ), Plot Regression( 0 ),
Plot Residual by Predicted( 0 ), Plot Studentized Residuals( 0 ),
Plot Effect Leverage( 0 ), Plot Residual by Normal Quantiles( 0 ),
Box Cox Y Transformation( 0 ), {:VISIT * :TRT01A <<
{LSMeans Contrast( [0 0 1 -1 0 0, 0 0 0 0 1 -1] )}}}
),
invisible
);
此外,如果您使用公式创建列然后立即删除它们,您可以使用 << Set Each value 而不是 Formula 和 Delete Formula
dtoutl << New Column("METAB_ID", Character, Nominal, << Set Each Value(Substitute(:MET_ID, "LOG10 FC ", "")));
您也可以用 << Set Each Value 替换 For Each Row (不确定这一点,也不确定这是否会提高速度):
Column(dtoutp, "LABEL") << Set Each Value(annot_pv[Sequence( 1, N Items (annot_pv), 1, 1 )]);
您还可以尝试将数据表保密(至少不可见),直到您需要它们为止。 您可以使用 << New Data View 使私有表可见(今天之前不知道这个)。
在不需要它们后立即关闭数据表/报告/窗口。
回复:JMP > JSL > 多次调用拟合模型(N > 2000)的脚本加速?您好 JMP 社区,
在下面找到最改进的脚本的注释版本,以运行和捕获大量拟合模型分析的输出。
提高运行时间的两个最重要的步骤是:
- 使用矩阵而不是数据表来聚合分析输出
- 替换而不是将分析输出数据插入到在主循环之前创建的主矩阵中。
Names Default to Here (1);
st = Today();
dt = Current Data Table ();
lc_path = dt << GetPath;
// START: CAPTURING OUTPUT ANNOTATION TO REBUILD ANNOTATION //
dts = dt << Summary( Group( :VISIT, :TRT01A ), Freq( "None" ), Weight( "None" ), invisible );
annot_lsm = {};
met_l = {};
annot_pv = {};
CList = dt << Get Column Names (string);
For (j = 1, j <= N Rows (dts), j++,
insert into (annot_lsm, dts:VISIT [j] || ", " || dts:TRT01A [j]);
);
annot_pv = annot_lsm;
annot_pv = Concat To(annot_pv, {"Estimate","Std Error", "t Ratio", "Prob>|t|", "SS", "Lower 95%", "Upper 95%"});
Close (dts, NoSave);
// END: CAPTURING OUTPUT ANNOTATION TO REBUILD ANNOTATION //
// START: CREATING EMPTY NASTER MATRICES TO HOLD TTHE OUTPUT OF THE FIT MODEL//
nro_l = 1933*6;
nco_l = 4;
nro_p = 1933 * 13;
nco_p = 2;
mtx_lsm = J(nro_l, nco_l);
mtx_pval = J(nro_p, nco_p);
// END: CREATING EMPTY MASTER MATRICES TO HOLD TTHE OUTPUT OF THE FIT MODEL//
//START MAIN LOOP//
For (i = 3921, i<= 5853, i++, //5853
//START LINEAR MODEL WITH CUSTGOMIZED CONTRASTS //
fm = dt << Fit Model(
Y( Column (i) ),
Effects( :VISIT, :TRT01A, :VISIT * :TRT01A, Column (i-1933) ),
Personality( "Standard Least Squares" ),
Emphasis( "Minimal Report" ),
Run(
Column (i) << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
Parameter Estimates( 1 ), Scaled Estimates( 0 ),
Plot Actual by Predicted( 0 ), Plot Regression( 0 ),
Plot Residual by Predicted( 0 ), Plot Studentized Residuals( 0 ),
Plot Effect Leverage( 0 ), Plot Residual by Normal Quantiles( 0 ),
Box Cox Y Transformation( 0 ), {:VISIT * :TRT01A <<
{LSMeans Contrast( [0 0 1 -1 0 0, 0 0 0 0 1 -1] )}}}
)
);
//END LINEAR MODEL WITH CUSTGOMIZED CONTRASTS //
//START CAPTURE ANALYSIS OUTPUTS FOR EACH RUN//
fmr = report (fm);
insert into (met_l, CList [i]); //COLLATING THE NAME OF THE COLUMN FOR EACH RUN//
table_list = fmr << xpath ("//OutlineBox [text() = 'VISIT*TRT01A']//TableBox");
temp_lsm = table_list [1]; //POINTER TO LSMEAN + STDERR TABLE//
temp_pval = table_list [2]; // POINTER TO CONTRAST TABLE //
temp_mtx_lsm = temp_lsm << Get as Matrix; // EXTRACT LSMEAN + STDERR OUTPUT AS A MATRIX //
temp_mtx_pval = temp_pval << Get as Matrix; // EXTRACT CONTRAST OUTPUT AS A MATRIX //
locr1_l = ((i-3921)*6) + 1; //GENERATE THE TOP ROW BLOCK FOR INSERTION OF LSMEAN + STDERR IN TO THE MASTER MATRIX //
locr2_l = ((i-3921)*6) + 6; //GENERATE THE BOTTOM ROW BLOCK FOR INSERTION OF LSMEAN + STDERR IN TO THE MASTER MATRIX //
mtx_lsm [locr1_l::locr2_l, 0] = temp_mtx_lsm [0,0]; //REPLACE PLACEHOLDER VALUES IN MASTER MATRIX WITH LOCAL LSMEAN + STDERR OUTPUT DATA FROM EACH RUN //
locr1_p = ((i-3921)*13) + 1; //GENERATE THE TOP ROW BLOCK FOR INSERTION OF CONTRAST IN TO THE MASTER MATRIX //
locr2_p = ((i-3921)*13) + 13; //GENERATE THE BOTTOM ROW BLOCK FOR INSERTION OF CONTRAST IN TO THE MASTER MATRIX //
mtx_pval [locr1_p::locr2_p, 0] = temp_mtx_pval [0,0]; //REPLACE PLACEHOLDER VALUES IN MASTER MATRIX WITH LOCAL CONTRAST OUTPUT DATA FROM EACH RUN //
fmr << close window;
);
// START CONVERT MASTER MATRICES INTO TABLES//
dtoutl = as Table (mtx_lsm);
dtoutp = as Table (mtx_pval);
// END CONVERT MASTER MATRICES INTO TABLES//
//START REBUILD ANNOTATIONS AND REFORMAT FOR LSMEAN + STDERR TABLE//
dtoutl << new column ("COMP", Character, Nominal);
dtoutl << new column ("MET_ID", Character, Nominal);
dtoutl:Col1 << set name ("LSMEAN");
dtoutl:Col2 << set name ("STDERR");
dtoutl:Col3 << set name ("UPPER CI");
dtoutl:Col4 << set name ("LOWER CI");
dtoutl << Begin Data Update;
For Each Row( dtoutl, :COMP = annot_lsm[Sequence( 1, N Items (annot_lsm), 1, 1 )] );
For each row( dtoutl, :MET_ID = met_l[Sequence (1,N Items (met_l), 1, 6)] );
dtoutl << End Data Update;
dtoutl << New Column ("METAB_ID", Character, Nominal, Formula (Substitute (:MET_ID, "LOG10 FC ", ""))) ;
dtoutl << New Column ("VISIT", Character, Nominal, Formula (Word (1,:COMP, ",")));
dtoutl << New Column ("ARM", Character, Ordinal, Formula (Word (-1, :COMP, ",")));
dtoutl:METAB_ID << Delete Formula;
dtoutl:ARM << Delete Formula;
dtoutl:VISIT << Delete Formula;
//END REBUILD ANNOTATIONS AND REFORMAT FOR LSMEAN + STDERR TABLE//
//START REBUILD ANNOTATIONS AND REFORMAT FOR CONTRAST TABLE//
dtoutp << new column ("LABEL", Character, Nominal);
dtoutp << new column ("MET_ID", Character, Nominal);
dtoutp << Begin Data Update;
For Each Row( dtoutp, :LABEL = annot_pv[Sequence( 1, N Items (annot_pv), 1, 1 )] );
For each row( dtoutp, :MET_ID = met_l[Sequence (1,N items (met_l), 1, 13)] );
dtoutp << End Data Update;
CNAME1 = Column (dtoutp, 3) [3];
CNAME2 = Column (dtoutp, 3) [5];
dtoutp << Select Where (:LABEL == "Prob>|t|");
dtoutp_s = dtoutp << subset (Selected Rows, Output Table Name ("P VAL"));
Close (dtoutp, NoSave);
dtoutp_s << New Column ("METAB_ID", Character, Nominal, Formula (Substitute (:MET_ID, "LOG10 FC ", "")));
dtoutp_s:Col1 << Set Name (CNAME1);
dtoutp_s:Col2 << Set Name (CNAME2);
dtoutp_s_s = dtoutp_s << Stack (Columns (as name(CNAME1), as name (CNAME2)), Output Table Name ("STACK P VAL"));
Close (dtoutp_s, NoSave);
dtoutp_s_s << New Column ("VISIT", Character, Nominal, Formula (Word (1,:Label, ",")));
dtoutp_s_s << New Column ("xARM", Character, Ordinal, Formula (Word (-1, :Label, ",")));
dtoutp_s_s:Data << Set name ("NOM P VAL");
dtoutl << Update(
With( dtoutp_s_s ),
Match Columns( :METAB_ID = :METAB_ID, :VISIT = :VISIT ),
Add Columns from Update Table( :NOM P VAL ),
Replace Columns in Main Table( None )
);
Close (dtoutp_s_s, NoSave);
//END REBUILD ANNOTATIONS AND REFORMAT FOR CONTRAST TABLE//
dtoutl << Save (substitute (lc_path, dt << get name||".jmp", "LINMOD_OUT_")||char(today())); //
et = Today ();
show (Date Difference (st, et, "second"));
最好的,
TS
回复:JMP > JSL > 多次调用拟合模型(N > 2000)的脚本加速?凉爽的! 我认为社区希望看到您为实现目标所做的工作,或者总结一下不同变化的重要性。
@DonMcCormack
回复:JMP > JSL > 多次调用拟合模型(N > 2000)的脚本加速?你好@Craige_Hales ,
谢谢你的建议。 感谢您推荐的方法,我的速度又提高了 33%。
我仍然需要完善一些调整,但我开始时的总运行时间约为 450 秒,初始更改时降至 91 秒,现在降至 61 秒。
最好的
TS
回复:JMP > JSL > 多次调用拟合模型(N > 2000)的脚本加速?5X 听起来很棒!
该平台可能还有一个隐形选项可能会有所帮助。
(试试 JSL 分析器,下一个建议可能会离谱。 使用 < 1% 的时间来加快速度是没有意义的……)
如果还不够,循环中的最后两个语句可能是下一个要查看的地方。 我认为您的 JSL 正在以越来越大的大小重新分配数组 2000 次。 如果您可以预先分配数组(使用 J(nr,nc,initvalue) 函数)并使用 mtx_lsm[a::b,0] = temp_mtx_lsm[0,0] 之类的内容存储到其中,您将节省一些时间.
编辑:这可能是一个 N^2 问题,因为它不仅会重新分配,还会复制数据;从 1000 次运行到 2000 次运行将复制 4 倍(而不是 2 倍)的数据量。
回复:JMP > JSL > 多次调用拟合模型(N > 2000)的脚本加速?您好 JMP 社区,
我已经尝试使用矩阵作为容器来聚合分析输出表的长集合,到目前为止,我已经将执行时间加快了约 5 倍(见下文)。 唯一的小不便是我不得不为捕获的输出表重建注释,因为(显然)矩阵不包含文本。
Names Default to Here (1);
st = Today();
dt = Current Data Table ();
dts = dt << Summary( Group( :VISIT, :TRT01A ), Freq( "None" ), Weight( "None" ), invisible );
annot_lsm = {};
met_l = {};
annot_pv = {};
CList = dt << Get Column Names (string);
For (j = 1, j <= N Rows (dts), j++,
insert into (annot_lsm, dts:VISIT [j] || ", " || dts:TRT01A [j]);
);
annot_pv = annot_lsm;
annot_pv = Concat To(annot_pv, {"Estimate","Std Error", "t Ratio", "Prob>|t|", "SS", "Lower 95%", "Upper 95%"});
Close (dts, NoSave);
mtx_lsm = [];
mtx_pval = [];
For (i = 1, i<= 2000, i++, //2000
fm = dt << Fit Model(
Y( Column (i) ),
Effects( :VISIT, :TRT01A, :VISIT * :TRT01A, Column (i+2000) ),
Personality( "Standard Least Squares" ),
Emphasis( "Minimal Report" ),
Run(
Column (i) << {Summary of Fit( 1 ), Analysis of Variance( 1 ),
Parameter Estimates( 1 ), Scaled Estimates( 0 ),
Plot Actual by Predicted( 0 ), Plot Regression( 0 ),
Plot Residual by Predicted( 0 ), Plot Studentized Residuals( 0 ),
Plot Effect Leverage( 0 ), Plot Residual by Normal Quantiles( 0 ),
Box Cox Y Transformation( 0 ), {:VISIT * :TRT01A <<
{LSMeans Contrast( [0 0 1 -1 0 0, 0 0 0 0 1 -1] )}}}
)
);
fmr = report (fm);
insert into (met_l, CList [i]);
table_list = fmr << xpath ("//OutlineBox [text() = 'VISIT*TRT01A']//TableBox");
temp_lsm = table_list [1];
temp_pval = table_list [2];
temp_mtx_lsm = temp_lsm << Get as Matrix;
temp_mtx_pval = temp_pval << Get as Matrix;
fmr << close window;
mtx_lsm = mtx_lsm |/ temp_mtx_lsm;
mtx_pval = mtx_pval |/ temp_mtx_pval ;
);
dtoutl = as Table (mtx_lsm);
dtoutp = as Table (mtx_pval);
dtoutl << new column ("COMP", Character, Nominal);
dtoutl << new column ("MET_ID", Character, Nominal);
dtoutl:Col1 << set name ("LSMEAN");
dtoutl:Col2 << set name ("STDERR");
dtoutl:Col3 << set name ("UPPER CI");
dtoutl:Col4 << set name ("LOWER CI");
dtoutl << Begin Data Update;
For Each Row( dtoutl, :COMP = annot_lsm[Sequence( 1, N Items (annot_lsm), 1, 1 )] );
For each row( dtoutl, :MET_ID = met_l[Sequence (1,N Items (met_l), 1, 6)] );
dtoutl << End Data Update;
dtoutp << new column ("LABEL", Character, Nominal);
dtoutp << new column ("MET_ID", Character, Nominal);
dtoutp << Begin Data Update;
For Each Row( dtoutp, :LABEL = annot_pv[Sequence( 1, N Items (annot_pv), 1, 1 )] );
For each row( dtoutp, :MET_ID = met_l[Sequence (1,N items (met_l), 1, 13)] );
dtoutp << End Data Update;